2017-11-17 23:06:00 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-20 18:37:05 +01:00
|
|
|
class Tower{
|
2017-11-17 23:06:00 +01:00
|
|
|
private $tower0;
|
|
|
|
private $tower1;
|
|
|
|
private $tower2;
|
|
|
|
public $discsCount;
|
|
|
|
|
|
|
|
/* on the hannoi tower
|
|
|
|
*/
|
2017-11-20 18:37:05 +01:00
|
|
|
public function __construct($discsCount,$towerArr = array()){
|
2017-11-17 23:06:00 +01:00
|
|
|
$this->discsCount = $discsCount;
|
2017-11-20 18:37:05 +01:00
|
|
|
if(empty($towerArr)){
|
|
|
|
|
|
|
|
//starting position for the tower
|
|
|
|
$this->tower0 = array();
|
|
|
|
for ( $i = $this->discsCount - 1 ;$i > -1 ; $i-- ){
|
|
|
|
$this->tower0[] = $i;
|
|
|
|
}
|
|
|
|
$this->tower1 = array();
|
|
|
|
$this->tower2 = array();
|
|
|
|
}else{
|
|
|
|
$this->tower0 = $towerArr[0];
|
|
|
|
$this->tower1 = $towerArr[1];
|
|
|
|
$this->tower2 = $towerArr[2];
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __toString(){
|
2017-11-20 18:37:05 +01:00
|
|
|
return json_encode($this->get_tower_array());
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 10:09:10 +01:00
|
|
|
|
2017-11-17 23:06:00 +01:00
|
|
|
public function list_moves_availables(){
|
|
|
|
$movesAvailables = array();
|
2017-11-21 10:09:10 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $movesAvailables;
|
|
|
|
}
|
2017-11-21 10:09:10 +01:00
|
|
|
|
2017-11-22 19:05:23 +01:00
|
|
|
public function get_tower_array(){
|
2017-11-17 23:06:00 +01:00
|
|
|
return array(
|
|
|
|
$this->tower0,
|
|
|
|
$this->tower1,
|
|
|
|
$this->tower2
|
|
|
|
);
|
|
|
|
}
|
2017-11-20 18:37:05 +01:00
|
|
|
private function check_if_move_is_available(Move $move){
|
2017-11-21 10:09:10 +01:00
|
|
|
if($move === false){
|
2017-11-20 18:37:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-11-21 10:09:10 +01:00
|
|
|
$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;
|
2017-11-20 18:37:05 +01:00
|
|
|
}
|
2017-11-17 23:06:00 +01:00
|
|
|
|
|
|
|
public function add_move(Move $move){
|
2017-11-20 19:26:05 +01:00
|
|
|
if($this->check_if_move_is_available($move)){
|
2017-11-20 18:37:05 +01:00
|
|
|
$towerArr = $this->get_tower_array();
|
|
|
|
$towerArr[$move->to][] = end($towerArr[$move->from]);
|
|
|
|
array_pop($towerArr[$move->from]);
|
|
|
|
return new Tower( $this->discsCount, $towerArr );
|
|
|
|
}
|
|
|
|
return false;
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
2017-11-22 19:05:23 +01:00
|
|
|
public function is_won(){
|
|
|
|
$arr = $this->get_tower_array();
|
|
|
|
if ((empty($arr[0]) && empty($arr[1]))
|
|
|
|
||(empty($arr[0]) && empty($arr[2]))){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 23:06:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
class Move{
|
|
|
|
|
|
|
|
private static $from0to1 = 0;
|
|
|
|
private static $from0to2 = 1;
|
|
|
|
private static $from1to0 = 2;
|
|
|
|
private static $from1to2 = 3;
|
|
|
|
private static $from2to0 = 4;
|
|
|
|
private static $from2to1 = 5;
|
|
|
|
|
2017-11-20 18:37:05 +01:00
|
|
|
public $from;
|
|
|
|
public $to;
|
2017-11-17 23:06:00 +01:00
|
|
|
|
|
|
|
private $value;
|
|
|
|
|
2017-11-21 10:09:10 +01:00
|
|
|
public function __construct(){
|
2017-11-17 23:06:00 +01:00
|
|
|
$this->value = 0;
|
|
|
|
}
|
|
|
|
|
2017-11-21 10:09:10 +01:00
|
|
|
private function set_value($value){
|
|
|
|
$this->value = $value;
|
|
|
|
switch ($value){
|
|
|
|
case Move::$from0to1:
|
|
|
|
$this->from = 0;
|
|
|
|
$this->to = 1;
|
|
|
|
break;
|
|
|
|
case Move::$from0to2:
|
|
|
|
$this->from = 0;
|
|
|
|
$this->to = 2;
|
|
|
|
break;
|
|
|
|
case Move::$from1to0:
|
|
|
|
$this->from = 1;
|
|
|
|
$this->to = 0;
|
|
|
|
break;
|
|
|
|
case Move::$from1to2:
|
|
|
|
$this->from = 1;
|
|
|
|
$this->to = 2;
|
|
|
|
break;
|
|
|
|
case Move::$from2to0:
|
|
|
|
$this->from = 2;
|
|
|
|
$this->to = 0;
|
|
|
|
break;
|
|
|
|
case Move::$from2to1:
|
|
|
|
$this->from = 2;
|
|
|
|
$this->to = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
2017-11-21 10:09:10 +01:00
|
|
|
}
|
|
|
|
public function __toString(){
|
|
|
|
switch($this->value){
|
|
|
|
case Move::$from0to1:
|
|
|
|
return "from col 0 to 1";
|
|
|
|
break;
|
|
|
|
case Move::$from0to2:
|
|
|
|
return "from col 0 to 2";
|
|
|
|
break;
|
|
|
|
case Move::$from1to0:
|
|
|
|
return "from col 1 to 0";
|
|
|
|
break;
|
|
|
|
case Move::$from1to2:
|
|
|
|
return "from col 1 to 2";
|
|
|
|
break;
|
|
|
|
case Move::$from2to0:
|
|
|
|
return "from col 2 to 0";
|
|
|
|
break;
|
|
|
|
case Move::$from2to1:
|
|
|
|
return "from col 2 to 1";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
2017-11-21 10:09:10 +01:00
|
|
|
}
|
|
|
|
public static function make($from,$to){
|
|
|
|
$move = new Move();
|
|
|
|
if(($from == 0) && ($to == 1))
|
|
|
|
$move->set_value(Move::$from0to1);
|
|
|
|
elseif(($from == 0) && ($to == 2))
|
|
|
|
$move->set_value(Move::$from0to2);
|
|
|
|
elseif(($from == 1) && ($to == 0))
|
|
|
|
$move->set_value(Move::$from1to0);
|
|
|
|
elseif(($from == 1) && ($to == 2))
|
|
|
|
$move->set_value(Move::$from1to2);
|
|
|
|
elseif(($from == 2) && ($to == 0))
|
|
|
|
$move->set_value(Move::$from2to1);
|
|
|
|
elseif(($from == 2) && ($to ==1))
|
|
|
|
$move->set_value(Move::$from2to1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return $move;
|
2017-11-20 18:37:05 +01:00
|
|
|
}
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
|
|
|
class Steps{
|
|
|
|
/*
|
|
|
|
*Memorise the differents towers configurations
|
|
|
|
*/
|
|
|
|
private $steps;
|
2017-11-22 19:05:23 +01:00
|
|
|
public function __construct(){
|
2017-11-17 23:06:00 +01:00
|
|
|
$this->steps = array();
|
|
|
|
}
|
|
|
|
|
2017-11-21 10:09:10 +01:00
|
|
|
private function convert_column_to_int($column,$base){
|
|
|
|
$exp = 0;
|
|
|
|
$total = 0;
|
|
|
|
foreach($column as $elem){
|
2017-11-22 19:05:23 +01:00
|
|
|
$total =$total + ($elem + 1) * pow( $base + 1 , $exp );
|
2017-11-21 10:09:10 +01:00
|
|
|
$exp++;
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convert_tower_to_int(Tower $tower){
|
|
|
|
$arrTower = $tower->get_tower_array();
|
|
|
|
return
|
2017-11-22 19:05:23 +01:00
|
|
|
$this::convert_column_to_int($arrTower[0],$tower->discsCount) +
|
|
|
|
$this::convert_column_to_int($arrTower[1],$tower->discsCount) * pow(10,$tower->discsCount) +
|
|
|
|
$this::convert_column_to_int($arrTower[2],$tower->discsCount) * pow(10,2 * $tower->discsCount);
|
2017-11-21 10:09:10 +01:00
|
|
|
}
|
2017-11-22 19:05:23 +01:00
|
|
|
|
2017-11-20 18:37:05 +01:00
|
|
|
public function add_step(Tower $tower){
|
2017-11-21 10:09:10 +01:00
|
|
|
$towerValue = $this::convert_tower_to_int($tower);
|
2017-11-17 23:06:00 +01:00
|
|
|
if(in_array($towerValue,$this->steps)){
|
2017-11-22 19:05:23 +01:00
|
|
|
return false;
|
2017-11-17 23:06:00 +01:00
|
|
|
}else{
|
|
|
|
$this->steps[] = $towerValue;
|
2017-11-22 19:05:23 +01:00
|
|
|
return true;
|
2017-11-17 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|