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); }
private bool IsLegalDiagonal(int newX, int newY) { var xDirection = newX - XCoordinate; var yDirection = newY - YCoordinate; if (Math.Abs(xDirection) != Math.Abs(yDirection)) { return(false); } 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(false); } x += xSign; y += ySign; } return(true); }
/* 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 = "" }); }
/* 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 = "" }); }
private bool IsLegalPerpendicular(int newX, int newY) { var xDirection = newX - XCoordinate; var yDirection = newY - YCoordinate; if (!((xDirection == 0) ^ (yDirection == 0))) { return(false); } int xSign = 0, ySign = 0; if (xDirection == 0) { ySign = yDirection / Math.Abs(yDirection); } else if (yDirection == 0) { xSign = xDirection / Math.Abs(xDirection); } int x = XCoordinate + xSign, y = YCoordinate + ySign; while ((x != newX) || (y != newY)) { if (ChessBoard.IsPieceAt(x, y)) { return(false); } x += xSign; y += ySign; } return(true); }
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 = "" }); } } }
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." }); } }