示例#1
0
        // Method to determine of the SelectedPiece is attacking the other players king
        public bool attackingKing(ref ChessBoardState chessBoardState)
        {
            // Switch Condition to test which piece is selected.
            switch (chessBoardState.SelectedPiece.piece)
            {
            // return if the piece is attacking the other players king.
            case Pieces.bb: return(BishopMoves.attackingKing(ref chessBoardState));

            case Pieces.bk: return(KingMoves.attackingKing(ref chessBoardState));

            case Pieces.bn: return(KnightMoves.attakingKing(ref chessBoardState));

            case Pieces.bp: return(PawnMoves.attakingKing(ref chessBoardState));

            case Pieces.bq: return(QueenMoves.attackingKing(ref chessBoardState));

            case Pieces.br: return(RookMoves.attackingKing(ref chessBoardState));

            case Pieces.wb: return(BishopMoves.attackingKing(ref chessBoardState));

            case Pieces.wk: return(KingMoves.attackingKing(ref chessBoardState));

            case Pieces.wn: return(KnightMoves.attakingKing(ref chessBoardState));

            case Pieces.wp: return(PawnMoves.attakingKing(ref chessBoardState));

            case Pieces.wq: return(QueenMoves.attackingKing(ref chessBoardState));

            case Pieces.wr: return(RookMoves.attackingKing(ref chessBoardState));
            }

            return(false);
        }
示例#2
0
        //Method to test if the other player is in check.
        public bool Check(ref ChessBoardState chessBoardState)
        {
            List <ChessPiece> playersPieces = chessBoardState.WhitesMove ? chessBoardState.WhitePieces : chessBoardState.BlackPieces;

            //Square otherKingSquare = chessBoardState.WhitesMove ? chessBoardState.BlackKingSquare : chessBoardState.WhiteKingSquare;

            ChessPiece selectedPiece = chessBoardState.SelectedPiece;

            for (int i = 0; i < playersPieces.Count; i++)
            {
                chessBoardState.SelectedPiece = playersPieces[i];
                //Console.Write($" {chessBoardState.SelectedPiece.piece.ToString()}");

                /*List<Square> playerPotentialMoves = getPotentialMoves(ref chessBoardState);
                 * for (int j = 0; j < playerPotentialMoves.Count; j++)
                 * {
                 *  if (playerPotentialMoves[j].Equals(otherKingSquare))
                 *  {
                 *      chessBoardState.SelectedPiece = selectedPiece;
                 *      return true;
                 *  }
                 * }*/
                if (attackingKing(ref chessBoardState))
                {
                    chessBoardState.SelectedPiece = selectedPiece;
                    return(true);
                }
            }
            chessBoardState.SelectedPiece = selectedPiece;
            return(false);
        }
示例#3
0
        // Method to return the valid moves of the selected piece.
        public List <Square> getValidMoves(ref ChessBoardState chessBoardState)
        {
            List <Square> validMoves = new List <Square>();

            ChessBoardState cBoard = cloneChessBoardState(ref chessBoardState);

            List <Square> potentialMoves = getPotentialMoves(ref cBoard);

            //Console.WriteLine(potentialMoves.Count);

            for (int i = 0; i < potentialMoves.Count; i++)
            {
                //Console.WriteLine($"{potentialMoves[i].row}, {potentialMoves[i].column}");
                movePiece(ref cBoard, potentialMoves[i]);
                if (!Check(ref cBoard))
                {
                    validMoves.Add(potentialMoves[i]);
                }
                cBoard = cloneChessBoardState(ref chessBoardState); // TODO undo move instead.
            }

            backGroundChanges = new List <ChessPiece>();

            if (chessBoardState.SelectedPiece.piece == Pieces.wk || chessBoardState.SelectedPiece.piece == Pieces.bk)
            {
                validCastling(ref chessBoardState, ref validMoves);;
            }
            //validMoves = potentialMoves;
            return(validMoves);
        }
