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));
 }
Example #2
0
 /**
  * @dataProvider fenProvider
  */
 public function testImportExport($fen)
 {
     $game = $this->createGame();
     Forsyth::import($game, $fen);
     $simplifyFen = function ($fen) {
         return substr($fen, 0, strpos($fen, ' '));
     };
     $this->assertEquals($simplifyFen($fen), $simplifyFen(Forsyth::export($game)));
 }
Example #3
0
 public function move(Game $game, $level)
 {
     $oldForsyth = Forsyth::export($game);
     // hack to have chess960 working with Crafty
     if (!$game->isStandardVariant()) {
         $oldForsyth = $this->removeCastlingInfos($oldForsyth);
     }
     $newForsyth = $this->getNewForsyth($oldForsyth, $level);
     $move = Forsyth::diffToMove($game, $newForsyth);
     return $move;
 }
Example #4
0
 /**
  * Creates a return game for the given player,
  * reverting players colors
  *
  * @param  Player the player who creates the return game
  * @return Player the new player on the new game
  **/
 public function createReturnGame(Player $player)
 {
     $game = $player->getGame();
     $variant = $game->getVariant();
     $nextGame = $this->createGame($variant);
     if (Game::VARIANT_960 == $game->getVariant()) {
         Forsyth::import($nextGame, $game->getInitialFen());
         $nextGame->setInitialFen($game->getInitialFen());
     }
     $nextPlayer = $nextGame->getPlayer($player->getOpponent()->getColor());
     $nextGame->setCreator($nextPlayer);
     if ($game->hasClock()) {
         $nextGame->setClock(clone $game->getClock());
     }
     $nextGame->setIsRated($game->getIsRated());
     $nextGame->getPlayer('white')->setUser($game->getPlayer('black')->getUser());
     $nextGame->getPlayer('black')->setUser($game->getPlayer('white')->getUser());
     $game->setNext($nextGame);
     return $nextPlayer;
 }
Example #5
0
 public function renderGameFen(Game $game, User $user = null)
 {
     $fenString = Forsyth::export($game, true);
     $player = $game->getPlayerByUserOrCreator($user);
     $authUser = $this->container->get('security.context')->getToken()->getUser();
     if ($authUser instanceof User && ($authPlayer = $game->getPlayerByUser($authUser))) {
         $gameUrl = $this->getRouterGenerator()->generate('lichess_player', array('id' => $authPlayer->getFullId()));
     } else {
         $gameUrl = $this->getRouterGenerator()->generate('lichess_game', array('id' => $game->getId(), 'color' => $player->getColor()));
     }
     return sprintf('<a href="%s" title="%s" class="mini_board parse_fen" data-color="%s" data-fen="%s"></a>', $gameUrl, $this->getTranslator()->trans('View in full size'), $player->getColor(), $fenString);
 }