Esempio n. 1
0
 /**
  * Add castling moves if available
  *
  * @return array the squares where the king can go
  **/
 protected function addCastlingSquares(King $king, array $squares)
 {
     $player = $king->getPlayer();
     $rooks = PieceFilter::filterNotMoved(PieceFilter::filterClass(PieceFilter::filterAlive($player->getPieces()), 'Rook'));
     if (empty($rooks)) {
         return $squares;
     }
     $opponentControlledKeys = $this->getPlayerControlledKeys($player->getOpponent(), true);
     foreach ($rooks as $rook) {
         $kingX = $king->getX();
         $kingY = $king->getY();
         $dx = $kingX > $rook->getX() ? -1 : 1;
         $possible = true;
         foreach (array($kingX + $dx, $kingX + 2 * $dx) as $_x) {
             $key = Board::postoKey($_x, $kingY);
             if ($this->board->hasPieceByKey($key) || in_array($key, $opponentControlledKeys)) {
                 $possible = false;
                 break;
             }
         }
         if ($possible) {
             if (-1 === $dx && $this->board->hasPieceByKey(Board::postoKey($kingX - 3, $kingY))) {
             } else {
                 $squares[] = $this->board->getSquareByKey($key);
             }
         }
     }
     return $squares;
 }