We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone, I'd like to know what it's the best way to simulate a Rubik's cube in Processing ? Do have I to draw face as squares or collapse cubies ?
Answers
It is not the easiest thing to make. I have a friend that made one recently in Processing, this is the link to his project: https://sites.google.com/site/krishnabharadwaj85/rubikcube
What he did was create the faces as custom shapes and group them together into the Rubik's cube individual pieces. Using the built-in box() doesn't work because the faces need to have different colors.
What was more difficult was rotating the faces. His method was to keep track of the current state of the cube in a logical 3x3x3 array which determined what "sides" of the cubes "owned" individual pieces during rotations. This way after a rotation occurred the logical array would know which pieces should be rotated in response to the next rotation.
Okay thanks
Okay thanks but I can't find the source code in Processing
At the bottom are 2 sub pages:
- Implementation talks about things similar to what I put
- Instructions has the source code
solve.c has errors :-(
If I remember correctly that part communicates with a Rubik's cube solver online somewhere (I never actually looked at his code) but it definitely worked a few months ago. The part that is not responsible for solving should be in the Processing file
The problem is that the instruction says that I have to compile solve.c before but it can't compile because of an error and I don't want to change anything because the code is too long... and when I want to run the sketch in Processing it needs omicronAPI !!:? why ?
"Instructions has the source code"
Would have been more logical to put them in Implementation, but whatever.
solve.c isn't necessary if you don't plan to solve the cube, I think.
"it needs omicronAPI !!:? why ?"
Well, the answer is probably in the source code...
The idea behind pointing you to an existing source is to provide inspiration and ideas about a possible implementation, so some work might be necessary...
@PhiLho okay, I just wanted and existing and working example
That API is needed to run a Processing sketch on Cyber-Commons, the touch wall in the video.
Why don't you just copy / paste the relevant classes from his explanation to a new sketch? Unless you want to try to get the C code to talk to the online solver or display on a large scale touch device then the only relevant parts of the code should be the Rubik's cube sections. As PhiLho pointed out the point of posting this was not to get someone else's code running, it is to give an idea of how to start.
this could get you started
I tried to upload the code here but too long, forum blocked :(
here you go
https://www.dropbox.com/s/jzarxw8a8z19caa/rcube3.zip
@asimes: thanks but for the moment I just want to simulate the Rubik's cube (movements include). @Chrisir: thank you for the link I check that out and view if I can add mouse support or key input edit: @Chrisir: the sketch doesn't work :( it need FileReader
see this version without FileReader
https://www.dropbox.com/s/z6ssezk2y9xouoe/rcube3 (2).zip
Anyway, why is FileReader a problem, isn't that part of java?
It doesn't need the solver to move, it can move with what it provided. The solver just makes it move in a sequence that solves the cube.
Below is a further stripped-down version of the file Chrisir posted. It may not necessarily have all the things he or I mentioned completely removed, but it runs, displays the cube, and rotates it correctly. Features like touch interaction, auto solving, undoing movements, etc. are no longer in the sketch:
Part 2 (too long for 1 post):
thank you @asimes but who am I supposed to move each face ? @Chrisir: I've the same problem when I launch the sketch: black sketch :?
up
hello,
if you encounter a black screen (I don't) you have to find your error. Look into the console (direct window below the code). Is there a error message stated?
When you insert println lines in the code you can also see how far he gets before he stops. What processing version are you using?
You asked asimes about the moving (can you see the rubiks cube in his sketch? Then why not work with this?) - search picking in the forum, it is not easy though.
Chrisir
@Chrisir: thanks for the advices that I've forgot, now I can't check because I don't have my pc with me. I'm using Processing 2.1.1. Yes I can see the Rubik's cube in his sketch. Picking ? picking what ?
Berkmann18
To pick means to get what side/rect has been clicked with the mouse - so you could enter the move....
Ho okay
I come back to you, because I've now a problem for the solver that I've built myself without the c file. here's the solver() and what goes with it:
Question / problem is...?
@Chrisir: ho yeah sorry :(, what should I put into the code to make the computer find the desired piece and than execute the algorithm to place it as wanted ?
Can you solve it by hand?
Apply the same tactic.
Read an article about it and abstract it
To start playing: write a function that recognizes a solution (solved cube) and make random moves
I've already a scramble() and reset() and the function that allow key input moves but I want to have a solver working by AI and staying in Processing (not going to use C/C++ or whatever)
Can you solve it by hand?
I think to write a solver is pretty hard.
anyway, to do so, you
either must have understood how to do it and translate that into code or
you do some research (wikipedia, youtube, google) to find an explanation how to do it; so find code how to do it or an explanation how to do it and translate it into code.
It may be worth pointing out that Rubik's cube solving programs can make use of algorithms people cannot reasonably perform, like God's algorithm. A quick Google search:
@Chrisir: I can solve all until the OLL-PLL which is tricky for me (solving it doing the back moves which I usely don't do). For the translation of the algo to the computer I just need to insert it into
moveStr
beforemoveStrValid = true
but the real problem is searching the pieces, getting there position on the cube and than doing the correct algo depending on the method used .. @asime: this solver (in the video) doesn't look to be coded in Processing ?@Berkmann18, It is not Processing but none of the algorithms you'd be looking into or inventing are specific to Processing either. Edit: By this I mean that you need to think about how to implement them in Processing, none of the algorithms are described for a particular programming language
You might want to consider writing a solver for a simpler puzzle before taking on a Rubik's cube. A common programming puzzle solver is the 15 tile puzzle. The general algorithms involved in solving it (breadth-first search, depth-first search, A*) are useful to writing solvers in general.
I have never written a Rubik's cube solver, but from what I understand God's algorithm is based on iterative deepening A*.
when I did this I ended up generating a string showing the current state and using a table of regular expressions to find the next bunch of moves to do to solve the next piece without destroying what was already there. you can modify the string generating function according to stage you are at - sometimes you only need to know the edges for the top face, for instance.
it's not the most efficient way - it wouldn't recognise a starting position that was one turn away from solved - but I figured there was scope for peephole optimisation later on. (yes, you could just plug a pre-canned algorithm in there, but where's the fun in that? I wanted it to solve it the way I would)
(it's always bugged me the way the xscreensaver version cheats and just remembers the moves used in the scramble and replays them backwards.)
@asim: I know how to make the computer execute the algorithm (eg. scramble()) but I need know how to search and find a piece/cubie and depending on it position the computer execute the algorithm for the case.
@koogs: the thing is that I want that the AI works as for every step of every method:
searching the piece(s)/cubie(s)
place it/them in the correct place depending on the method used and which step has to be done.
And yes I want that it solve the cube as I'll solve mine or someone else, with the possibility to solve the cube with different method (here: Beginner's, Fridrich and Petrus).
And btw I've tried to see what the computer will do with the Undo method (undoing all moves until the solve-stat) but at my suprise it bugged the sketch and it's not fun to see the cube being "undoed" and not solved and I'd like to present what I've done at my CNS exam and I'll get a better mark if I use a good AI which realy solve the cube as an humain without having to know how many moves and which moves has been applied to the cube to scramble it.
(two newlines to force a new paragraph btw. i've fixed the above post)
what's the current state of the code? i can see asimes post above and two truncated findCubie() fragments but...
my code would generate a 54 character string to show the current state like
"wwwwwwwwwrrrrrrrrryyyyyyyyyooooooooogggggggggbbbbbbbbb"
(solved)for each step i had patterns defined that matched the string (with .s for pieces that didn't matter) and an associated move list to get the cube to the next required state. then would just use String.match to see which move list to use. i was doing this was one piece at a time so the match strings were quite easy and there weren't many. 24 for the first corner, 21 for the next corner, 18 for the third...
you can use this to locate edges etc, although the colours might not be together
".....w......r........................................."
- top right edge is in positionok, that's a mess. it didn't indent properly using pre tags and i can't redo it using spaces as it's too large. am splitting it into chunks and will delete the original (hadn't realised how long it was...)
part 1, setup and draw
part 2, cube class, stack class
part 3 rubikCube class
part 4 global methods
part 5 global methods cont
Yes so ? @koogs