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(){
$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){ public function list_moves_availables(){
if ($this->check_if_move_is_available($move['from'],$move['to'])){ $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; $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,17 +52,20 @@ 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;
} }
$fromTower = "tower".$move->from;
$toTower = "tower".$move->to;
if( (!empty($this->{$fromTower}))
&&( (end($this->{$toTower}) > end($this->{$fromTower}))
||(empty ($this->{$toTower}))
)
){
return true; return true;
} }
return false;
}
public function add_move(Move $move){ public function add_move(Move $move){
if($this->check_if_move_is_available($move)){ if($this->check_if_move_is_available($move)){
@ -96,7 +92,7 @@ class Move{
private $value; private $value;
public function __construst(){ public function __construct(){
$this->value = 0; $this->value = 0;
} }
@ -157,31 +153,23 @@ class Move{
break; break;
} }
} }
public static function make($str){ public static function make($from,$to){
$move = new Move(); $move = new Move();
switch((string)$str){ if(($from == 0) && ($to == 1))
case "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);
break; elseif(($from == 1) && ($to == 0))
case "1 to 0":
$move->set_value(Move::$from1to0); $move->set_value(Move::$from1to0);
break; elseif(($from == 1) && ($to == 2))
case "1 to 2":
$move->set_value(Move::$from1to2); $move->set_value(Move::$from1to2);
break; elseif(($from == 2) && ($to == 0))
case "2 to 0":
$move->set_value(Move::$from2to0);
break;
case "2 to 1":
$move->set_value(Move::$from2to1); $move->set_value(Move::$from2to1);
break; elseif(($from == 2) && ($to ==1))
default: $move->set_value(Move::$from2to1);
else
return false; return false;
break;
}
return $move; return $move;
} }
} }
@ -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){
} }
*/