Ejemplo n.º 1
0
        private PlayerMove GetMoveFromPlayer(IUserInput input, IUserResponse userResponse, Player player, PieceColor currentColor)
        {
            bool isValidSelection = false;
            var  move             = player.MakeMove(input);

            while (!isValidSelection)
            {
                if (ChessBoard.IsLegalBoardPosition(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate) &&
                    ChessBoard.IsPieceAt(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate))
                {
                    if (ChessBoard.PieceAt(move.PieceSelectionXCoordinate, move.PieceSelectionYCoordinate).PieceColor == currentColor)
                    {
                        isValidSelection = true;
                    }
                    else
                    {
                        userResponse.BadPieceSelection("You must select your own piece to move.");
                        move = WhitePlayer.MakeMove(input);
                    }
                }
                else
                {
                    userResponse.BadPieceSelection("You must select a piece at a valid location.");
                    move = WhitePlayer.MakeMove(input);
                }
            }

            return(move);
        }
Ejemplo n.º 2
0
        /*  IsLegalMove() for the king also calls into the ChessBoard to see if
         *  making that move would put it in check.
         */

        public MovementResult IsLegalMove(int newX, int newY)
        {
            if (!ChessBoard.IsLegalBoardPosition(newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That board position does not exist."
                       }
            }
            ;

            if ((Math.Abs(newX - XCoordinate) > 1) || (Math.Abs(newY - YCoordinate) > 1))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That is not a valid move for this piece."
                       }
            }
            ;

            if (!ChessBoard.IsPositionSafeForKing(this, newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "A king cannot move into check."
                       }
            }
            ;

            if (ChessBoard.IsPieceAt(newX, newY))
            {
                if (ChessBoard.PieceAt(newX, newY).PieceColor == PieceColor)
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a piece of the same color already there."
                           }
                }
            }
            ;

            return(new MovementResult()
            {
                WasSuccessful = true,
                ReasonForFailure = ""
            });
        }
Ejemplo n.º 3
0
        private TurnResult TakeTurn(PlayerMove playerMove, IUserInput input, IUserResponse userResponse, Player player, PieceColor pieceColor)
        {
            var turnResult = ChessBoard
                             .PieceAt(playerMove.PieceSelectionXCoordinate, playerMove.PieceSelectionYCoordinate)
                             .Move(playerMove.PieceDestinationXCoordinate, playerMove.PieceDestinationYCoordinate);

            userResponse.ReturnMoveInformation(turnResult, ChessBoard);

            while (!turnResult.TurnCompleted)
            {
                playerMove = GetMoveFromPlayer(input, userResponse, player, pieceColor);
                turnResult = ChessBoard
                             .PieceAt(playerMove.PieceSelectionXCoordinate, playerMove.PieceSelectionYCoordinate)
                             .Move(playerMove.PieceDestinationXCoordinate, playerMove.PieceDestinationYCoordinate);
                userResponse.ReturnMoveInformation(turnResult, ChessBoard);
            }

            return(turnResult);
        }
Ejemplo n.º 4
0
        /*  Every piece, for the most part, goes through a similar set of
         *  steps to determine if it can make a particular move.
         *
         *  1. Is the position even a space on the board?
         *  2. Is there a friendly piece there?
         *  3. Does it follow the movement rules of the piece?
         *
         *  If any of those conditions are unfavorable, an unsuccessful
         *  movement result is sent back to the caller with a reason on
         *  why that move could not be made.
         */

        public MovementResult IsLegalMove(int newX, int newY)
        {
            if (!ChessBoard.IsLegalBoardPosition(newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That board position does not exist."
                       }
            }
            ;

            if (ChessBoard.IsPieceAt(newX, newY))
            {
                if (ChessBoard.PieceAt(newX, newY).PieceColor == PieceColor)
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a piece of the same color already there."
                           }
                }
            }
            ;

            if (!(IsLegalDiagonal(newX, newY) ^ IsLegalPerpendicular(newX, newY)))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That is not a valid move for this piece."
                       }
            }
            ;

            return(new MovementResult()
            {
                WasSuccessful = true,
                ReasonForFailure = ""
            });
        }
