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

View File

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