// Code Review: Аргументи методу повинні починатися з малої літери.
        public static bool CheckIfArePossibleMoves(ChessBoard board, bool IsWhite)
        {
            // Code Review: Назва локальної змінної повинна починатися з малої літери.
            int           comparerresult = IsWhite ? 1 : -1;
            List <Square> pieces         = new List <Square>();

            for (char tfile = 'a'; tfile <= 'h'; tfile++)
            {
                for (sbyte trank = 1; trank <= 8; trank++)
                {
                    if (board[tfile, trank].CompareTo(0) == comparerresult)
                    {
                        pieces.Add(new Square(tfile, trank));
                    }
                }
            }
            foreach (Square temp in pieces)
            {
                char piecetype = GetLetter(board[temp]);
                GetPiecePositionsType function = IsWhite ? GetWhitePiecePositionsType(piecetype) : GetBlackPiecePositionsType(piecetype);
                if (function(board, temp.file, temp.rank).Count > 0)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #2
0
        // Code Review: Надто об'ємний метод.
        // Code Review: Потрібно розділяти умови дужками () в операторах if.
        public static ChessBoard PerformWhiteMove(ChessBoard board, string notation)
        {
            ChessBoard tempboard = board.ShallowCopy();

            #region King-castling
            if (notation == "O-O" && DefaultInfo.WhiteKingIsUnMoved && DefaultInfo.WhiteHsideRookIsUnMoved)
            {
                char kingfile = 'e';
                for (char tfile = 'b'; tfile <= 'g'; tfile++)
                {
                    if (board[tfile, 1] == (sbyte)DefaultPieces.WhiteKing)
                    {
                        kingfile = tfile;
                        break;
                    }
                }
                char Hrookfile = 'h';
                for (char tfile = 'h'; tfile >= 'c'; tfile++)
                {
                    if (board[tfile, 1] == (sbyte)DefaultPieces.WhiteRook)
                    {
                        Hrookfile = tfile;
                        break;
                    }
                }
                tempboard[kingfile, 1]  = 0;
                tempboard[Hrookfile, 1] = 0;
                tempboard['g', 1]       = (sbyte)DefaultPieces.WhiteKing;
                tempboard['f', 1]       = (sbyte)DefaultPieces.WhiteRook;
                if (WhiteKing.GetPossiblePositions(board, kingfile, 1).Contains(tempboard))
                {
                    return(tempboard);
                }
                else
                {
                    throw new ArgumentException("Wrong move");
                }
            }
            #endregion
            #region Queen-castling
            else if (notation == "O-O-O" && DefaultInfo.WhiteKingIsUnMoved && DefaultInfo.WhiteAsideRookIsUnMoved)
            {
                char kingfile = 'e';
                for (char tfile = 'b'; tfile <= 'g'; tfile++)
                {
                    if (board[tfile, 1] == (sbyte)DefaultPieces.WhiteKing)
                    {
                        kingfile = tfile;
                        break;
                    }
                }
                char Arookfile = 'h';
                for (char tfile = 'a'; tfile <= 'f'; tfile++)
                {
                    if (board[tfile, 1] == (sbyte)DefaultPieces.WhiteRook)
                    {
                        Arookfile = tfile;
                        break;
                    }
                }
                tempboard[kingfile, 1]  = 0;
                tempboard[Arookfile, 1] = 0;
                tempboard['c', 1]       = (sbyte)DefaultPieces.WhiteKing;
                tempboard['d', 1]       = (sbyte)DefaultPieces.WhiteRook;
                if (WhiteKing.GetPossiblePositions(board, kingfile, 1).Contains(tempboard))
                {
                    return(tempboard);
                }
                else
                {
                    throw new ArgumentException("Wrong move");
                }
            }
            #endregion
            #region Pawn
            else if (notation[0] <= 'h' && notation[0] >= 'a')
            {
                #region simple move
                if (notation[1] <= '8' && notation[1] >= '3' && notation.Length == 2)
                {
                    //simple move (not 4 rank)
                    if (notation[1] != 4)
                    {
                        sbyte pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) - 1);
                        char  pawnfile = notation[0];
                        tempboard[pawnfile, pawnrank]     = 0;
                        tempboard[pawnfile, pawnrank + 1] = (sbyte)DefaultPieces.WhitePawn;
                        if (WhitePawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard))
                        {
                            return(tempboard);
                        }
                        else
                        {
                            throw new ArgumentException("Wrong move");
                        }
                    }
                    //simple move (4 rank)
                    else
                    {
                        char  pawnfile = notation[0];
                        sbyte pawnrank;
                        if (board[pawnfile, (sbyte)(Char.GetNumericValue(notation[1]) - 1)] == (sbyte)DefaultPieces.WhitePawn)
                        {
                            pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) - 1);
                        }
                        else if (board[pawnfile, (sbyte)(Char.GetNumericValue(notation[1]) - 2)] == (sbyte)DefaultPieces.WhitePawn)
                        {
                            pawnrank = (sbyte)(Char.GetNumericValue(notation[1]) - 2);
                        }
                        else
                        {
                            throw new ArgumentException("Wrong move");
                        }
                        tempboard[pawnfile, pawnrank] = 0;
                        tempboard[pawnfile, (sbyte)Char.GetNumericValue(notation[1])] = (sbyte)DefaultPieces.WhitePawn;
                        if (WhitePawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard))
                        {
                            return(tempboard);
                        }
                        else
                        {
                            throw new ArgumentException("Wrong move");
                        }
                    }
                }
                #endregion
                #region capture move
                else if (notation.Length == 4 && notation[1] == 'x' && notation[2] <= 'h' && notation[2] >= 'a' && notation[3] <= '8' && notation[3] >= '3')
                {
                    char  pawnfile  = notation[0];
                    char  enemyfile = notation[2];
                    sbyte enemyrank = (sbyte)Char.GetNumericValue(notation[3]);
                    sbyte pawnrank  = (sbyte)(enemyrank - 1);
                    tempboard[pawnfile, pawnrank]   = 0;
                    tempboard[enemyfile, enemyrank] = (sbyte)DefaultPieces.WhitePawn;
                    if (WhitePawn.GetPossiblePositions(board, pawnfile, pawnrank).Contains(tempboard))
                    {
                        return(tempboard);
                    }
                    else
                    {
                        throw new ArgumentException("Wrong move");
                    }
                }
                #endregion
                //To Do en passant!!!
                else
                {
                    throw new ArgumentException("Wrong move");
                }
            }
            #endregion
            #region Pieces (not Pawn)
            else if (notation[0] <= 'Z' && notation[0] >= 'A')
            {
                #region Non-ambiguous
                if ((notation.Length == 3 && notation[1] <= 'h' && notation[1] >= 'a' && notation[2] >= '0' && notation[2] <= '8') || (notation.Length == 4 && notation[1] == 'x' && notation[2] <= 'h' && notation[2] >= 'a' && notation[2] >= '0' && notation[2] <= '8'))
                {
                    char  goalfile;
                    sbyte goalrank;
                    if (notation.Length == 3)
                    {
                        goalfile = notation[1];
                        goalrank = (sbyte)Char.GetNumericValue(notation[2]);
                    }
                    else
                    {
                        goalfile = notation[2];
                        goalrank = (sbyte)Char.GetNumericValue(notation[3]);
                    }
                    //Check if piece is not-ambigious and get piece square
                    List <Square> PossibleMovers     = GetPossibleBlackMoversToSquare(board, goalfile, goalrank);
                    int           NumberOfSamePieces = 0;
                    Square        PieceSquare        = new Square();
                    foreach (Square tempsquare in PossibleMovers)
                    {
                        if (board[tempsquare.file, tempsquare.rank] == GetSbyteFromWhitePieceLetter(notation[0]))
                        {
                            NumberOfSamePieces++;
                            PieceSquare = tempsquare;
                        }
                    }
                    if (NumberOfSamePieces != 1)
                    {
                        throw new ArgumentException("wrong notation");
                    }
                    tempboard[PieceSquare.file, PieceSquare.rank] = 0;
                    tempboard[goalfile, goalrank] = GetSbyteFromWhitePieceLetter(notation[0]);
                    GetPiecePositionsType piecefunction = GetWhitePiecePositionsType(notation[0]);
                    if (piecefunction(board, goalfile, goalrank).Contains(tempboard))
                    {
                        return(tempboard);
                    }
                    else
                    {
                        throw new ArgumentException("wrong move");
                    }
                }
                #endregion
                #region Ambiguous
                else if (notation.Length <= 6 && notation.Length >= 4 && notation[2] != 'x')
                {
                    //&& ( (notation.Length==4 && notation[1]>='a' && notation[1]<='h' && notation[2]>='a' && notation[2]<='h' && notation[3]<='8' && notation[3]>='1' ) || ( (notation.Length==4 && notation[1]>='a' && notation[1]<='h' && notation[2]>='a' && notation[2]<='h' && notation[3]<='8' && notation[3]>='1' )  ) )
                    char  piecefile;
                    sbyte piecerank;
                    char  goalfile;
                    sbyte goalrank;
                    sbyte piecetype = GetSbyteFromWhitePieceLetter(notation[0]);
                    //Ngf3
                    if (notation.Length == 4 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8')
                    {
                        goalfile  = notation[2];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[3]);
                        piecefile = notation[1];
                        for (sbyte i = 1; i <= 8; i++)
                        {
                            if (board[piecefile, i] == piecetype)
                            {
                                piecerank = i;
                                break;
                            }
                        }
                    }
                    //Ngxf3
                    else if (notation.Length == 5 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] == 'x' && notation[3] >= 'a' && notation[3] <= 'h' && notation[4] >= '1' && notation[4] <= '8')
                    {
                        goalfile  = notation[3];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[4]);
                        piecefile = notation[1];
                        for (sbyte i = 1; i <= 8; i++)
                        {
                            if (board[piecefile, i] == piecetype)
                            {
                                piecerank = i;
                                break;
                            }
                        }
                    }
                    //N5f3
                    else if (notation.Length == 4 && notation[1] >= '1' && notation[1] <= '8' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8')
                    {
                        goalfile  = notation[2];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[4]);
                        piecerank = (sbyte)Char.GetNumericValue(notation[1]);
                        for (char tfile = 'a'; tfile <= 'h'; tfile++)
                        {
                            if (board[tfile, piecerank] == piecetype)
                            {
                                piecefile = tfile;
                                break;
                            }
                        }
                    }
                    //N5xf4
                    else if (notation.Length == 5 && notation[1] >= '1' && notation[1] <= '8' && notation[2] == 'x' && notation[3] >= 'a' && notation[3] <= 'h' && notation[4] >= '1' && notation[4] <= '8')
                    {
                        goalfile  = notation[3];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[5]);
                        piecerank = (sbyte)Char.GetNumericValue(notation[1]);
                        for (char tfile = 'a'; tfile <= 'h'; tfile++)
                        {
                            if (board[tfile, piecerank] == piecetype)
                            {
                                piecefile = tfile;
                                break;
                            }
                        }
                    }
                    //Ng5f3
                    else if (notation.Length == 5 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= '1' && notation[2] <= '8' && notation[3] >= 'a' && notation[3] <= 'h' && notation[4] >= '1' && notation[4] <= '8')
                    {
                        goalfile  = notation[3];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[4]);
                        piecefile = notation[1];
                        piecerank = (sbyte)Char.GetNumericValue(notation[2]);
                    }
                    //Ng5xf3
                    else if (notation.Length == 6 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= '1' && notation[2] <= '8' && notation[3] == 'x' && notation[4] >= 'a' && notation[4] <= 'h' && notation[5] >= '1' && notation[5] <= 'h')
                    {
                        goalfile  = notation[4];
                        goalrank  = (sbyte)Char.GetNumericValue(notation[5]);
                        piecefile = notation[1];
                        piecerank = (sbyte)Char.GetNumericValue(notation[2]);
                    }
                }
                #endregion
            }
            #endregion
            else
            {
                throw new ArgumentException("Wrong notation");
            }
        }
        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");
            }
        }
        // Code Review: Надто об'ємний метод.
        private static ChessBoard PerformBlackAmbiguousMove(ChessBoard board, string notation, ChessBoard tempboard)
        {
            char  piecefile = default(char);
            sbyte piecerank = default(sbyte);
            char  goalfile;
            sbyte goalrank;
            sbyte piecetype = GetSbyteFromBlackPieceLetter(notation[0]);

            //Ngf3
            if (notation.Length == 4 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8')
            {
                goalfile  = notation[2];
                goalrank  = (sbyte)Char.GetNumericValue(notation[3]);
                piecefile = notation[1];
                for (sbyte i = 1; i <= 8; i++)
                {
                    if (board[piecefile, i] == piecetype)
                    {
                        piecerank = i;
                        break;
                    }
                }
            }
            //N5f3
            else if (notation.Length == 4 && notation[1] >= '1' && notation[1] <= '8' && notation[2] >= 'a' && notation[2] <= 'h' && notation[3] >= '1' && notation[3] <= '8')
            {
                goalfile  = notation[2];
                goalrank  = (sbyte)Char.GetNumericValue(notation[3]);
                piecerank = (sbyte)Char.GetNumericValue(notation[1]);
                for (char tfile = 'a'; tfile <= 'h'; tfile++)
                {
                    if (board[tfile, piecerank] == piecetype)
                    {
                        piecefile = tfile;
                        break;
                    }
                }
            }
            //Ng5f3
            else if (notation.Length == 5 && notation[1] >= 'a' && notation[1] <= 'h' && notation[2] >= '1' && notation[2] <= '8' && notation[3] >= 'a' && notation[3] <= 'h' && notation[4] >= '1' && notation[4] <= '8')
            {
                goalfile  = notation[3];
                goalrank  = (sbyte)Char.GetNumericValue(notation[4]);
                piecefile = notation[1];
                piecerank = (sbyte)Char.GetNumericValue(notation[2]);
            }
            else
            {
                throw new ArgumentException("wrong notation");
            }

            tempboard[goalfile, goalrank]   = piecetype;
            tempboard[piecefile, piecerank] = 0;
            GetPiecePositionsType piecefunction = GetBlackPiecePositionsType(notation[0]);

            if (piecefunction(board, piecefile, piecerank).Contains(tempboard))
            {
                if (board[piecefile, piecerank] == (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 < piecefile)
                        {
                            DefaultInfo.BlackHsideRookIsUnMoved = false;
                        }
                        else
                        {
                            DefaultInfo.BlackAsideRookIsUnMoved = false;
                        }
                    }
                }
                return(tempboard);
            }
            else
            {
                throw new ArgumentException("wrong move");
            }
        }