User:Tisane/Rubik's cube saga
On Friday, February 19, 2010, this user rose to the podium and declared to the gathered multitudes of the wiki:
“ | I choose to write a PHP computer program to recursively solve the Rubik's cube in this weekend, not because it is easy, but because it is hard, because that goal will serve to organize and measure the best of my energies and skills, because that challenge is one that I am willing to accept, one I am unwilling to postpone, and one which I intend to win. | ” |
That night, this user purchased a Rubix cube from Target and marked square numbers (1-9 on each face, in the same configuration as a telephone keypad) all over the six sides with a permanent marker. This user then spent several hours trying to get Apache to work with PHP, before finally reading in his trusty copy of Learning PHP 5 that an easier option, EasyPHP, is available.[1] A quick install of that immediately took care of that problem, and after firing up Netbeans, soon this user had a "Hello world" program up and running on the localhost.
This user had written out the pseudocode a long time ago, so now it was just a matter of implementing it. But ah, how the mythical fallen angel was in the details! Setting up six arrays of nine squares apiece was easy, but it was surprisingly tricky getting the rotateRight and rotateDown functions to work. This user is not used to programming languages that require you to put a dollar sign in front of all your variables. Hopefully, that dollar sign represents how much money this user is going to make once this user becomes a skilled PHP developer. There were six possible moves:
- Rotate top row right
- Rotate middle row right
- Rotate bottom row right
- Rotate left column down
- Rotate middle column down
- Rotate right column down
Finally, the rotation functions passed initial tests as being bug-free. This user then began programming the artificial intelligence. This particular AI uses a brute force approach. It tests the cube to see if it is solved yet. If not, it cycles through each of the six possible basic movements of the cube. Each time it manipulates the cube, it checks to see if the cube has ever been in this position before. If so, it undoes that move and tries a different one. If all six possible moves are exhausted, then it goes back a step (by returning to the previous level of recursion). In this way, it is much like solving the eight queens puzzle or the knight's tour. All that is needed is that you throw a certain amount of computing power at the problem, and leave it to exhaust all other possibilities but the correct one.
Then this user ran into difficulties involving scope - specifically, how to modify the history from the recursive function that solves the cube. This was solved using global variables. Soon, this user had confirmed that the cube can solve some simple permutations - e.g., when one of the rows has been rotated. But it cannot solve any permutation involving downward movement within 60 seconds. I can't tell whether there's a flaw in the program or whether it just lacks adequate computing firepower. See User:Tisane/Rubik's cube program. It is an anticlimactic ending, to be sure.
- ^ EasyPHP was good enough for this project, but XAMPP is better in general; it has less of a tendency to give you white screens of death in PHPmyAdmin.