Ejemplo n.º 5
0
        public MovementResult IsLegalMove(int newX, int newY)
        {
            if (!ChessBoard.IsLegalBoardPosition(newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That board position does not exist."
                       }
            }
            ;

            if (ChessBoard.IsPieceAt(newX, newY))
            {
                if (ChessBoard.PieceAt(newX, newY).PieceColor == PieceColor)
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a piece of the same color already there."
                           }
                }
                ;
            }

            var xDirection = newX - XCoordinate;
            var yDirection = newY - YCoordinate;

            if (Math.Abs(xDirection) != Math.Abs(yDirection))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That is not a valid move for this piece."
                       }
            }
            ;

            var xSign = xDirection / Math.Abs(xDirection);
            var ySign = yDirection / Math.Abs(yDirection);

            int x = XCoordinate + xSign, y = YCoordinate + ySign;

            while ((x != newX) && (y != newY))
            {
                if (ChessBoard.IsPieceAt(x, y))
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a piece in the way of that movement."
                           }
                }
                ;

                x += xSign;
                y += ySign;
            }

            return(new MovementResult()
            {
                WasSuccessful = true,
                ReasonForFailure = ""
            });
        }
    }
}
Ejemplo n.º 6
0
        public override MovementResult IsLegalMove(int newX, int newY)
        {
            if (!ChessBoard.IsLegalBoardPosition(newX, newY))
            {
                return new MovementResult()
                       {
                           WasSuccessful    = false,
                           ReasonForFailure = "That board position does not exist."
                       }
            }
            ;

            if (ChessBoard.IsPieceAt(newX, newY))
            {
                if (ChessBoard.PieceAt(newX, newY).PieceColor == PieceColor.White)
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = false,
                               ReasonForFailure = "There is a friendly piece at that position."
                           }
                }
            }
            ;

            if ((newX == XCoordinate && newY == YCoordinate + 1) && !ChessBoard.IsPieceAt(newX, newY))
            {
                var movementResult = new MovementResult()
                {
                    WasSuccessful    = true,
                    ReasonForFailure = ""
                };

                if (newY == 7)
                {
                    movementResult.Flags.Add(Flag.PawnPromotion);
                }

                return(movementResult);
            }

            if ((newX == XCoordinate && newY == YCoordinate + 2) &&
                !ChessBoard.IsPieceAt(XCoordinate, YCoordinate + 1) &&
                !ChessBoard.IsPieceAt(XCoordinate, YCoordinate + 2) &&
                !HasMoved)
            {
                HasDoubleSteppedLastTurn = true;
                return(new MovementResult()
                {
                    WasSuccessful = true,
                    ReasonForFailure = ""
                });
            }

            if ((newX == XCoordinate - 1 || newX == XCoordinate + 1) && newY == YCoordinate + 1)
            {
                if (ChessBoard.IsPieceAt(newX, newY))
                {
                    return new MovementResult()
                           {
                               WasSuccessful    = true,
                               ReasonForFailure = ""
                           }
                }
                ;
                else if (ChessBoard.IsPieceAt(newX, newY - 1))
                {
                    var piece = ChessBoard.PieceAt(newX, newY - 1);

                    if (piece is BlackPawn)
                    {
                        var pawn = (BlackPawn)piece;
                        if (pawn.HasDoubleSteppedLastTurn)
                        {
                            ChessBoard.EnPassantKill(pawn);
                            return(new MovementResult()
                            {
                                WasSuccessful = true,
                                ReasonForFailure = ""
                            });
                        }
                    }
                }
            }

            return(new MovementResult()
            {
                WasSuccessful = false,
                ReasonForFailure = "That was not a valid move for this piece."
            });
        }
    }