import zigasou'scode
parent
294ab30e48
commit
6286837f64
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
class Tail{
|
||||
|
||||
public $tail;
|
||||
|
||||
|
||||
public function __toString(){
|
||||
$str = "";
|
||||
foreach(Tail::$tail as $coord){
|
||||
$str .= "[".$coord."],";
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
public function __make(Coords $InitialCoords){
|
||||
$this->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);
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once '../Direction.php';
|
||||
require_once '../Coords.php';
|
||||
require_once '../TronPlayer.php';
|
||||
|
||||
class TronPlayerTest extends TestCase {
|
||||
public function validPlayer(Direction $direction) {
|
||||
return new TronPlayer(
|
||||
'http://127.0.0.1',
|
||||
'test',
|
||||
new Coords(0, 0),
|
||||
$direction
|
||||
);
|
||||
}
|
||||
|
||||
public function testTronPlayerCreation() {
|
||||
$this->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) { }
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
class AlreadyBeenAddedException extends LogicException { }
|
||||
|
||||
class Trail {
|
||||
private $trail;
|
||||
|
||||
public function __construct() {
|
||||
$this->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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue