예제 #1
0
 public function parseGameString($notations, $whitePlayer = null, $blackPlayer = null)
 {
     $notations = trim($notations);
     $whitePlayer = $whitePlayer ?: PlayerFactory::createWhite();
     $blackPlayer = $blackPlayer ?: PlayerFactory::createBlack();
     $board = BoardFactory::create();
     $game = GameFactory::create($whitePlayer, $blackPlayer, $board);
     $movePairs = preg_split('/(\\d+)\\. /', $notations);
     $movePairs = array_filter(array_map('trim', $movePairs));
     $parsed = [];
     foreach ($movePairs as $pairData) {
         list($whiteNotation, $blackNotation) = explode(' ', $pairData);
         if ($whiteNotation !== '0-1') {
             try {
                 $whiteParsed = $this->parse($whiteNotation, $game, Color::WHITE);
                 $game->doMove($this->createMoveFromParsed($whiteParsed));
                 $game->finishTurn($this->moveCalculator);
                 $parsed[] = $whiteParsed;
             } catch (\Exception $e) {
                 throw new \RuntimeException(sprintf('Failed to parse notation for white: %s', $whiteNotation), null, $e);
             }
         }
         if ($blackNotation !== '1-0') {
             try {
                 $blackParsed = $this->parse($blackNotation, $game, Color::BLACK);
                 $game->doMove($this->createMoveFromParsed($blackParsed));
                 $game->finishTurn($this->moveCalculator);
                 $parsed[] = $blackParsed;
             } catch (\Exception $e) {
                 throw new \RuntimeException(sprintf('Failed to parse notation for black: %s', $blackNotation), null, $e);
             }
         }
     }
     return $game;
 }
예제 #2
0
 /**
  * @param PlayerInterface     $whitePlayer
  * @param PlayerInterface     $blackPlayer
  * @param BoardInterface|null $board
  * @param string|null         $id
  *
  * @return Game
  */
 public static function create(PlayerInterface $whitePlayer, PlayerInterface $blackPlayer, BoardInterface $board = null, $id = null)
 {
     if ($board === null) {
         $board = BoardFactory::create();
     }
     $game = new Game($whitePlayer, $blackPlayer, $board, $id);
     $board->setGame($game);
     return $game;
 }
예제 #3
0
 /**
  * @return Game
  */
 private function createGame()
 {
     $whitePlayer = PlayerFactory::create(Color::WHITE, 'White', true);
     $blackPlayer = PlayerFactory::create(Color::BLACK, 'Black', true);
     $board = BoardFactory::create();
     $gameState = $this->createGameState();
     $gameState->setFrom(SquareInterface::POSITION_E2);
     $gameState->setTo(SquareInterface::POSITION_E4);
     $gameState->setColor(Color::WHITE);
     $gameState->setMoveType(MoveInterface::TYPE_NORMAL);
     $gameState->setPiece(PieceFactory::create(PieceInterface::TYPE_PAWN, Color::WHITE));
     $game = new Game($whitePlayer, $blackPlayer, $board);
     $game->addState($gameState);
     return $game;
 }
예제 #4
0
 /**
  * @param PersistableGameInterface $persistableGame
  *
  * @return Game
  */
 protected function createFromPersistable(PersistableGameInterface $persistableGame)
 {
     $whitePlayer = new Player(Color::WHITE, $persistableGame->getWhitePlayerName(), $persistableGame->isWhitePlayerHuman());
     $blackPlayer = new Player(Color::BLACK, $persistableGame->getBlackPlayerName(), $persistableGame->isBlackPlayerHuman());
     $squares = [];
     if (null !== ($lastState = $persistableGame->getLastState())) {
         foreach ($lastState->getSquares() as $squareData) {
             $squares[] = SquareFactory::create($squareData['position'], $squareData['piece']);
         }
     }
     $board = BoardFactory::create($squares);
     $game = new Game($whitePlayer, $blackPlayer, $board, $persistableGame->getUid());
     foreach ($persistableGame->getStates() as $persistableGameState) {
         $game->addState($this->createFromPersistableState($persistableGameState));
     }
     $game->setFinished($persistableGame->hasFinished());
     $game->setFinishedReason($persistableGame->getFinishedReason());
     if ($lastState !== null) {
         $game->setCurrentColor($lastState->getColor() === Color::WHITE ? Color::BLACK : Color::WHITE);
     }
     return $game;
 }
예제 #5
0
 /**
  * @param string $asciiBoard
  *
  * @return Board
  */
 public static function fromAscii($asciiBoard)
 {
     $asciiBoard = trim($asciiBoard);
     $squares = [];
     $rows = array_map('trim', explode("\n", trim($asciiBoard)));
     unset($rows[8]);
     unset($rows[9]);
     foreach ($rows as $x => $row) {
         $rowNumber = 8 - $x;
         $columns = array_map('trim', explode(' ', substr($row, 5)));
         foreach ($columns as $y => $pieceAscii) {
             $columnNumber = $y + 1;
             if ($pieceAscii === '…') {
                 $piece = null;
             } else {
                 $piece = PieceDecorator::fromAscii($pieceAscii);
             }
             $position = intval($columnNumber . $rowNumber);
             $squares[] = new Square($position, $piece);
         }
     }
     $board = BoardFactory::create($squares);
     return $board;
 }
예제 #6
0
 /**
  * @param array|null $pieces
  * @param int        $color
  *
  * @return BoardWalker
  */
 protected function createBoardWalker(array $pieces = null, $color = Color::WHITE)
 {
     $board = BoardFactory::createEmpty();
     foreach ($pieces as $position => $pieceType) {
         $piece = PieceFactory::create($pieceType, $color);
         $board->getSquare($position)->setPiece($piece);
     }
     return new BoardWalker($board, $color);
 }