Example #1
0
        static void Main(string[] args)
        {
            Chessboard chessboard = new Chessboard();

            // IPiece interface? Wanna be able to get it all in a list.
            Pawn   whitePawn   = new Pawn(true, chessboard.Board[4, 3]);
            Pawn   blackPawn   = new Pawn(false, chessboard.Board[6, 0]);
            Knight blackKnight = new Knight(false, chessboard.Board[2, 2]);
            King   whiteKing   = new King(false, chessboard.Board[7, 3]);
            Rook   whiteRook   = new Rook(true, chessboard.Board[3, 6]);
            Bishop blackBishop = new Bishop(false, chessboard.Board[2, 4]);
            Queen  whiteQueen  = new Queen(true, chessboard.Board[1, 5]);

            List <Pieces> pieces = new List <Pieces>
            {
                whitePawn,
                blackPawn,
                blackKnight,
                whiteKing,
                whiteRook,
                blackBishop,
                whiteQueen
            };

            Console.WriteLine("The Pieces");
            chessboard.ClearMarkedLegalMoves();
            PrintBoardOccupiedAndLegal(chessboard);

            foreach (Pieces piece in pieces)
            {
                Console.WriteLine("=====================================================");
                Console.WriteLine(piece.Name);
                chessboard.ClearMarkedLegalMoves();
                chessboard.FindLegalMoves(piece);
                PrintBoardOccupiedAndLegal(chessboard);
            }

            Console.WriteLine("=====================================================");

            Console.WriteLine("Before Move");
            chessboard.MovePiece(whiteQueen, chessboard.Board[2, 4]);
            chessboard.ClearMarkedLegalMoves();
            PrintBoardOccupiedAndLegal(chessboard);

            Console.WriteLine("=====================================================");

            Console.WriteLine("Clear Board");
            chessboard.ClearBoard();
            PrintBoardOccupiedAndLegal(chessboard);

            Console.WriteLine("=====================================================");

            Console.WriteLine("New Game");
            chessboard.NewGame();
            PrintBoardOccupiedAndLegal(chessboard);

            Console.WriteLine("=====================================================");

            Console.WriteLine("Queen Moves");
            chessboard.ClearBoard();
            Queen queen = new Queen(true, chessboard.Board[4, 4]);
            Pawn  pawn  = new Pawn(false, chessboard.Board[2, 4]);

            PrintBoardOccupiedAndLegal(chessboard);

            Console.WriteLine("=====================================================");

            Console.WriteLine("Find Queen Obstruction");
        }
Example #2
0
        /** This method generates all the possible moves the player is able to make
         * It goes through each piece and their valid squares and determines if it can
         * make each move for each piece and square. It also evaluates if the piece can capture,
         * castle or en passant
         * @param a_player - The opposing player
         * @param a_board - The board the game is played on
         * @param a_previousMove - the previous move that was played
         * @author Thomas Hooper
         * @date July 2019
         */
        public void GenerateMoves(Player a_player, Board a_board, Move a_previousMove)
        {
            List <Move> moves = new List <Move>();

            foreach (Piece p in Pieces)
            {
                foreach (BoardSquare s in p.ValidSquares)
                {
                    #region Just Moving
                    if (!s.Occupied)
                    {
                        moves.Add(new Move(s, p));
                    }
                    #endregion
                    #region Capturing
                    else
                    {
                        if (a_player.Pieces.Exists(x => x.Row == s.Row && x.Column == s.Column))                        //BoardSquare Color
                        {
                            if (p.AttackingSquares.Exists(x => x.Row == s.Row && x.Column == s.Column) && s.PieceColor != p.Color)
                            {
                                Piece capturedPiece = a_player.Pieces.Find(x => x.Row == s.Row && x.Column == s.Column);
                                moves.Add(new Move(s, p, capturedPiece));
                            }
                        }
                    }
                    #endregion
                }
            }

            #region En Passant
            if (a_previousMove != null)
            {
                if (a_previousMove.MovingPiece is Pawn && (a_previousMove.MovingPiece.Row == 3 || a_previousMove.MovingPiece.Row == 4))
                {
                    if (Pieces.Exists(x => x is Pawn && x.Row == a_previousMove.MovingPiece.Row))
                    {
                        if (a_previousMove.MovingPiece.Color == Color.White && a_previousMove.MovingPiece.Row == 4)
                        {
                            if (a_previousMove.OriginalSquare.Row == 6)
                            {
                                List <Piece> pawns = Pieces.FindAll(x => x.Row == 4);
                                foreach (Piece p in pawns)
                                {
                                    if (Math.Abs(p.Column - a_previousMove.MovingPiece.Column) == 1)
                                    {
                                        BoardSquare square = a_board.ReturnSquare(a_previousMove.OriginalSquare.Row + 1, a_previousMove.OriginalSquare.Column);
                                        moves.Add(new Move(square, p)
                                        {
                                            EnPassant = true
                                        });
                                    }
                                }
                            }
                        }
                        else if (a_previousMove.MovingPiece.Color == Color.Black && a_previousMove.MovingPiece.Row == 3)
                        {
                            if (a_previousMove.OriginalSquare.Row == 1)
                            {
                                List <Piece> pawns = Pieces.FindAll(x => x.Row == 3);
                                foreach (Piece p in pawns)
                                {
                                    if (Math.Abs(p.Column - a_previousMove.MovingPiece.Column) == 1)
                                    {
                                        BoardSquare square = a_board.ReturnSquare(a_previousMove.OriginalSquare.Row - 1, a_previousMove.OriginalSquare.Column);
                                        moves.Add(new Move(square, p)
                                        {
                                            EnPassant = true
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
            }
            #endregion
            #region Castling
            King k = (King)Pieces.Find(x => x is King);
            if (!k.HasMoved && !Check)
            {
                BoardSquare s1 = new BoardSquare();
                BoardSquare s2 = new BoardSquare();
                BoardSquare s3 = new BoardSquare();
                if (Pieces.Exists(x => x is Rook && !x.HasMoved && x.Column == 7))
                {
                    Rook r = (Rook)Pieces.Find(x => x is Rook && !x.HasMoved && x.Column == 7);
                    s1 = a_board.ReturnSquare(k.Row, k.Column + 1);
                    s2 = a_board.ReturnSquare(k.Row, k.Column + 2);
                    if (!s1.Occupied && !s2.Occupied)
                    {
                        moves.Add(new Move(s2, k, r, true));
                    }
                }
                if (Pieces.Exists(x => x is Rook && !x.HasMoved && x.Column == 0))
                {
                    Rook r = (Rook)Pieces.Find(x => x is Rook && !x.HasMoved && x.Column == 0);
                    s1 = a_board.ReturnSquare(k.Row, k.Column - 1);
                    s2 = a_board.ReturnSquare(k.Row, k.Column - 2);
                    s3 = a_board.ReturnSquare(k.Row, k.Column - 3);

                    if (!s1.Occupied && !s2.Occupied && !s3.Occupied)
                    {
                        moves.Add(new Move(s2, k, r, true));
                    }
                }
            }
            #endregion
            Moves = moves;
        }