Example #1
0
 /** This takes a string and returns the Player's piece that's name equals the string
  * @param a_pieceString - The string we are matching to a piece
  * @returns The piece that's name matches the string
  * @author Thomas Hooper
  * @date August 2019
  */
 public Piece ReturnPieceFromString(string a_pieceString)
 {
     return(Pieces.Find(x => x.Name.Equals(a_pieceString)));
 }
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;
        }