Hanoi-towers/resolver.php

37 lines
822 B
PHP
Raw Normal View History

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