Пример #1
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);
                         }
                     }
                 }
             }
         }
     }
 }
Пример #2
0
 /**
  * {@inheritdoc}
  */
 public function configureWalker(BoardWalker $walker, GameInterface $game)
 {
     $pawnMoves = $game->getMovesByPiece($game->getBoard()->getSquare($walker->getStartingPosition())->getPiece()->getId());
     if (empty($pawnMoves)) {
         if ($game->getCurrentColor() === Color::WHITE && $walker->getRow() == 2 || $game->getCurrentColor() === Color::BLACK && $walker->getRow() == 7) {
             $newPosition = $walker->peek(BoardWalker::DIRECTION_FORWARD, 1);
             // allowed to make two steps forward
             if ($newPosition && !$game->getBoard()->getSquare($newPosition)->getPiece()) {
                 $newPosition = $walker->peek(BoardWalker::DIRECTION_FORWARD, 2);
                 if ($newPosition && !$game->getBoard()->getSquare($newPosition)->getPiece()) {
                     // temporary check because walker does not seem to consider obstructing pieces of own color
                     $walker->jump(BoardWalker::DIRECTION_FORWARD, 2)->restart();
                 }
             }
         }
     }
     $walker->forward(1, false)->restart();
     $forwardLeftCapture = $walker->peek(BoardWalker::DIRECTION_FORWARDLEFT, 1, null, false, true);
     $forwardRightCapture = $walker->peek(BoardWalker::DIRECTION_FORWARDRIGHT, 1, null, false, true);
     if ($forwardLeftCapture !== null) {
         $walker->forwardLeft(1)->restart();
     }
     if ($forwardRightCapture !== null) {
         $walker->forwardRight(1)->restart();
     }
     $this->walkEnPassant($walker, $game->getLastMove(false));
 }