Process-oriented solutions
<?php function Hanio ($n, $x, $y, $z) {//Put n plates, as required from X to Z,y is the mediation//recursive bounce condition if ($n ==1) {Move ($n, $x, $z);} else{//these three are core Hanio ($n-1, $x, $z, $y), Move ($n, $x, $z), Hanio ($n-1, $y, $x, $z);}} function Move ($n, $x, $y) {$format = ' move%d from%s to%s ';p rintf ($format, $n, $x, $y); echo "<br/>";} Hanio (2, ' A ', ' B ', ' C ');? >
Process-oriented writing
<?php class hanio{private $n;//scale private $start;//Start column private $mediator;//intermediary pillar private $goal;//target pillar private $format = ' Move%d from%s to%s ';p ublic function __construct ($n, $start, $mediator, $goal) {$this->n = $n; $this->start = $start; $ This->mediator = $mediator; $this->goal = $goal;} Single disk move private function Move ($n, $start, $goal) {printf ($this->format, $n, $start, $goal); echo "<br/>";} Public Function GetResult () {$this->handle ($this->n, $this->start, $this->mediator, $this->goal);} Private function handle ($n, $x, $y, $z) {//recursive jump-out condition if ($n ==1) {$this->move ($n, $x, $z);} else{//these three are core $this->handle ($n-1, $x, $z, $y), $this->move ($n, $x, $z), $this->handle ($n-1, $y, $x, $z);}} Class Client{public static function main () {$hanio = new Hanio (4, ' A ', ' B ', ' C '); $hanio->getresult ();}} Client::main ();? >
Hanoi Tower problem PHP Solution