Exemple #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);
        }
Exemple #2
0
        public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank)
        {
            List <ChessBoard> result = new List <ChessBoard>();
            ChessBoard        tempboard;
            List <Square>     moves   = new List <Square>();
            Square            current = new Square(file, rank);

            WhitePiece.GetDiagonalDestinations(board, ref moves, current, 1, 1);
            WhitePiece.GetDiagonalDestinations(board, ref moves, current, 1, -1);
            WhitePiece.GetDiagonalDestinations(board, ref moves, current, -1, 1);
            WhitePiece.GetDiagonalDestinations(board, ref moves, current, -1, -1);
            foreach (Square move in moves)
            {
                tempboard = Piece.PerformMove(board, current, move);
                result    = Piece.AddNewPosition(result, tempboard, true);
            }
            return(result);
        }
Exemple #3
0
        public static List <ChessBoard> GetPossiblePositions(ChessBoard board, char file, sbyte rank)
        {
            List <ChessBoard> result = new List <ChessBoard>();
            ChessBoard        tempboard;
            List <Square>     moves   = new List <Square>();
            Square            current = new Square(file, rank);

            WhitePiece.GetVerticalUpDestinations(board, moves, current);
            WhitePiece.GetVerticalDownDestinations(board, moves, current);
            WhitePiece.GetHorizontalLeftDestinations(board, moves, current);
            WhitePiece.GetHorizontalRightDestinations(board, moves, current);
            Console.WriteLine(moves.Count);
            foreach (Square move in moves)
            {
                tempboard = Piece.PerformMove(board, current, move);
                result    = Piece.AddNewPosition(result, tempboard, true);
            }
            Console.WriteLine(result.Count);
            return(result);
        }
Exemple #4
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");
            }
        }