master
Gnieark 7 years ago
parent 6f0b9eb321
commit 1e08e2a86b

@ -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'])){
$movesAvailables[] = $move;
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,16 +52,19 @@ 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;
}
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){
@ -96,93 +92,85 @@ class Move{
private $value;
public function __construst(){
public function __construct(){
$this->value = 0;
}
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;
}
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;
}
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;
}
}
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;
}
public static function make($str){
$move = new Move();
switch((string)$str){
case "0 to 1":
$move->set_value(Move::$from0to1);
break;
case "0 to 2":
$move->set_value(Move::$from0to2);
break;
case "1 to 0":
$move->set_value(Move::$from1to0);
break;
case "1 to 2":
$move->set_value(Move::$from1to2);
break;
case "2 to 0":
$move->set_value(Move::$from2to0);
break;
case "2 to 1":
$move->set_value(Move::$from2to1);
break;
default:
return false;
break;
}
return $move;
}
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;
}
}
class Steps{
@ -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 = $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{

@ -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){
//out of recursif if
function resolveHanoi(Tower $tower, Steps $steps){
$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){
}
*/

Loading…
Cancel
Save