示例#4
0
 // Method to test if a rook is being moved.
 private void RookMove(ref ChessBoardState chessBoardState)
 {
     // Condition to test if the Selected Piece is a rook.
     if (chessBoardState.SelectedPiece.piece == Pieces.wr || chessBoardState.SelectedPiece.piece == Pieces.br)
     {
         // Condition to test if it is WhitesMove
         if (chessBoardState.WhitesMove)
         {
             // White King rook
             if (chessBoardState.WhiteRooksMoved[0] == false && chessBoardState.SelectedPiece.square.Equals(new Square(7, 7)))
             {
                 chessBoardState.WhiteRooksMoved[0] = true;
             }
             // White Queen rook
             if (chessBoardState.WhiteRooksMoved[1] == false && chessBoardState.SelectedPiece.square.Equals(new Square(7, 0)))
             {
                 chessBoardState.WhiteRooksMoved[1] = true;
             }
             return;
         }
         // Black King rook
         if (chessBoardState.BlackRooksMoved[0] == false && chessBoardState.SelectedPiece.square.Equals(new Square(0, 7)))
         {
             chessBoardState.BlackRooksMoved[0] = true;
         }
         // Black Queen rook
         if (chessBoardState.BlackRooksMoved[1] == false && chessBoardState.SelectedPiece.square.Equals(new Square(0, 0)))
         {
             chessBoardState.BlackRooksMoved[1] = true;
         }
     }
 }
示例#5
0
        public List <Square> getPotentialMoves(ref ChessBoardState chessBoardState)
        {
            List <Square> potentialMoves = new List <Square>();

            // Get the potential moves for the selected Piece
            Pieces piece = chessBoardState.SelectedPiece.piece;

            switch (piece)
            {
            case Pieces.bb: potentialMoves = BishopMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.bk: potentialMoves = KingMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.bn: potentialMoves = KnightMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.bp: potentialMoves = PawnMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.bq: potentialMoves = QueenMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.br: potentialMoves = RookMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wb: potentialMoves = BishopMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wk: potentialMoves = KingMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wn: potentialMoves = KnightMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wp: potentialMoves = PawnMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wq: potentialMoves = QueenMoves.getPotentialMoves(ref chessBoardState); break;

            case Pieces.wr: potentialMoves = RookMoves.getPotentialMoves(ref chessBoardState); break;
            }
            return(potentialMoves);
        }
