Пример #1
0
 /**
  * @param BoardWalker   $walker
  * @param MoveInterface $lastMove
  */
 private function walkEnPassant(BoardWalker $walker, MoveInterface $lastMove = null)
 {
     $lastEnemyPiece = $lastMove ? $walker->getBoard()->getSquare($lastMove->getTo())->getPiece() : null;
     if ($lastMove === null || $lastEnemyPiece === null || $lastEnemyPiece->getType() !== PieceInterface::TYPE_PAWN) {
         return;
     }
     $leftToPosition = $walker->peek(BoardWalker::DIRECTION_LEFT, 1, null, true, false);
     $rightToPosition = $walker->peek(BoardWalker::DIRECTION_RIGHT, 1, null, true, false);
     if ($leftToPosition !== null) {
         if (abs(BoardHelper::getRowFromPosition($lastMove->getFrom()) - BoardHelper::getRowFromPosition($leftToPosition)) !== 2) {
             return;
         }
         if ($lastMove->getTo() === $leftToPosition) {
             // en passant left
             $walker->forwardLeft(1, false)->restart();
         }
     }
     if ($rightToPosition !== null) {
         if (abs(BoardHelper::getRowFromPosition($lastMove->getFrom()) - BoardHelper::getRowFromPosition($rightToPosition)) !== 2) {
             return;
         }
         if ($lastMove->getTo() === $rightToPosition) {
             // en passant left
             $walker->forwardRight(1, false)->restart();
         }
     }
 }
Пример #2
0
 public function testGetRow()
 {
     $this->boardWalker->start(SquareInterface::POSITION_D5);
     $this->assertEquals(5, $this->boardWalker->getRow());
     $this->boardWalker->forward(1);
     $this->assertEquals(6, $this->boardWalker->getRow());
     $this->boardWalker->start(SquareInterface::POSITION_D5)->left(1);
     $this->assertEquals(5, $this->boardWalker->getRow());
     $this->boardWalker->start(SquareInterface::POSITION_D5)->backward(1);
     $this->assertEquals(4, $this->boardWalker->getRow());
     $this->boardWalker->start(SquareInterface::POSITION_D5)->right(1);
     $this->assertEquals(5, $this->boardWalker->getRow());
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function configureWalker(BoardWalker $walker, GameInterface $game)
 {
     $walker->omnidirectional(1);
     $kingPosition = BoardHelper::getKingPosition($game->getBoard(), $game->getCurrentColor());
     $kingColumn = BoardHelper::getColumnFromPosition($kingPosition);
     $kingRow = BoardHelper::getRowFromPosition($kingPosition);
     $board = $game->getBoard();
     $king = $board->getSquare($kingPosition)->getPiece();
     $track = $kingPosition === SquareInterface::POSITION_E1;
     // check for possible castle move
     if (empty($game->getMovesByPiece($king->getId()))) {
         // see if one of the rooks did not make any moves
         $rookCriteria = ['piece_type' => PieceInterface::TYPE_ROOK, 'piece_color' => $game->getCurrentColor()];
         foreach ($board->getPiecesBy($rookCriteria) as $rookPosition => $rook) {
             if (empty($game->getMovesByPiece($rook->getId()))) {
                 // see if there are no pieces between the king and this rook
                 $rookColumn = BoardHelper::getColumnFromPosition($rookPosition);
                 $columnDiff = abs($kingColumn - $rookColumn) - 1;
                 $track = $track === true && $columnDiff === 3;
                 if ($columnDiff > 1) {
                     $skip = false;
                     for ($x = 1; $x <= $columnDiff; $x++) {
                         if ($rookColumn > $kingColumn) {
                             $cursor = $rookColumn - $x . $kingRow;
                         } else {
                             $cursor = $rookColumn + $x . $kingRow;
                         }
                         if (null !== ($piece = $board->getSquare($cursor)->getPiece())) {
                             $skip = true;
                             break;
                         }
                     }
                     if ($skip) {
                         continue;
                     }
                     if ($kingColumn > $rookColumn) {
                         if ($game->getCurrentColor() === Color::WHITE) {
                             $walker->jump(BoardWalker::DIRECTION_LEFT, 2);
                         } else {
                             $walker->jump(BoardWalker::DIRECTION_RIGHT, 2);
                         }
                     } else {
                         if ($game->getCurrentColor() === Color::WHITE) {
                             $walker->jump(BoardWalker::DIRECTION_RIGHT, 2);
                         } else {
                             $walker->jump(BoardWalker::DIRECTION_LEFT, 2);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #4
0
 /**
  * {@inheritdoc}
  */
 public function configureWalker(BoardWalker $walker, GameInterface $game)
 {
     $walker->lshaped();
 }
Пример #5
0
 /**
  * {@inheritdoc}
  */
 public function configureWalker(BoardWalker $walker, GameInterface $game)
 {
     $walker->omnidirectional('*');
 }
Пример #6
0
 /**
  * {@inheritdoc}
  */
 public function configureWalker(BoardWalker $walker, GameInterface $game)
 {
     $walker->horizontal('*');
     $walker->vertical('*');
 }