This commit is contained in:
Gnieark 2017-11-21 10:09:10 +01:00
parent 6f0b9eb321
commit 1e08e2a86b
2 changed files with 133 additions and 120 deletions

View File

@ -30,27 +30,20 @@ class Tower{
return json_encode($this->get_tower_array());
}
/*
to rewrite with move object
public function list_moves_availables(){
$moves = array(
array('from' => 0, 'to' => 1),
array('from' => 0, 'to' => 2),
array('from' => 1, 'to' => 0),
array('from' => 1, 'to' => 2),
array('from' => 2, 'to' => 0),
array('from' => 2, 'to' => 1)
);
$movesAvailables = array();
foreach($moves as $move){
if ($this->check_if_move_is_available($move['from'],$move['to'])){
public function list_moves_availables(){
$movesAvailables = array();
for( $i = 0; $i < 3; $i++ ){
for( $j = 0; $j < 3; $j++ ){
$move = Move::make($i,$j);
if(($move !== false) && ($this->check_if_move_is_available($move))){
$movesAvailables[] = $move;
}
}
}
return $movesAvailables;
}
*/
private function get_tower_array(){
return array(
$this->tower0,
@ -59,17 +52,20 @@ class Tower{
);
}
private function check_if_move_is_available(Move $move){
$fromTower = "tower".$move->from;
$toTower = "tower".(string)$move->to;
echo "kk$toTower\n".$move."|";
print_r($this->{$toTower});
//echo end($this->$toTower)."|". end($this->$fromTower);
if(($move!==false) || (empty($this->$fromTower)) || (end($this->$toTower) < end($this->$fromTower))){
if($move === false){
return false;
}
$fromTower = "tower".$move->from;
$toTower = "tower".$move->to;
if( (!empty($this->{$fromTower}))
&&( (end($this->{$toTower}) > end($this->{$fromTower}))
||(empty ($this->{$toTower}))
)
){
return true;
}
return false;
}
public function add_move(Move $move){
if($this->check_if_move_is_available($move)){
@ -96,7 +92,7 @@ class Move{
private $value;
public function __construst(){
public function __construct(){
$this->value = 0;
}
@ -157,31 +153,23 @@ class Move{
break;
}
}
public static function make($str){
public static function make($from,$to){
$move = new Move();
switch((string)$str){
case "0 to 1":
if(($from == 0) && ($to == 1))
$move->set_value(Move::$from0to1);
break;
case "0 to 2":
elseif(($from == 0) && ($to == 2))
$move->set_value(Move::$from0to2);
break;
case "1 to 0":
elseif(($from == 1) && ($to == 0))
$move->set_value(Move::$from1to0);
break;
case "1 to 2":
elseif(($from == 1) && ($to == 2))
$move->set_value(Move::$from1to2);
break;
case "2 to 0":
$move->set_value(Move::$from2to0);
break;
case "2 to 1":
elseif(($from == 2) && ($to == 0))
$move->set_value(Move::$from2to1);
break;
default:
elseif(($from == 2) && ($to ==1))
$move->set_value(Move::$from2to1);
else
return false;
break;
}
return $move;
}
}
@ -194,15 +182,29 @@ class Steps{
$this->steps = array();
}
public function add_step(Tower $tower){
//3 tours contenant au max n valeurs de 0à n-1*
//a way to convert the tower to a simple integer (long)
$arr = $tower->get_tower_array();
$tower0 = int_val(base_convert(implode("",$arr[0]),$tower->discsCount,10));
$tower1 = int_val(base_convert(implode("",$arr[1]),$tower->discsCount,10));
$tower2 = int_val(base_convert(implode("",$arr[2]),$tower->discsCount,10));
private function convert_column_to_int($column,$base){
$exp = 0;
$total = 0;
foreach($column as $elem){
$total += $elem * pow( $base , $exp );
$exp++;
}
return $total;
}
private function convert_tower_to_int(Tower $tower){
$arrTower = $tower->get_tower_array();
return
$this::convert_column_to_int($arrTower[0]) +
$this::convert_column_to_int($arrTower[1]) * pow(10,$tower->discsCount) +
$this::convert_column_to_int($arrTower[2]) * pow(10,2 * $tower->discsCount);
}
public function add_step(Tower $tower){
$towerValue = $this::convert_tower_to_int($tower);
$towerValue = $tower0 * pow( 10, 2 * $tower->count) + $tower1 * pow( 10, $tower->count) + $tower2;
if(in_array($towerValue,$this->steps)){
return false;
}else{

View File

@ -6,19 +6,31 @@ include("class.towers.php");
$tower = new Tower($discCount);
$steps = new Steps();
/*
echo "1".$tower."\n";
$move = new Move();
$move->make("0 to 1");
$move = Move::make(0,2);
$tower = $tower->add_move($move);
echo "2".$tower."\n";
/*
print_r($tower->list_moves_availables());
*/
resolveHanoi($tower);
function resolveHanoi($tower){
function resolveHanoi(Tower $tower, Steps $steps){
//out of recursif if
$steps->add_step($tower);
$availablesMoves = $tower->list_moves_availables();
//take only the moves who will generate a new unknowed Tower
$uniquesAvailableMoves = array();
foreach($availablesMoves as $move){
$newTower = $tower-> add_move($move);
//to do check if unique ************************************************
}
//don't take moves that will generate an ever used tower configuration
$useful_moves = array();
foreach($availablesMoves as $move){
@ -27,4 +39,3 @@ function resolveHanoi($tower){
}
*/