Ejemplo n.º 1
0
 public function setPieceFromArray(array $piece = null)
 {
     if ($piece === null) {
         $this->piece = null;
     } else {
         $this->piece = PieceFactory::create($piece['type'], $piece['color'], $piece['id']);
     }
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public static function create($position, array $piece = null)
 {
     if ($piece !== null) {
         $piece = PieceFactory::create($piece['type'], $piece['color'], array_key_exists('id', $piece) ? $piece['id'] : null);
     }
     $square = new Square($position, $piece);
     return $square;
 }
Ejemplo n.º 3
0
 /**
  * @param string $ascii
  *
  * @return PieceInterface
  */
 public static function fromAscii($ascii)
 {
     foreach (self::$asciiPieces as $color => $types) {
         foreach ($types as $type => $asciiType) {
             if ($asciiType === $ascii) {
                 return PieceFactory::create($type, $color);
             }
         }
     }
     throw new \InvalidArgumentException(sprintf('Unknown ASCII piece to convert: %s', $ascii));
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
0
 /**
  * @param MoveInterface $move
  * @param GameInterface $game
  */
 public static function apply(MoveInterface $move, GameInterface $game)
 {
     self::enrich($move, $game);
     $from = $move->getFrom();
     $fromSquare = $game->getBoard()->getSquare($from);
     $fromPiece = $fromSquare->getPiece();
     $to = $move->getTo();
     $toSquare = $game->getBoard()->getSquare($to);
     $toPiece = $toSquare->getPiece();
     $lastMove = $game->getLastMove(false);
     switch ($move->getType()) {
         case MoveInterface::TYPE_NORMAL:
             $fromSquare->setPiece(null);
             $toSquare->setPiece($fromPiece);
             break;
         case MoveInterface::TYPE_CASTLE_KINGSIDE:
         case MoveInterface::TYPE_CASTLE_QUEENSIDE:
             $fromSquare->setPiece(null);
             $toSquare->setPiece($fromPiece);
             if ($move->getType() === MoveInterface::TYPE_CASTLE_KINGSIDE) {
                 if ($game->getCurrentColor() === Color::WHITE) {
                     $rookSquarePosition = SquareInterface::POSITION_H1;
                     $rookSquarePositionNew = SquareInterface::POSITION_F1;
                 } else {
                     $rookSquarePosition = SquareInterface::POSITION_H8;
                     $rookSquarePositionNew = SquareInterface::POSITION_F8;
                 }
             } else {
                 if ($game->getCurrentColor() === Color::WHITE) {
                     $rookSquarePosition = SquareInterface::POSITION_A1;
                     $rookSquarePositionNew = SquareInterface::POSITION_C1;
                 } else {
                     $rookSquarePosition = SquareInterface::POSITION_A8;
                     $rookSquarePositionNew = SquareInterface::POSITION_C8;
                 }
             }
             $rookSquare = $game->getBoard()->getSquare($rookSquarePosition);
             $rook = $rookSquare->getPiece();
             $rookSquare->setPiece(null);
             $newRookSquare = $game->getBoard()->getSquare($rookSquarePositionNew);
             $newRookSquare->setPiece($rook);
             break;
         case MoveInterface::TYPE_CAPTURE:
             $capture = new Capture($fromPiece, $toPiece, $to);
             $move->setCapture($capture);
             $fromSquare->setPiece(null);
             $toSquare->setPiece($fromPiece);
             break;
         case MoveInterface::TYPE_CAPTURE_EN_PASSANT:
             $capturedSquare = $game->getBoard()->getSquare($lastMove->getTo());
             $capturedPiece = $game->getBoard()->getSquare($lastMove->getTo())->getPiece();
             $capture = new Capture($fromPiece, $capturedPiece, $capturedSquare->getPosition());
             $game->getCurrentPlayer()->getCaptures()->add($capture);
             $capturedSquare->setPiece(null);
             $fromSquare->setPiece(null);
             $toSquare->setPiece($fromPiece);
             break;
     }
     if ($fromPiece->getType() === PieceInterface::TYPE_PAWN) {
         if ($game->getCurrentColor() === Color::WHITE && BoardHelper::getRowFromPosition($from) === 7 || $game->getCurrentColor() === Color::BLACK && BoardHelper::getRowFromPosition($from) === 2) {
             if (!$move->getPromotion()) {
                 if (!$game->getCurrentPlayer()->isHuman()) {
                     // @todo Make something to select the best possible promotion piece (can be different than queen, i.e. if check/mate is possible with knight)
                     $move->setPromotion(PieceInterface::TYPE_QUEEN);
                 } else {
                     throw new \InvalidArgumentException('Can\'t promote the pawn: no type set to promote into');
                 }
             }
             $piece = PieceFactory::create($move->getPromotion(), $game->getCurrentColor());
             $toSquare->setPiece($piece);
         }
     }
 }
Ejemplo n.º 6
0
 /**
  * @param PersistableGameStateInterface $persistableGameState
  *
  * @return GameState
  */
 protected function createFromPersistableState(PersistableGameStateInterface $persistableGameState)
 {
     $gameState = new GameState();
     if ($pieceData = $persistableGameState->getPiece()) {
         $piece = PieceFactory::create($pieceData['type'], $pieceData['color'], array_key_exists('id', $pieceData) ? $pieceData['id'] : null);
         $gameState->setPiece($piece);
         $gameState->setColor($persistableGameState->getColor());
         $gameState->setFrom($persistableGameState->getFrom());
         $gameState->setTo($persistableGameState->getTo());
         $gameState->setMoveType($persistableGameState->getMoveType());
         if ($captureData = $persistableGameState->getCapture()) {
             $capturingPiece = PieceFactory::create($captureData['capturing_type'], $captureData['capturing_color'], $captureData['capturing_id']);
             $capturedPiece = PieceFactory::create($captureData['captured_type'], $captureData['captured_color'], $captureData['captured_id']);
             $capture = new Capture($capturingPiece, $capturedPiece, $captureData['position']);
             $gameState->setCapture($capture);
         }
     }
     return $gameState;
 }
Ejemplo n.º 7
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);
 }