Example #1
0
        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);
        }
        // Code Review: Надто об'ємний метод.
        // Code Review: Аргументи методу повинні починатися з малої літери.
        private static ChessBoard PerformCastling(ChessBoard board, bool IsWhite, bool IsKingCastling, out char kingfile)
        {
            ChessBoard tempboard = board.ShallowCopy();
            sbyte      castlingrank;
            sbyte      kingsbyte;
            sbyte      rooksbyte;
            char       rookfile;

            if (IsWhite)
            {
                castlingrank = 1;
                kingsbyte    = (sbyte)DefaultPieces.WhiteKing;
                rooksbyte    = (sbyte)DefaultPieces.WhiteRook;
            }
            else
            {
                castlingrank = 8;
                kingsbyte    = (sbyte)DefaultPieces.BlackKing;
                rooksbyte    = (sbyte)DefaultPieces.BlackRook;
            }
            kingfile = Piece.GetPosition(board, kingsbyte).file;
            if (IsKingCastling)
            {
                rookfile = 'h';
                for (char tfile = 'h'; tfile >= 'c'; tfile--)
                {
                    if (board[tfile, castlingrank] == rooksbyte)
                    {
                        rookfile = tfile;
                        break;
                    }
                }
                tempboard = Piece.PerformMove(tempboard, new Square(kingfile, castlingrank), new Square('g', castlingrank));
                tempboard = Piece.PerformMove(tempboard, new Square(rookfile, castlingrank), new Square('f', castlingrank));
            }
            else
            {
                rookfile = 'a';
                for (char tfile = 'a'; tfile <= 'f'; tfile++)
                {
                    if (board[tfile, castlingrank] == rooksbyte)
                    {
                        rookfile = tfile;
                        break;
                    }
                }
                tempboard = Piece.PerformMove(tempboard, new Square(kingfile, castlingrank), new Square('c', castlingrank));
                tempboard = Piece.PerformMove(tempboard, new Square(rookfile, castlingrank), new Square('d', castlingrank));
            }
            return(tempboard);
        }