Esempio n. 1
0
 /**
  * {@inheritdoc}
  */
 public function possibleCapturesFrom($position, BoardInterface $board)
 {
     $moves = [];
     $fromPiece = $board->getSquare($position)->getPiece();
     foreach ($this->possibleMovesFrom($position, $board) as $possibleMove) {
         $toPiece = $board->getSquare($possibleMove->getTo())->getPiece();
         if ($toPiece !== null && $toPiece->getColor() !== $fromPiece->getColor()) {
             $moves[] = $possibleMove;
         }
     }
     return $moves;
 }
Esempio n. 2
0
 /**
  * @param int      $direction        The direction to step into
  * @param int      $jumps            The number of jumps to make in this direction
  * @param int|null $startingPosition
  * @param bool     $ignoreCapture
  *
  * @return int|null The expected position, or null if the move is not possible
  *
  * @throws \InvalidArgumentException
  */
 public function peek($direction, $jumps = 1, $startingPosition = null, $ignoreCapture = false, $requireAttack = false)
 {
     $newPosition = null;
     if ($startingPosition !== null) {
         $column = BoardHelper::getColumnFromPosition($startingPosition);
         $row = BoardHelper::getRowFromPosition($startingPosition);
     } else {
         $column = $this->getColumn();
         $row = $this->getRow();
     }
     switch ($direction) {
         case self::DIRECTION_FORWARD:
             if ($this->color === Color::WHITE) {
                 $newRow = $this->formatNumber($row + $jumps);
             } else {
                 $newRow = $this->formatNumber($row - $jumps);
             }
             if ($newRow !== null) {
                 $newPosition = intval($column . $newRow);
             }
             break;
         case self::DIRECTION_FORWARDRIGHT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column + $jumps);
                 $newRow = $this->formatNumber($row + $jumps);
             } else {
                 $newColumn = $this->formatNumber($column - $jumps);
                 $newRow = $this->formatNumber($row - $jumps);
             }
             if ($newColumn !== null && $newRow !== null) {
                 $newPosition = intval($newColumn . $newRow);
             }
             break;
         case self::DIRECTION_RIGHT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column + $jumps);
             } else {
                 $newColumn = $this->formatNumber($column - $jumps);
             }
             if ($newColumn !== null) {
                 $newPosition = intval($newColumn . $row);
             }
             break;
         case self::DIRECTION_BACKWARDRIGHT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column + $jumps);
                 $newRow = $this->formatNumber($row - $jumps);
             } else {
                 $newColumn = $this->formatNumber($column - $jumps);
                 $newRow = $this->formatNumber($row + $jumps);
             }
             if ($newColumn !== null && $newRow !== null) {
                 $newPosition = intval($newColumn . $newRow);
             }
             break;
         case self::DIRECTION_BACKWARD:
             if ($this->color === Color::WHITE) {
                 $newRow = $this->formatNumber($row - $jumps);
             } else {
                 $newRow = $this->formatNumber($row + $jumps);
             }
             if ($newRow !== null) {
                 $newPosition = intval($column . $newRow);
             }
             break;
         case self::DIRECTION_BACKWARDLEFT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column - $jumps);
                 $newRow = $this->formatNumber($row - $jumps);
             } else {
                 $newColumn = $this->formatNumber($column + $jumps);
                 $newRow = $this->formatNumber($row + $jumps);
             }
             if ($newColumn !== null && $newRow !== null) {
                 $newPosition = intval($newColumn . $newRow);
             }
             break;
         case self::DIRECTION_LEFT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column - $jumps);
             } else {
                 $newColumn = $this->formatNumber($column + $jumps);
             }
             if ($newColumn !== null) {
                 $newPosition = intval($newColumn . $row);
             }
             break;
         case self::DIRECTION_FORWARDLEFT:
             if ($this->color === Color::WHITE) {
                 $newColumn = $this->formatNumber($column - $jumps);
                 $newRow = $this->formatNumber($row + $jumps);
             } else {
                 $newColumn = $this->formatNumber($column + $jumps);
                 $newRow = $this->formatNumber($row - $jumps);
             }
             if ($newColumn !== null && $newRow !== null) {
                 $newPosition = intval($newColumn . $newRow);
             }
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Unknown direction to calculate: %s', $direction));
     }
     if ($ignoreCapture !== true && $newPosition !== null) {
         $toPiece = $this->board->getSquare($newPosition)->getPiece();
         if (null !== $toPiece) {
             if ($toPiece->getColor() === $this->getColor()) {
                 // own piece, can't capture
                 return null;
             }
         } elseif ($requireAttack === true) {
             return null;
         }
     }
     return $newPosition;
 }