Hanoi-towers/resolver.php
2017-11-22 19:11:13 +01:00

37 lines
822 B
PHP

<?php
$discCount = 6;
//load the class
include("class.towers.php");
$tower = new Tower($discCount);
$steps = new Steps(array());
$steps->add_step($tower);
resolveHanoi($tower,$steps);
function resolveHanoi(Tower $tower, Steps $steps){
$result = false;
if($tower->is_won()){
echo "\nSolution founded. Reverse this steps:\n".$tower;
return true;
}
$availablesMoves = $tower->list_moves_availables();
//take only the moves who will generate a new unknowed Tower
foreach($availablesMoves as $move){
$newTower = $tower->add_move($move);
$newSteps = $steps;
if($newSteps->add_step($newTower)){
$r = resolveHanoi($newTower,$newSteps);
if($r){
$result = true;
echo "\n".$tower;
}
}
}
return $result;
}