/** * @return Game */ public function createGame() { $game = new Game(); $game->setPlayers(array('white' => $this->createPlayer($game, 'white'), 'black' => $this->createPlayer($game, 'black'))); $game->setCreator($game->getPlayer('white')); return $game; }
public function move(Game $game, $level) { $analyser = new Analyser($game->getBoard()); $moveTree = $analyser->getPlayerPossibleMoves($game->getTurnPlayer()); // choose random piece do { $from = array_rand($moveTree); } while (empty($moveTree[$from])); // choose random move $to = $moveTree[$from][array_rand($moveTree[$from])]; return $from . ' ' . $to; }
public function getIsMyTurn() { return $this->game->getTurns() % 2 xor 'white' === $this->color; }
/** * @depends testGameCreation */ public function testPlayerQueens(Entities\Game $game) { $player = $game->getPlayer('white'); $queens = $player->getPiecesByClass('Queen'); $this->assertTrue($queens[0] instanceof Entities\Piece\Queen); $this->assertEquals(1, $queens[0]->getY()); $this->assertEquals(1, count($queens)); $this->assertSame($player, $queens[0]->getPlayer()); $this->assertTrue($queens[0]->isClass('Queen')); $this->assertFalse($queens[0]->getIsDead()); }
/** * @depends testGameCreation */ public function testFilterNotClass(Entities\Game $game) { $piece1 = $game->getBoard()->getPieceByPos(1, 1); $piece2 = $game->getBoard()->getPieceByPos(2, 1); $this->assertSame(array($piece2), PieceFilter::filterNotClass(array($piece1, $piece2), 'Rook')); }
public function setPlayers() { $game = new Game(); $game->setPlayers($this->createPlayerStubs()); $this->assertEquals(2, count($game->getPlayers())); }
public function diffToMove(Game $game, $forsythe) { $moves = array('from' => array(), 'to' => array()); $x = 1; $y = 8; $board = $game->getBoard(); $forsythe = str_replace('/', '', preg_replace('#\\s*([\\w\\d/]+)\\s.+#i', '$1', $forsythe)); for ($itForsythe = 0, $forsytheLen = strlen($forsythe); $itForsythe < $forsytheLen; $itForsythe++) { $letter = $forsythe[$itForsythe]; $key = Board::posToKey($x, $y); if (is_numeric($letter)) { for ($x = $x, $max = $x + intval($letter); $x < $max; $x++) { $_key = Board::posToKey($x, $y); if (!$board->getSquareByKey($_key)->isEmpty()) { $moves['from'][] = $_key; } } } else { $color = ctype_lower($letter) ? 'black' : 'white'; switch (strtolower($letter)) { case 'p': $class = 'Pawn'; break; case 'r': $class = 'Rook'; break; case 'n': $class = 'Knight'; break; case 'b': $class = 'Bishop'; break; case 'q': $class = 'Queen'; break; case 'k': $class = 'King'; break; } if ($piece = $board->getPieceByKey($key)) { if ($class != $piece->getClass() || $color !== $piece->getColor()) { $moves['to'][] = $key; } } else { $moves['to'][] = $key; } ++$x; } if ($x > 8) { $x = 1; --$y; } } if (empty($moves['from'])) { return null; } // two pieces moved: it's a castle if (2 === count($moves['from'])) { if ($board->getPieceByKey($moves['from'][0])->isClass('King')) { $from = $moves['from'][0]; } else { $from = $moves['from'][1]; } if (in_array($board->getSquareByKey($moves['to'][0])->getX(), array(3, 7))) { $to = $moves['to'][0]; } else { $to = $moves['to'][1]; } } else { $from = $moves['from'][0]; $to = $moves['to'][0]; } return $from . ' ' . $to; }