示例#6
0
        public void GetMoves_Rook(ChessColor color)
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSideToMove(color);
            board.SetSpot(4, 5, color == ChessColor.White ? ChessPiece.WhiteRook : ChessPiece.BlackRook);

            var expectedMoves = new List <string>
            {
                "Rd5-d1",
                "Rd5-d2",
                "Rd5-d3",
                "Rd5-d4",
                "Rd5-a5",
                "Rd5-b5",
                "Rd5-c5",
                "Rd5-e5",
                "Rd5-f5",
                "Rd5-g5",
                "Rd5-h5",
                "Rd5-d6",
                "Rd5-d7",
                "Rd5-d8"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#7
0
        public void GetMoves_WhiteCanDeliverCheck()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSpot(1, 1, ChessPiece.WhiteRook);
            board.SetSpot(5, 1, ChessPiece.WhiteKing);
            board.SetSpot(5, 8, ChessPiece.BlackKing);
            board.SetSpot(1, 8, ChessPiece.BlackRook);

            var expectedMoves = new List <string>
            {
                "Ra1-b1",
                "Ra1-c1",
                "Ra1-d1",
                "Ra1-a2",
                "Ra1-a3",
                "Ra1-a4",
                "Ra1-a5",
                "Ra1-a6",
                "Ra1-a7",
                "Ra1xa8",
                "Ke1-d1",
                "Ke1-f1",
                "Ke1-d2",
                "Ke1-e2",
                "Ke1-f2",
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#8
0
        public void GetMoves_BlackPawn_PromotionCapture_Blockers()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSideToMove(ChessColor.Black);
            board.SetSpot(4, 2, ChessPiece.BlackPawn);
            board.SetSpot(3, 1, ChessPiece.WhiteKnight);
            board.SetSpot(5, 1, ChessPiece.BlackKnight);

            var expectedMoves = new List <string>
            {
                // Knight moves (don't forget!)
                "Ne1-c2",
                "Ne1-g2",
                "Ne1-d3",
                "Ne1-f3",

                "Pd2xc1=b",
                "Pd2xc1=n",
                "Pd2xc1=q",
                "Pd2xc1=r",
                "Pd2-d1=b",
                "Pd2-d1=n",
                "Pd2-d1=q",
                "Pd2-d1=r",
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#9
0
        public ChessBoardState GetBoardState()
        {
            ChessBoardState state = ChessBoardState.Normal;

            GetBoardThreats(
                out var ownKingUnderThreat,
                CurrentPlayer == PiecePlayer.White ? PiecePlayer.Black : PiecePlayer.White);
            int movesAvailable = GetAllAvailableMoves().Count();

            if (ownKingUnderThreat == true)
            {
                if (movesAvailable == 0)
                {
                    state = ChessBoardState.CheckMate;
                }
                else
                {
                    state = ChessBoardState.Check;
                }
            }
            else if (movesAvailable == 0)
            {
                state = ChessBoardState.StaleMate;
            }

            return(state);
        }
示例#10
0
        public void GetMoves_BlackPawn_PromotionCapture()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSideToMove(ChessColor.Black);
            board.SetSpot(4, 2, ChessPiece.BlackPawn);
            board.SetSpot(3, 1, ChessPiece.WhiteKnight);
            board.SetSpot(5, 1, ChessPiece.WhiteKnight);

            var expectedMoves = new List <string>
            {
                "Pd2xc1=b",
                "Pd2xc1=n",
                "Pd2xc1=q",
                "Pd2xc1=r",
                "Pd2-d1=b",
                "Pd2-d1=n",
                "Pd2-d1=q",
                "Pd2-d1=r",
                "Pd2xe1=b",
                "Pd2xe1=n",
                "Pd2xe1=q",
                "Pd2xe1=r",
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#11
0
        public void GetPossibleFirstMovesForBlack()
        {
            var board = new ChessBoardState();

            board.TryMakeMove(5); // White moves a pawn.

            var expectedMoves = new List <string>
            {
                "Pa7-a5",
                "Pa7-a6",
                "Pb7-b5",
                "Pb7-b6",
                "Pc7-c5",
                "Pc7-c6",
                "Pd7-d5",
                "Pd7-d6",
                "Pe7-e5",
                "Pe7-e6",
                "Pf7-f5",
                "Pf7-f6",
                "Pg7-g5",
                "Pg7-g6",
                "Ph7-h5",
                "Ph7-h6",
                "Nb8-a6",
                "Nb8-c6",
                "Ng8-f6",
                "Ng8-h6"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#12
0
        public void GetMoves_WhitePawn_PromotionCapture()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSpot(4, 7, ChessPiece.WhitePawn);
            board.SetSpot(3, 8, ChessPiece.BlackKnight);
            board.SetSpot(5, 8, ChessPiece.BlackKnight);

            var expectedMoves = new List <string>
            {
                "Pd7xc8=b",
                "Pd7xc8=n",
                "Pd7xc8=q",
                "Pd7xc8=r",
                "Pd7-d8=b",
                "Pd7-d8=n",
                "Pd7-d8=q",
                "Pd7-d8=r",
                "Pd7xe8=b",
                "Pd7xe8=n",
                "Pd7xe8=q",
                "Pd7xe8=r",
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#13
0
        public void GetMoves_WhitePawn_PromotionCapture_Blockers()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSpot(4, 7, ChessPiece.WhitePawn);
            board.SetSpot(3, 8, ChessPiece.BlackKnight);
            board.SetSpot(5, 8, ChessPiece.WhiteKnight);

            var expectedMoves = new List <string>
            {
                "Pd7xc8=b",
                "Pd7xc8=n",
                "Pd7xc8=q",
                "Pd7xc8=r",
                "Pd7-d8=b",
                "Pd7-d8=n",
                "Pd7-d8=q",
                "Pd7-d8=r",

                // Knight moves (don't forget!)
                "Ne8-d6",
                "Ne8-f6",
                "Ne8-c7",
                "Ne8-g7"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#14
0
        public void GetDefaultFEN()
        {
            var board    = new ChessBoardState();
            var boardFEN = board.GetFEN();

            Assert.AreEqual("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", boardFEN);
        }
示例#15
0
        public void GetPossibleFirstMovesForWhite()
        {
            var board = new ChessBoardState();

            var expectedMoves = new List <string>
            {
                "Nb1-a3",
                "Nb1-c3",
                "Ng1-f3",
                "Ng1-h3",
                "Pa2-a3",
                "Pa2-a4",
                "Pb2-b3",
                "Pb2-b4",
                "Pc2-c3",
                "Pc2-c4",
                "Pd2-d3",
                "Pd2-d4",
                "Pe2-e3",
                "Pe2-e4",
                "Pf2-f3",
                "Pf2-f4",
                "Pg2-g3",
                "Pg2-g4",
                "Ph2-h3",
                "Ph2-h4"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#16
0
        public void GetMoves_WhiteKing_CanCastleBothSides()
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSpot(5, 1, ChessPiece.WhiteKing);
            board.SetSpot(1, 1, ChessPiece.WhiteRook);
            board.SetSpot(8, 1, ChessPiece.WhiteRook);
            board.SetSpot(1, 2, ChessPiece.BlackRook);
            board.SetSpot(8, 2, ChessPiece.BlackRook);
            board.SetWhiteCanCastleKingside(true);
            board.SetWhiteCanCastleQueenside(true);

            var expectedMoves = new List <string>
            {
                // A1 Rook
                "Ra1-b1",
                "Ra1-c1",
                "Ra1-d1",
                "Ra1xa2",

                // King
                "O-O-O",
                "Ke1-d1",
                "Ke1-f1",
                "O-O",

                // H1 Rook
                "Rh1-f1",
                "Rh1-g1",
                "Rh1xh2"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#17
0
 private void PawnPromotion(ref ChessBoardState chessBoardState, Square square)
 {
     // Condition to test if the SelectedPiece is a pawn.
     if (chessBoardState.SelectedPiece.piece == Pieces.wp || chessBoardState.SelectedPiece.piece == Pieces.bp)
     {
         // Initialize backRank to be the row of the other players backRank.
         int backRank = chessBoardState.WhitesMove ? 0 : 7;
         // Condtion to test if the row the pawn is being moved to is the backRank.
         if (square.row == backRank)
         {
             // Condition to test if it is WhitesMove and to queen.
             if (chessBoardState.WhitesMove && chessBoardState.WhiteAlwaysQueen)
             {
                 chessBoardState.SelectedPiece.piece = Pieces.wq;
                 return;
             }
             // Condition to test if it is BlacksMove and to queen.
             if (!chessBoardState.WhitesMove && chessBoardState.BlackAlwaysQueen)
             {
                 chessBoardState.SelectedPiece.piece = Pieces.bq;
                 return;
             }
             chessBoardState.promotePawn = true;
             //Console.WriteLine("promote!");
         }
     }
 }
示例#18
0
        private void GetMoves_Bishop(ChessColor color)
        {
            var board = new ChessBoardState();

            board.Clear();
            board.SetSideToMove(color);
            board.SetSpot(4, 5, color == ChessColor.White ? ChessPiece.WhiteBishop : ChessPiece.BlackBishop);

            var expectedMoves = new List <string>
            {
                "Bd5-h1",
                "Bd5-a2",
                "Bd5-g2",
                "Bd5-b3",
                "Bd5-f3",
                "Bd5-c4",
                "Bd5-e4",
                "Bd5-c6",
                "Bd5-e6",
                "Bd5-b7",
                "Bd5-f7",
                "Bd5-a8",
                "Bd5-g8"
            };

            CheckPossibleMoves(board, expectedMoves);
        }
示例#19
0
        internal bool attackingKing(ref ChessBoardState chessBoardState)
        {
            // Declare otherKingSquare to be the square the other players king is on.
            Square otherKingSquare = chessBoardState.WhitesMove ? chessBoardState.BlackKingSquare : chessBoardState.WhiteKingSquare;

            // Condition to test if the king is one square from the other king.
            if (Math.Abs(chessBoardState.SelectedPiece.square.row - otherKingSquare.row) <= 1 && Math.Abs(chessBoardState.SelectedPiece.square.column - otherKingSquare.column) <= 1)
            {
                return(true);
            }
            return(false);
        }
示例#20
0
        //Method to test if the selected pawn is attaking the other players king.
        internal bool attakingKing(ref ChessBoardState chessBoardState)
        {
            // Declare otherKingSquare to be the square the other players king is on.
            Square otherKingSquare = chessBoardState.WhitesMove ? chessBoardState.BlackKingSquare : chessBoardState.WhiteKingSquare;

            int pawnOffset = chessBoardState.WhitesMove ? -1 : 1;

            if ((chessBoardState.SelectedPiece.square.row + pawnOffset == otherKingSquare.row) && ((chessBoardState.SelectedPiece.square.column + 1 == otherKingSquare.column) || (chessBoardState.SelectedPiece.square.column - 1 == otherKingSquare.column)))
            {
                return(true);
            }

            return(false);
        }