예제 #1
0
        /* TODO: Still not sure where to implement it, but I may need methods:
         * TODO: bool CanClaimDraw + bool ClaimDraw + OfferDraw
         */

        /// <summary>Gets the valid moves of the given <see cref="ChessGame"/>.</summary>
        /// <param name="board">The <see cref="ChessGame"/> that you want to get its valid moves.</param>
        /// <returns>Returns a list of the valid moves.</returns>
        public static List <Move> GetValidMoves(ChessGame board)
        {
            Player player     = board.WhoseTurn;
            var    validMoves = new List <Move>();

            IEnumerable <Square> playerOwnedSquares = s_allSquares.Where(sq => board[sq]?.Owner == player);

            Square[] nonPlayerOwnedSquares = s_allSquares.Where(sq => board[sq]?.Owner != player).ToArray(); // Converting to array to avoid "Possible multiple enumeration" as suggested by ReSharper.

            foreach (Square playerOwnedSquare in playerOwnedSquares)
            {
                validMoves.AddRange(nonPlayerOwnedSquares
                                    .Select(nonPlayerOwnedSquare => new Move(playerOwnedSquare, nonPlayerOwnedSquare, player))
                                    .Where(move => ChessGame.IsValidMove(move, board)));
            }

            return(validMoves);
        }
예제 #2
0
        /// <summary>Gets the valid moves of the given <see cref="ChessGame"/> that has a specific given source <see cref="Square"/>.</summary>
        /// <param name="source">The source <see cref="Square"/> that you're looking for its valid moves.</param>
        /// <param name="board">The <see cref="ChessGame"/> that you want to get its valid moves from the specified square.</param>
        /// <returns>Returns a list of the valid moves that has the given source square.</returns>
        ///
        public static List <Move> GetValidMovesOfSourceSquare(Square source, ChessGame board)
        {
            var   validMoves = new List <Move>();
            Piece piece      = board[source];

            if (piece == null || piece.Owner != board.WhoseTurn)
            {
                return(validMoves);
            }

            Player player = piece.Owner;

            Square[] nonPlayerOwnedSquares = s_allSquares.Where(sq => board[sq]?.Owner != player).ToArray();

            validMoves.AddRange(nonPlayerOwnedSquares
                                .Select(nonPlayerOwnedSquare => new Move(source, nonPlayerOwnedSquare, player, PawnPromotion.Queen)) // If promoteTo is null, valid pawn promotion will cause exception. Need to implement this better and cleaner in the future.
                                .Where(move => ChessGame.IsValidMove(move, board)));
            return(validMoves);
        }
예제 #3
0
        /* TODO: Still not sure where to implement it, but I may need methods:
         * TODO: bool CanClaimDraw + bool ClaimDraw + OfferDraw
         */

        /// <summary>Gets the valid moves of the given <see cref="ChessGame"/>.</summary>
        /// <param name="board">The <see cref="ChessGame"/> that you want to get its valid moves.</param>
        /// <returns>Returns a list of the valid moves.</returns>
        public static List <Move> GetValidMoves(ChessGame board)
        {
            // Although nullable is enabled and board is non-nullable ref type, this check is needed
            // because this is a public method that can be used by an application that doesn't have
            // nullable enabled.
            _ = board ?? throw new ArgumentNullException(nameof(board));


            Player player     = board.WhoseTurn;
            var    validMoves = new List <Move>();

            IEnumerable <Square> playerOwnedSquares = s_allSquares.Where(sq => board[sq.File, sq.Rank]?.Owner == player);

            Square[] nonPlayerOwnedSquares = s_allSquares.Where(sq => board[sq.File, sq.Rank]?.Owner != player).ToArray(); // Converting to array to avoid "Possible multiple enumeration" as suggested by ReSharper.

            foreach (Square playerOwnedSquare in playerOwnedSquares)
            {
                validMoves.AddRange(nonPlayerOwnedSquares
                                    .Select(nonPlayerOwnedSquare => new Move(playerOwnedSquare, nonPlayerOwnedSquare, player))
                                    .Where(move => ChessGame.IsValidMove(move, board)));
            }

            return(validMoves);
        }