示例#1
0
 /**
  * @param MoveInterface $move
  *
  * @return string
  */
 public static function toNotation(MoveInterface $move)
 {
     if (null === $move->getPiece()) {
         return '';
     }
     $notation = '';
     $piece = $move->getPiece();
     $toLabel = strtolower($move->getToLabel());
     $isPawn = $piece->getType() === PieceInterface::TYPE_PAWN;
     $pieceLetter = PieceDecorator::toLetter($piece);
     if (null !== ($capture = $move->getCapture())) {
         if ($isPawn) {
             $column = BoardHelper::getColumnFromPosition($move->getFrom());
             $pieceLetter = BoardHelper::columnNumberToLetter($column);
         }
         $notation .= sprintf('%sx%s', $pieceLetter, $toLabel);
         if ($isPawn) {
             if ($move->getType() === MoveInterface::TYPE_CAPTURE_EN_PASSANT) {
                 $notation .= 'e.p.';
             }
         }
         return $notation;
     }
     if ($isPawn) {
         return $toLabel;
     }
     return sprintf('%s%s', $pieceLetter, $toLabel);
 }
示例#2
0
 /**
  * @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;
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function controlsCenter(MoveInterface $move, BoardInterface $board)
 {
     if ($move->getPiece()->getType() === PieceInterface::TYPE_PAWN) {
         if (in_array($move->getTo(), [SquareInterface::POSITION_D4, SquareInterface::POSITION_D5, SquareInterface::POSITION_E4, SquareInterface::POSITION_E5])) {
             return true;
         }
     }
     return false;
 }
示例#4
0
 /**
  * @param MoveInterface $move
  * @param GameInterface $game
  *
  * @throws InvalidMoveException
  */
 private static function validate(MoveInterface $move, GameInterface $game)
 {
     if ($game->hasFinished()) {
         throw new InvalidMoveException('Game has already finished');
     }
     if (null === ($piece = $move->getPiece())) {
         if (null === ($piece = $game->getBoard()->getSquare($move->getFrom())->getPiece())) {
             var_export($game->getBoard()->getSquaresAsString());
             throw new InvalidMoveException(sprintf('There must be a piece to move on that position: %s', $move->getFromLabel()));
         }
     }
     if ($game->getCurrentColor() !== $piece->getColor()) {
         throw new InvalidMoveException(sprintf('You can only move your own pieces: the %s on %s belongs to %s', $piece->getTypeLabel(), $move->getFromLabel(), $game->getOpposingPlayer()->getColor()));
     }
 }
示例#5
0
 /**
  * @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;
 }