public function createPieces(Game $game) { $pieces = array(); $player = $game->getPlayer('white'); // Bishop on black square $file = 2 * mt_rand(1, 4) - 1; $pieces[$file] = $this->createPiece('Bishop', $file, 1); // Bishop on white square $file = 2 * mt_rand(1, 4); $pieces[$file] = $this->createPiece('Bishop', $file, 1); // Queen and Knights foreach (array(6 => 'Queen', 5 => 'Knight', 4 => 'Knight') as $rand => $class) { $file = $this->getEmptyFile($pieces, mt_rand(1, $rand)); $pieces[$file] = $this->createPiece($class, $file, 1); } // Rooks and King foreach (array('Rook', 'King', 'Rook') as $class) { $file = $this->getEmptyFile($pieces, 1); $pieces[$file] = $this->createPiece($class, $file, 1); } // Pawns for ($it = 1; $it <= 8; $it++) { $pieces[] = $this->createPiece('Pawn', $it, 2); } $pieces = array_values($pieces); $player->setPieces($pieces); $player->getOpponent()->setPieces($this->mirrorPieces($pieces)); $game->ensureDependencies(); $game->setInitialFen(Forsyth::export($game)); }
protected function fewMaterial(Game $game) { $white = $game->getPlayer('white'); $black = $game->getPlayer('black'); $whiteNbPieces = $white->getNbAlivePieces(); $blackNbPieces = $black->getNbAlivePieces(); if (2 < $whiteNbPieces || 2 < $blackNbPieces) { return false; } $whitePiece = $this->getLastPiece($white); $blackPiece = $this->getLastPiece($black); if ($this->canNotMate($white) && $this->canNotMate($black)) { return true; } return false; }
public function createPiecesMinimal(Game $game) { $pieces = array(); $player = $game->getPlayer('white'); $pieces[] = new Piece(1, 2, 'Pawn'); $player->setPieces($pieces); $player->getOpponent()->setPieces($this->mirrorPieces($pieces)); $game->setInitialFen(null); }
protected function getPgnResult(Game $game) { if ($game->getIsFinished()) { if ($game->getPlayer('white')->getIsWinner()) { return '1-0'; } elseif ($game->getPlayer('black')->getIsWinner()) { return '0-1'; } return '1/2-1/2'; } return '*'; }
protected function win(Game $game, $color = 'white') { $game->setStatus(Game::MATE); $game->setWinner($game->getPlayer($color)); $this->finisher->finish($game); }
/** * Create a game from a visual block notation like: r bqkb r ppp ppp p n n p B P N PPPP PPP RNBQK R */ public function createGameFromVisualBlock($data) { $data = $this->fixVisualBlock($data); $game = new Game(); $players = array(); foreach (array('white', 'black') as $color) { $game->addPlayer(new Player($color)); } $game->setCreator($game->getPlayer('white')); foreach (explode("\n", $data) as $_y => $line) { $y = 8 - $_y; for ($x = 1; $x < 9; $x++) { $byte = $line[$x - 1]; if (' ' === $byte) { continue; } $color = ctype_lower($byte) ? 'black' : 'white'; $player = $game->getPlayer($color); switch (strtolower($byte)) { 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; } $player->addPiece(new Piece($x, $y, $class)); } } return $game; }
/** * @depends testGameCreationStandard */ public function testPlayerQueens(Document\Game $game) { $player = $game->getPlayer('white'); $queens = $player->getPiecesByClass('Queen'); $this->assertTrue($queens[0] instanceof Document\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()); }
public function rematchAccept(Player $player, Game $nextGame, array $messages) { // tell players to move to next game $this->post('rematch-accept/' . $this->gameColorUrl($player) . '/' . $nextGame->getId(), array("whiteRedirect" => $this->url('lichess_player', array('id' => $nextGame->getPlayer('black')->getFullId())), "blackRedirect" => $this->url('lichess_player', array('id' => $nextGame->getPlayer('white')->getFullId())), "entry" => $this->encodeLobbyEntry($nextGame), "messages" => $this->encodeMessages($messages))); }
protected function updateElo(Game $game) { // Game can be aborted if (!$game->getIsFinished()) { return; } if (!$game->getIsRated()) { return; } // Don't rate games with less than 2 moves if ($game->getTurns() < 2) { return; } $white = $game->getPlayer('white'); $black = $game->getPlayer('black'); $whiteUser = $white->getUser(); $blackUser = $black->getUser(); // Don't rate games when one ore more player is not registered if (!$whiteUser || !$blackUser) { return; } if ($winner = $game->getWinner()) { $win = $winner->isWhite() ? -1 : 1; } else { $win = 0; } list($whiteElo, $blackElo) = $this->calculator->calculate($white->getElo(), $black->getElo(), $win); $white->setEloDiff($whiteEloDiff = $whiteElo - $white->getElo()); $black->setEloDiff($blackEloDiff = $blackElo - $black->getElo()); $this->eloUpdater->updateElo($whiteUser, $whiteUser->getElo() + $whiteEloDiff, $game); $this->eloUpdater->updateElo($blackUser, $blackUser->getElo() + $blackEloDiff, $game); $this->logger->notice($game, sprintf('Elo exchanged: %s', $whiteEloDiff)); }