Hanoi-towers/resolver.php

37 lines
823 B
PHP
Raw Normal View History

2017-11-17 23:06:00 +01:00
<?php
2017-11-22 19:15:26 +01:00
$discCount = 3;
2017-11-17 23:06:00 +01:00
//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()){
2017-11-22 19:15:26 +01:00
echo "\nSolution founded. Reverse these steps:\n".$tower;
2017-11-22 19:05:23 +01:00
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
}