/// <summary> /// Unique rule checking for king castling /// </summary> /// <param name="king"></param> /// <returns>returns king positions when castling is possible</returns> private List<Coords> GetKingCastlingCoords(King king) { List<Coords> castling = new List<Coords>(); if (king.HasMoved || king.Checks.Count > 0) return castling; List<Coords> availableAttacks = GetAllAvailableCoords(king.Side == PieceColor.White ? PieceColor.Black : PieceColor.White, true); King enemyKing = king.Side == PieceColor.White ? Board.BlackKing : Board.WhiteKing; if (!availableAttacks.Contains(king.Coords + 2) && !enemyKing.AvailableCoords(Board.Tiles).Contains(king.Coords + 2) && !availableAttacks.Contains(king.Coords + 1) && Board.Tiles[(int)king.Coords + 4].Piece?.EPiece == EPiece.Rook && !((Rook)Board.Tiles[(int)king.Coords + 4].Piece).HasMoved) castling.Add(king.Coords + 2); if (!availableAttacks.Contains(king.Coords - 2) && !enemyKing.AvailableCoords(Board.Tiles).Contains(king.Coords - 2) && !availableAttacks.Contains(king.Coords - 1) && Board.Tiles[(int)king.Coords - 3].Piece?.EPiece == EPiece.Rook && !((Rook)Board.Tiles[(int)king.Coords - 3].Piece).HasMoved) castling.Add(king.Coords - 2); return castling; }
/// <summary> /// Coordinates king can move to /// </summary> /// <param name="king"></param> /// <returns></returns> private List<Coords> GetKingAvailableCoords(King king) { return king .AvailableCoords(Board.Tiles) .Except(GetAllAvailableCoords(king.Side == PieceColor.White ? PieceColor.Black : PieceColor.White, true, true)) .Except(king.Side == PieceColor.White ? Board.BlackKing.AvailableCoords(Board.Tiles) : Board.WhiteKing.AvailableCoords(Board.Tiles)) .Except(king.Checks.Where(x => !x.IsAttackBlockable).Select(x => x.Coordinate)) .Concat(GetKingCastlingCoords(king)) .ToList(); }