Exemple #1
0
 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;
 }
Exemple #2
0
 /**
  * Transform a game to standart Forsyth Edwards Notation
  * http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
  */
 public function export(Game $game)
 {
     $board = $game->getBoard();
     $emptySquare = 0;
     $forsythe = '';
     for ($y = 8; $y > 0; $y--) {
         for ($x = 1; $x < 9; $x++) {
             if ($piece = $board->getPieceByPos($x, $y)) {
                 if ($emptySquare) {
                     $forsythe .= $emptySquare;
                     $emptySquare = 0;
                 }
                 $forsythe .= $this->pieceToForsythe($piece);
             } else {
                 ++$emptySquare;
             }
         }
         if ($emptySquare) {
             $forsythe .= $emptySquare;
             $emptySquare = 0;
         }
         $forsythe .= '/';
     }
     $forsythe = trim($forsythe, '/');
     // b ou w to indicate turn
     $forsythe .= ' ';
     $forsythe .= $game->getTurns() % 2 ? 'b' : 'w';
     // possibles castles
     $forsythe .= ' ';
     $hasCastle = false;
     $analyser = new Analyser($board);
     foreach ($game->getPlayers() as $player) {
         if ($analyser->canCastleKingside($player)) {
             $hasCastle = true;
             $forsythe .= $player->isWhite() ? 'K' : 'k';
         }
         if ($analyser->canCastleQueenside($player)) {
             $hasCastle = true;
             $forsythe .= $player->isWhite() ? 'Q' : 'q';
         }
     }
     if (!$hasCastle) {
         $forsythe .= '-';
     }
     return $forsythe;
 }
 protected function computePossibleMoves($data, $turn = 0)
 {
     $game = $this->createGame($data);
     $game->setTurns($turn);
     $game->setStatus(Game::STARTED);
     $analyser = new Analyser($game->getBoard());
     $moveTree = $analyser->getPlayerPossibleMoves($game->getTurnPlayer());
     return $moveTree;
 }
Exemple #4
0
 /**
  * Transform a game to standard Forsyth Edwards Notation
  * http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
  */
 public static function export(Game $game, $positionOnly = false)
 {
     static $reverseClasses = array('Pawn' => 'p', 'Rook' => 'r', 'Knight' => 'n', 'Bishop' => 'b', 'Queen' => 'q', 'King' => 'k');
     $board = $game->getBoard();
     $emptySquare = 0;
     $forsyth = '';
     for ($y = 8; $y > 0; $y--) {
         for ($x = 1; $x < 9; $x++) {
             if ($piece = $board->getPieceByPosNoCheck($x, $y)) {
                 if ($emptySquare) {
                     $forsyth .= $emptySquare;
                     $emptySquare = 0;
                 }
                 $notation = $reverseClasses[$piece->getClass()];
                 if ('white' === $piece->getColor()) {
                     $notation = strtoupper($notation);
                 }
                 $forsyth .= $notation;
             } else {
                 ++$emptySquare;
             }
         }
         if ($emptySquare) {
             $forsyth .= $emptySquare;
             $emptySquare = 0;
         }
         $forsyth .= '/';
     }
     $forsyth = trim($forsyth, '/');
     if ($positionOnly) {
         return $forsyth;
     }
     // b ou w to indicate turn
     $forsyth .= ' ';
     $forsyth .= substr($game->getTurnColor(), 0, 1);
     // possibles castles
     $forsyth .= ' ';
     $hasCastle = false;
     $analyser = new Analyser($board);
     foreach ($game->getPlayers() as $player) {
         if ($analyser->canCastleKingSide($player)) {
             $hasCastle = true;
             $forsyth .= $player->isWhite() ? 'K' : 'k';
         }
         if ($analyser->canCastleQueenSide($player)) {
             $hasCastle = true;
             $forsyth .= $player->isWhite() ? 'Q' : 'q';
         }
     }
     if (!$hasCastle) {
         $forsyth .= '-';
     }
     // en passant
     $enPassant = '-';
     foreach (PieceFilter::filterClass(PieceFilter::filterAlive($game->getPieces()), 'Pawn') as $piece) {
         if ($piece->getFirstMove() === $game->getTurns() - 1) {
             $color = $piece->getPlayer()->getColor();
             $y = $piece->getY();
             if ($color === 'white' && 4 === $y || $color === 'black' && 5 === $y) {
                 $enPassant = Board::posToKey($piece->getX(), 'white' === $color ? $y - 1 : $y + 1);
                 break;
             }
         }
     }
     $forsyth .= ' ' . $enPassant;
     // Halfmove clock: This is the number of halfmoves since the last pawn advance or capture.
     // This is used to determine if a draw can be claimed under the fifty-move rule.
     $forsyth .= ' ' . $game->getHalfmoveClock();
     // Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move.
     $forsyth .= ' ' . $game->getFullMoveNumber();
     return $forsyth;
 }
 public function showAction($hash)
 {
     $player = $this->findPlayer($hash);
     $game = $player->getGame();
     if (!$game->getIsStarted()) {
         return $this->render('LichessBundle:Player:waitNext', array('player' => $player));
     }
     if (!$player->getOpponent()->getIsAi() && !$game->getIsFinished()) {
         $synchronizer = new Synchronizer();
         $synchronizer->update($player);
         $this->container->getLichessPersistenceService()->save($game);
     }
     $analyser = new Analyser($game->getBoard());
     $isKingAttacked = $analyser->isKingAttacked($game->getTurnPlayer());
     if ($isKingAttacked) {
         $checkSquareKey = $game->getTurnPlayer()->getKing()->getSquareKey();
     } else {
         $checkSquareKey = null;
     }
     $this->container->getLichessSocketService()->write($player, array());
     return $this->render('LichessBundle:Player:show', array('player' => $player, 'checkSquareKey' => $checkSquareKey, 'possibleMoves' => $player->isMyTurn() && !$game->getIsFinished() ? $analyser->getPlayerPossibleMoves($player, $isKingAttacked) : null));
 }