Hanoi-towers/resolver.php

42 lines
948 B
PHP
Raw Normal View History

2017-11-17 23:06:00 +01:00
<?php
2017-11-23 23:38:43 +01:00
/*
* Hanoi towers ' resolver
* Copyright (C) 2017 Gnieark https://blog-du-grouik.tinad.fr/
* licensed under the terms of the GNU General Public V3. See License file.
*/
2017-11-17 23:06:00 +01:00
2017-11-23 23:52:56 +01:00
$discCount = 6;
2017-11-17 23:06:00 +01:00
//load the class
2017-11-23 23:38:43 +01:00
include("inc.php");
2017-11-17 23:06:00 +01:00
$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-23 23:38:43 +01:00
echo "a winning suite was found:\n";
foreach($steps->steps as $oneTower){
echo $oneTower."\n";
}
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();
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;
}
}
2017-11-17 23:06:00 +01:00
}
2017-11-22 19:05:23 +01:00
return $result;
2017-11-23 23:38:43 +01:00
}