This commit is contained in:
Gnieark 2017-11-20 18:37:05 +01:00
parent 3b38e2f894
commit ea305819ab
2 changed files with 70 additions and 52 deletions

View File

@ -1,6 +1,6 @@
<?php
class Towers{
class Tower{
private $tower0;
private $tower1;
private $tower2;
@ -8,54 +8,26 @@ class Towers{
/* on the hannoi tower
*/
public function __construct($discsCount,$towerArr){
public function __construct($discsCount,$towerArr = array()){
$this->discsCount = $discsCount;
//starting position for the tower
$this->tower0 = array();
for ( $i = $this->discsCount - 1 ;$i > -1 ; $i++ ){
$this->tower0[] = $i;
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];
}
$this->tower1 = array();
$this->tower2 = array();
}
public function move_disc($from,$to){
//checks for the hanois rules
if(
($from == $to)
||(!in_array($from,array(0,1,2)))
||(!in_array($to,array(0,1,2)))
){
return false;
}
$fromTower = "tower".$from;
$toTower = "tower".$to;
//the disc must be putted on a bigger one
if((empty($this->$fromTower)) || (end($this->$toTower) < end($this->$fromTower))){
return false;
}
//move the disc:
$this->$toTower[] = end($this->$fromTower);
array_pop($this->$fromTower);
return true;
}
private function check_if_move_is_available($from,$to){
$fromTower = "tower".$from;
$toTower = "tower".$to;
if(($from == $to) || (empty($this->$fromTower)) || (end($this->$toTower) < end($this->$fromTower))){
return false;
}
return true;
}
public function __toString(){
return json_encode($this->get_tower_array();
return json_encode($this->get_tower_array());
}
@ -78,16 +50,30 @@ class Towers{
return $movesAvailables;
}
public function get_tower_array(){
private function get_tower_array(){
return array(
$this->tower0,
$this->tower1,
$this->tower2
);
}
private function check_if_move_is_available(Move $move){
$fromTower = "tower".$move->from;
$toTower = "tower".$move->to;
if(($from == $to) || (empty($this->$fromTower)) || (end($this->$toTower) < end($this->$fromTower))){
return false;
}
return true;
}
public function add_move(Move $move){
if($this->check_if_move_is_available($move->from,$move->to)){
$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;
}
}
@ -100,8 +86,8 @@ class Move{
private static $from2to0 = 4;
private static $from2to1 = 5;
private $from;
private $to;
public $from;
public $to;
private $value;
@ -110,7 +96,7 @@ class Move{
}
private function set_value($value){
$this->value = $value();
$this->value = $value;
switch ($value){
case Move::$from0to1:
$this->from = 0;
@ -166,8 +152,37 @@ class Move{
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;
break;
}
return $move;
}
}
class Steps{
/*
*Memorise the differents towers configurations
@ -177,7 +192,7 @@ class Steps{
$this->steps = array();
}
public function add_step(Towers $tower){
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();

View File

@ -6,6 +6,8 @@ include("class.towers.php");
$tower = new Tower($discCount);
echo $tower;
/*
resolveHanoi($tower);
function resolveHanoi($tower){
@ -14,10 +16,11 @@ function resolveHanoi($tower){
$availablesMoves = $tower->list_moves_availables();
//don't take moves that will generate an ever used tower configuration
$useful_moves = array();
$useful_moves = array();
foreach($availablesMoves as $move){
}
}
*/