|
|
|
@ -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'])){
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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){
|
|
|
|
|
//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));
|
|
|
|
|
|
|
|
|
|
$towerValue = $tower0 * pow( 10, 2 * $tower->count) + $tower1 * pow( 10, $tower->count) + $tower2;
|
|
|
|
|
|
|
|
|
|
$towerValue = $this::convert_tower_to_int($tower);
|
|
|
|
|
|
|
|
|
|
if(in_array($towerValue,$this->steps)){
|
|
|
|
|
return false;
|
|
|
|
|
}else{
|
|
|
|
|