示例#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
 protected function getGame($id, $storageType)
 {
     /*
     $adapterRegistry = new StorageAdapterRegistry();
     $adapterRegistry->register(new OrmAdapter($managerRegistry, Game::class, GameState::class), 'orm');
     $storageHelper = new StorageHelper($adapterRegistry, $storageType);
     
     return $storageHelper->loadGame($id);
     */
     return GameFactory::create(PlayerFactory::createWhite(), PlayerFactory::createBlack());
 }
 /**
  * @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 string $asciiBoard
  *
  * @return Game
  */
 protected function createGameFromAsciiBoard($asciiBoard)
 {
     $board = BoardDecorator::fromAscii($asciiBoard);
     $game = GameFactory::create(PlayerFactory::createWhite(), PlayerFactory::createBlack(), $board);
     return $game;
 }