/// <summary> /// Moves the piece in the Chess board /// </summary> /// <param name="movementType"></param> /// <param name="newX"></param> /// <param name="newY"></param> public void Move(MovementType movementType, int newX, int newY) { //first determine if the new position is valid //I am using the chessboard if (_chessBoard.IsLegalBoardPosition(newX, newY)) { //verify that the x move is correct if (!VerifyXMove(newX, this.XCoordinate)) { //don't do the move return; } //verify that the Y move is correct if (!VerifyYMove(newY, this.YCoordinate)) { //don't do the move return; } this.XCoordinate = newX; this.YCoordinate = newY; _chessBoard.Add(this, this.XCoordinate, this.YCoordinate, this.PieceColor); } else { string result = string.Format("Not an acceptable move X: {0} Y: {1}", newX, newY); } // throw new NotImplementedException("Need to implement Pawn.Move()"); }
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); }
//Filter moves down to ones where an opposing piece couldnt move to. //So we dont have to check the state of the board on the next move and then reset it unless the king tries to capture. private bool CanMove(Tuple <int, int> movement) { var isLegal = ChessBoard.IsLegalBoardPosition(movement.Item1, movement.Item2) && !IsLocationBlocked(movement); var moveRestrictions = OpposingColor == PieceColor.Black ? ChessBoard.BlackMoveLocations : ChessBoard.WhiteMoveLocations; var positionAvailable = !moveRestrictions.Contains(movement); return(isLegal && positionAvailable); }
/* 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 = "" }); }
protected IEnumerable <Tuple <int, int> > RowDirections(int rowDir) { for (int i = 1, nextRow = NextPosition(CurrentRow, i, rowDir); //var declarations ChessBoard.IsLegalBoardPosition(nextRow, CurrentColumn) && !IsLocationBlocked(nextRow, CurrentColumn); //terminating conditions i++, nextRow = NextPosition(CurrentRow, i, rowDir)) //iterators { var nextLocation = new Tuple <int, int>(nextRow, CurrentColumn); yield return(nextLocation); if (CaptureableLocation.Equals(nextLocation)) { yield break; } } }
private IEnumerable <Tuple <int, int> > ColumnDirections(int colDir) { for (int i = 1, nextCol = NextPosition(CurrentColumn, i, colDir); //var declarations ChessBoard.IsLegalBoardPosition(CurrentRow, nextCol) && !IsLocationBlocked(CurrentRow, nextCol); //terminating conditions i++, nextCol = NextPosition(CurrentColumn, i, colDir)) //iterators { var nextLocation = new Tuple <int, int>(CurrentRow, nextCol); yield return(nextLocation); if (CaptureableLocation.Equals(nextLocation)) { yield break; } } }
/* 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 CanMove(Tuple <int, int> move) { return(ChessBoard.IsLegalBoardPosition(move.Item1, move.Item2) && !IsLocationBlocked(move)); }
public void _005_the_playing_board_should_know_that_X_equals_0_and_Y_equals_0_is_a_valid_board_position() { var isValidPosition = _chessBoard.IsLegalBoardPosition(0, 0); Assert.That(isValidPosition, Is.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." }); } }
/// <summary> /// Moves the piece in the Chess board /// </summary> /// <param name="movementType"></param> /// <param name="newX"></param> /// <param name="newY"></param> public override void Move(MovementType movementType, int newX, int newY) { Logger.LogMessage(string.Format("Pawn at current location of {0},{1} is doing a movement type of {2} to {3}, {4}", XCoordinate, YCoordinate, movementType.ToString(), newX, newY), LogLevel.info); try { //first determine if the new position is valid //I am using the chessboard method to determine if the new X and Y are located on the //chess board if (_chessBoard.IsLegalBoardPosition(newX, newY)) { //determine legal moves for move vs capture switch (movementType) { case MovementType.Move: //verify that the x move is correct if (!VerifyXMove(newX)) { Logger.LogMessage("the new x coordinate is incorrect " + newX, LogLevel.error); //don't do the move return; } //verify that the Y move is correct if (!VerifyYMove(newY)) { Logger.LogMessage("the new y coordinate is incorrect " + newY, LogLevel.error); //don't do the move return; } break; //in this case Pawns can only move diagonally case MovementType.Capture: if (!VerifyDiagonalMove(newX, newY)) { Logger.LogMessage("the new x " + newX + " or y coordinate is incorrect " + newY, LogLevel.error); return; } break; case MovementType.Special: if (!VerifySpecialMove(newX, newY)) { Logger.LogMessage("the new x " + newX + " or y coordinate is incorrect " + newY, LogLevel.error); return; } break; default: break; } this.XCoordinate = newX; this.YCoordinate = newY; _chessBoard.Add(this, this.XCoordinate, this.YCoordinate, this.PieceColor); } else { string result = string.Format("Not an acceptable move X: {0} Y: {1}", newX, newY); Logger.LogMessage(result, LogLevel.error); } } catch (Exception ex) { Logger.LogMessage("An exception occurred" + ex.Message, LogLevel.error); } }