import zigasou'scode

This commit is contained in:
Gnieark 2016-07-13 07:05:42 +02:00
parent 294ab30e48
commit 6286837f64
5 changed files with 120 additions and 38 deletions

View File

@ -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;
}

View File

@ -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":

View File

@ -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);
}
}

View File

@ -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) { }
}
}

46
src/arenas/tron/trail.php Normal file
View File

@ -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;
}
}