Tower of Hanoi Class
Posted by Chad Humphries over 2 years ago
Tower of Hanoi game.


*
* To use this class simply define the object with
* how many discs, the starting peg, the ending peg:
*
* $hanoi= new hanoi(3, 1, 3);
*
* To have the computer solve it for you:
*
* $hanoi->solve();
*
* To move things by yourself use the move function
* with parameters of the start peg and end peg. It
* will take the top disc on the start peg and move to
* the top of the end peg:
*
* $hanoi->move(0,2); // top disc from first peg to top of 3rd peg
*
* To show what discs are on what pegs:
*
* $hanoi->show();
*
* <-# Example Solution For 3 Dsics #->
*
* $hanoi->move(0, 2);
* $hanoi->move(0, 1);
* $hanoi->move(2, 1);
* $hanoi->move(0, 2);
* $hanoi->move(1, 0);
* $hanoi->move(1, 2);
* $hanoi->move(0, 2);
*
**********************************************/


class hanoi 
{
    var $discs;
    var $start;
    var $end;
    var $remain;
    var $pegs = array(array());

    function __construct ($discs, $start, $end)
    {
        $this->discs = $discs;
        $this->start = $start-1;
        $this->end = $end-1;
        $this->remain = (6 - $start - $end) - 1;
        
        for($i=0;$i<$discs;$i++)
            $this->peg[0][$i] = $i;
    }

    function check_win()
    {
        if(!(count($this->peg[$this->start])) 
            && !(count($this->peg[$this->remain])) 
            && (count($this->peg[$this->end]) == $this->discs))
        {
            echo "You Win!\n";
            die();
        }
    }
    
    function top_piece($peg)
    {
        $test = 0;
        if(isset($this->peg[$peg]))
        {
            foreach($this->peg[$peg] as $value)
            {
                if($test<=$value)
                    $test = $value;
            }
        }
        return $test;
    }

    function move ($start, $end)
    {
        $from = $this->top_piece($start);
        $to = $this->top_piece($end);
        if($to <= $from)
        {
            unset($this->peg[$start][$from]);
            $this->peg[$end][$from] = $from;
            
            echo "Moved top disc from peg $start to peg $end\n";
            $this->check_win();
        }
        else
        {
            echo "Invalid Move: Cannot move top disc from peg $start to peg $end\n";
        }
    }

    function solve ()
    {
        $this->solve_main($this->discs, $this->start, $this->end);
    }
    
    function solve_main($discs, $start, $end)
    {
        if ($discs == 1)
        {
            echo "Move top disc from peg $start to peg $end\n";
        }
        else
        {
            $this->solve_main(($discs-1), $start, $this->solve_remain($start, $end));
            $this->solve_main(1, $start, $end);
            $this->solve_main(($discs-1), $this->solve_remain($start, $end), $end);
        }
    }
    
    function solve_remain($peg1, $peg2)
    {
        return (6 - $peg1 - $peg2);
    }

    function show()
    {
        for($i=1;$i<=3;$i++)
        {
            echo "Peg $i: ";
            if(isset($this->peg[$i-1]))
            {
                foreach($this->peg[$i-1] as $value)
                    echo "$value ";
            }
            echo "\n";
        }
    }
}
?> 
Language PHP / Tagged with methods