/** * @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(); } } }
/** * @param MoveInterface $move * @param bool $silentFailure */ public function add(MoveInterface $move, $silentFailure = false) { $piece = $move->getPiece(); if ($piece === null) { throw new \InvalidArgumentException('You can only add moves to the registry that has a piece connected to it'); } $moveData = $this->serialize($move->getTo()); $key = $this->getKey($move->getFrom(), $piece->getType(), $piece->getColor()); if (!array_key_exists($key, $this->moves)) { $this->moves[$key] = []; } foreach ($this->moves[$key] as $existingMoveData) { if ($existingMoveData === $moveData) { if ($silentFailure === true) { return; } throw new \InvalidArgumentException(sprintf('This move (%s) has already been registered with this key (%s)', $moveData, $existingMoveData)); } } $this->moves[$key][] = $moveData; $this->fresh = false; }
/** * {@inheritdoc} */ public function mockMove(MoveInterface $move, BoardInterface $board) { $deepCopy = new DeepCopy(); /** @var BoardInterface $futureBoard */ $futureBoard = $deepCopy->copy($board); $fromSquare = $futureBoard->getSquare($move->getFrom()); $fromPiece = $fromSquare->getPiece(); $fromSquare->setPiece(null); $futureBoard->getSquare($move->getTo())->setPiece($fromPiece); return $futureBoard; }
/** * @param MoveInterface $move * @param GameInterface $game * * @return int|null */ private static function determineEnPassantType(MoveInterface $move, GameInterface $game) { if (null !== ($lastMove = $game->getLastMove())) { if ($lastMove->getPiece()->getType() === PieceInterface::TYPE_PAWN) { if (abs(BoardHelper::getColumnFromPosition($lastMove->getTo()) - BoardHelper::getColumnFromPosition($move->getFrom())) === 1 && abs(BoardHelper::getRowFromPosition($lastMove->getTo()) - BoardHelper::getRowFromPosition($lastMove->getFrom())) === 2 && BoardHelper::getRowFromPosition($move->getFrom()) === BoardHelper::getRowFromPosition($lastMove->getTo())) { $toPiece = $game->getBoard()->getSquare($move->getTo())->getPiece(); $rowDiff = abs(BoardHelper::getRowFromPosition($move->getFrom()) - BoardHelper::getRowFromPosition($move->getTo())); $columnDiff = abs(BoardHelper::getColumnFromPosition($move->getFrom()) - BoardHelper::getColumnFromPosition($move->getTo())); if ($rowDiff === 1 && $columnDiff === 1) { if ($toPiece === null) { // en passant capture return MoveInterface::TYPE_CAPTURE_EN_PASSANT; } else { return MoveInterface::TYPE_CAPTURE; } } } } } return null; }
/** * @param MoveInterface $move * * @return GameState */ private function createStateFromMove(MoveInterface $move) { if (!$move->getFrom()) { return null; } $gameState = new GameState(); $gameState->setFrom($move->getFrom()); $gameState->setTo($move->getTo()); $gameState->setPiece($move->getPiece()); $gameState->setCapture($move->getCapture()); $gameState->setMoveType($move->getType()); return $gameState; }