User:Tisane/Rubik's cube program

';              //print ' ';            //   for ($faceCount=1; $faceCount<=6; $faceCount++) { //      print 'Face'.$faceCount; //                    }            }

// Make this cube perfectly solved public function createDefaultCube { for ($faceCount=1; $faceCount<=6; $faceCount++) { // Each color is associated with a letter. E.g., W is White. for ($squareCount=1; $squareCount<=9; $squareCount++) {

switch ($faceCount) { case 1: $color='W'; break; case 2: $color='O'; break; case 3: $color='G'; break; case 4: $color='R'; break; case 5: $color='B'; break; case 6: $color='Y'; break; }                       // Add square numbers $this->setSquare($faceCount,$squareCount,$color.$squareCount);

}               }                return; }

public function solveCube{ global $solvedCube; global $myHistory; $solvedCube->createDefaultCube; //$solvedIt=$this->compareCube($solvedCube); //if ($solvedIt==false) $this->anotherMove; $myHistory->showMeTheHistory;

}           public function manipulateCube($whichMove){ switch ($whichMove){ case 1: $this->rotateRight(1); break; case 2: $this->rotateRight(2); break; case 3: $this->rotateRight(3); break; case 4: $this->rotateDown(1); break; case 5: $this->rotateDown(2); break; case 6: $this->rotateDown(3); break; }           }            public function anotherMove{

global $myHistory; global $solvedCube; $solvedIt=$this->compareCube($solvedCube); // Have we solved it? // $this->showMeTheCube; if ($solvedIt==true){ return true; // We're done! Now let's get out of this recursion. }               else {                   if ($myHistory->isItInTheHistory($this)==true) return false; // We've been here before for ($count=1; $count<=6 && $solvedIt==false; $count++){ $this->manipulateCube($count); // Manipulate the cube $this->showMeTheCube; $myHistory->storeThisCube($this,$count); // Store the current cube and move info $solvedIt=$this->anotherMove; // Now try manipulating the cube a different way if ($solvedIt==false) $myHistory->eraseThisCube; // Go back }                   if ($solvedIt==true) return true; // Again, let's GTFO

else return false; // We'll have to retrace our steps }               }            } class history{ private $historyNumber=0; private $highestWeHaveBeen=0; public function storeThisCube($whichCube,$whichMove) {       $this->historyNumber++; if ($this->historyNumber>$this->highestWeHaveBeen) {           $this->historyCube[$this->historyNumber]=new cube; $this->highestWeHaveBeen++; }       $this->historyCube[$this->historyNumber]->duplicateCube($whichCube); $this->historyCube[$this->historyNumber]->setMove($whichMove); }

public function eraseThisCube {       $this->historyNumber--; }

public function isItInTheHistory($whichCube){ for ($count=1; $count<$this->historyNumber; $count++){ // Notice the operator is <, not <= if ($this->historyCube[$count]->compareCube($whichCube)==true){ return true; }       }        return false; }

public function showMeTheHistory{ for ($count=1; $count<=$this->historyNumber; $count++){ echo $this->historyCube[$count]->displayMove; }   } }

// Main program $myCube= new cube; $myCube->createDefaultCube; //$myCube->showMeTheCube; $myCube->rotateDown(1); //      $myCube->rotateRight(1); //    $myCube->rotateRight(1); //$myCube->showMeTheCube; //$myCube->rotateDown(1); //$myCube->showMeTheCube; $myCube->solveCube; ?>