public static bool IsSafe(ChessBoard board) { Square current = Piece.GetPosition(board, (sbyte)DefaultPieces.WhiteKing); char file = current.file; int rank = current.rank; List <Square> result = WhitePiece.GetPossibleBlackAttackersToSquare(board, current); return(result.Count == 0); }
public static List <Square> GetPossibleWhiteAttackersToSquare(ChessBoard board, Square goalsquare) { ChessBoard tempboard = board.ShallowCopy(); tempboard.DebugConsoleSimpleDraw(); Console.WriteLine(); tempboard.ReverseSides(); //tempboard.DebugConsoleSimpleDraw(); Square tempsquare = new Square(goalsquare.file, goalsquare.rank); tempsquare.Reverse(); var result = WhitePiece.GetPossibleBlackAttackersToSquare(tempboard, tempsquare); for (int i = 0; i < result.Count; i++) { Square temp = result[i]; temp.Reverse(); result[i] = temp; } return(result); }
private static ChessBoard PerformBlackNonAmbiguousMove(ChessBoard board, string notation, ChessBoard tempboard) { char goalfile = notation[1]; sbyte goalrank = (sbyte)Char.GetNumericValue(notation[2]); //Check if piece is not-ambigious and get piece square List <Square> PossibleMovers = WhitePiece.GetPossibleBlackAttackersToSquare(board, new Square(goalfile, goalrank)); int NumberOfSamePieces = 0; Square PieceSquare = new Square(); foreach (Square tempsquare in PossibleMovers) { if (board[tempsquare.file, tempsquare.rank] == GetSbyteFromBlackPieceLetter(notation[0])) { NumberOfSamePieces++; PieceSquare = tempsquare; } } if (NumberOfSamePieces != 1) { throw new ArgumentException("wrong notation"); } tempboard[PieceSquare.file, PieceSquare.rank] = 0; tempboard[goalfile, goalrank] = GetSbyteFromBlackPieceLetter(notation[0]); GetPiecePositionsType piecefunction = GetBlackPiecePositionsType(notation[0]); if (piecefunction(board, PieceSquare.file, PieceSquare.rank).Contains(tempboard)) { if (board[PieceSquare.file, PieceSquare.rank] == (sbyte)DefaultPieces.BlackRook && (DefaultInfo.BlackAsideRookIsUnMoved || DefaultInfo.BlackHsideRookIsUnMoved)) { if (DefaultInfo.BlackAsideRookIsUnMoved && !DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackAsideRookIsUnMoved = false; } else if (!DefaultInfo.BlackAsideRookIsUnMoved && DefaultInfo.BlackHsideRookIsUnMoved) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { char rookfile; for (rookfile = 'a'; rookfile <= 'h'; rookfile++) { if (board[rookfile, 8] == (sbyte)DefaultPieces.BlackRook) { break; } } if (rookfile < PieceSquare.file) { DefaultInfo.BlackHsideRookIsUnMoved = false; } else { DefaultInfo.BlackAsideRookIsUnMoved = false; } } } return(tempboard); } else { throw new ArgumentException("wrong move"); } }