diff --git a/src/arenas/tron/TronPlayer.php b/src/arenas/tron/TronPlayer.php index 0a992ff..8bee942 100644 --- a/src/arenas/tron/TronPlayer.php +++ b/src/arenas/tron/TronPlayer.php @@ -8,21 +8,22 @@ class TronPlayer{ public $isAlive = true; - public function grow($dir=""){ - $targetCell = $this->getTargetCell($dir); - $this->tail[] = $targetCell; - return $targetCell; + + public function grow(Direction $dir){ + //$targetCell = $this->getTargetCell($dir); + //$this->tail[] = $targetCell; + //return $targetCell; + $this->trail->grow($dir); } public function loose(){ - $this->isAlive = false; - // $this->tail = array(); + $this->tail->empty_tail(); return false; } public function __make($botId, Coords $initialsCoords,$name,$url){ $this->id = $botId; - $this->tail = Tail::make($initialsCoords); + $this->tail = Trail::make($initialsCoords); $this->name = $name; $this->url = $url; } diff --git a/src/arenas/tron/act.php b/src/arenas/tron/act.php index d0baf84..0a32d87 100644 --- a/src/arenas/tron/act.php +++ b/src/arenas/tron/act.php @@ -14,6 +14,8 @@ require_once(__DIR__."/functions.php"); require_once ("TronGame.php"); require_once ("TronPlayer.php"); require_once ("Direction.php"); +require_once ("Trail.php"); +require_once ("Coords.php"); switch ($_POST['act']){ case "initGame": diff --git a/src/arenas/tron/tail.php b/src/arenas/tron/tail.php deleted file mode 100644 index 0f45451..0000000 --- a/src/arenas/tron/tail.php +++ /dev/null @@ -1,31 +0,0 @@ -tail = array($InitialCoords); - } - - public function empty_tail(){ - - } - - public function grow(Direction $dir){ - $last = $this->getLastTailCoord(); - $this->tail[] = $last->addDirection($dir); - } - - public function getLastTailCoord(){ - return end(Tail::$tail); - } -} \ No newline at end of file diff --git a/src/arenas/tron/test/TrailTest.php b/src/arenas/tron/test/TrailTest.php new file mode 100644 index 0000000..fc3d5c0 --- /dev/null +++ b/src/arenas/tron/test/TrailTest.php @@ -0,0 +1,64 @@ +assertInstanceOf( + TronPlayer::class, + $this->validPlayer(Direction::make('x+')) + ); + } + + public function directions() { + return array( + array(Direction::make('x+')), + array(Direction::make('x-')), + array(Direction::make('y+')), + array(Direction::make('y-')), + ); + } + + /** + * @dataProvider directions + * @expectedException OppositeForbiddenException + */ + public function testOppositeForbidden(Direction $direction) { + $player = $this->validPlayer($direction); + $player->changeDirection($direction->opposite()); + } + + public function testAlreadyLost() { + $right = Direction::make('x+'); + $down = Direction::make('y-'); + $left = Direction::make('x-'); + $up = Direction::make('y+'); + + $player = $this->validPlayer($right); + $player->nextMove($right); + $player->nextMove($down); + $player->nextMove($left); + + try { + $player->nextMove($up); + throw new Exception('TronPlayer did not throw AlreadyPlayedException'); + } catch(AlreadyPlayedException $e) { } + + try { + $player->nextMove($up); + throw new Exception('TronPlayer did not throw AlreadyLostException'); + } catch(AlreadyLostException $e) { } + } +} diff --git a/src/arenas/tron/trail.php b/src/arenas/tron/trail.php new file mode 100644 index 0000000..874bb45 --- /dev/null +++ b/src/arenas/tron/trail.php @@ -0,0 +1,46 @@ +trail = new SplStack(); + } + + public function last() { + return $this->trail->top(); + } + + public function add($value) { + if(!$this->trail->isEmpty()) { + if(Trail::kind($this->trail->bottom()) !== Trail::kind($value)) { + throw new TypeError( + 'items added to a trail must be of the same kind' + ); + } + + if($this->contains($value)) { + throw new AlreadyBeenAddedException( + 'value has already been added to the trail' + ); + } + } + + $this->trail->push($value); + } + + public function contains($searchedValue) { + foreach($this->trail as $value) { + if($value == $searchedValue) return TRUE; + } + + return FALSE; + } + + public static function kind($var) { + $type = gettype($var); + if($type == 'object') $type .= ' ' . get_class($var); + return $type; + } +}