//method that moves a pawn and checks to make sure move is valid public void MovePawn(ChessPawn pawn, int verticalMotion, int horizontalMotion) { if (pawn.Color.Equals("Black", StringComparison.OrdinalIgnoreCase)) { verticalMotion = verticalMotion; } else { verticalMotion = -verticalMotion; } Console.WriteLine("Pawn entered"); if (!((ChessPawn)pawn).FirstMoveTaken) { if (verticalMotion > 2) { throw new System.InvalidOperationException("Pawns cannot move that far vertically, even on the first move."); } else if (verticalMotion < 0) { throw new System.InvalidOperationException("Pawns cannot move backwards."); } else if (horizontalMotion < -1 || horizontalMotion > 1) { throw new System.InvalidOperationException("Pawns can never move more than one space horizontally.") } else if (horizontalMotion != 0 && !Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied || Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece != null && Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece.Color.Equals(pawn.Color)) { throw new System.InvalidOperationException("Pawns cannot move horizontally without attacking a piece of the opposite color."); } else { if (Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition +])
/// <summary> /// Allows a pawn to move /// </summary> /// <param name="pawn">Requires an object of the pawn class</param> /// <param name="verticalMotion">How far a pawn is being moved vertically</param> /// <param name="horizontalMotion">How far a pawn is being moved horizontally</param> public void MovePawn(ChessPawn pawn, int verticalMotion, int horizontalMotion) { //contains logic if a pawn is taking its first move if (!((ChessPawn)pawn).FirstMoveTaken) { //ensures the pawn does not move more than 2 spaces forward if (verticalMotion > 2) { throw new System.InvalidOperationException("Pawns cannot move that far vertically, even on the first move."); } //ensures the pawn does not move backwards else if (verticalMotion < 0) { throw new System.InvalidOperationException("Pawns cannot move backwards."); } //makes the game more user friendly, and flips the sign of vertical motion if white pieces are used if (pawn.Color.Equals("Black", StringComparison.OrdinalIgnoreCase)) { verticalMotion = verticalMotion; } else { verticalMotion = -verticalMotion; } //ensures the user is not trying to move the pawn too far horizontally if (horizontalMotion < -1 || horizontalMotion > 1) { throw new System.InvalidOperationException("Pawns can never move more than one space horizontally."); } //ensures that the user cannot move a pawn diagonally without attacking another piece else if (horizontalMotion != 0 && !Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied || Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece != null && Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece.Color.Equals(pawn.Color)) { throw new System.InvalidOperationException("Pawns cannot move horizontally without attacking a piece of the opposite color."); } //Logic for a successful move/attack else { if (Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied && horizontalMotion == 1 || Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied && horizontalMotion == -1) { Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece.Defeat( Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece); } Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece = Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece; Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece = null; Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].IsOccupied = false; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied = true; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece.HorizontalPosition = pawn.HorizontalPosition + horizontalMotion; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition].ChessPiece.VerticalPosition = pawn.VerticalPosition + verticalMotion; } } ///contains logic if a pawn is taking any move after its first else { if (verticalMotion == 2) { throw new System.InvalidOperationException("Pawns can only move that far vertically on the first move."); } else if (verticalMotion > 1) { throw new System.InvalidOperationException("Pawns cannot move that far forward."); } if (pawn.Color.Equals("Black", StringComparison.OrdinalIgnoreCase)) { verticalMotion = verticalMotion; } else { verticalMotion = -verticalMotion; } if (horizontalMotion < -1 || horizontalMotion > 1) { throw new System.InvalidOperationException("Pawns can never move more than one space horizontally."); } else { if (Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied && horizontalMotion == 1 || Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied && horizontalMotion == -1) { Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece.Defeat( Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece); } Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece = Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece; Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].ChessPiece = null; Chessboard[pawn.VerticalPosition, pawn.HorizontalPosition].IsOccupied = false; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].IsOccupied = true; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition + horizontalMotion].ChessPiece.HorizontalPosition = pawn.HorizontalPosition + horizontalMotion; Chessboard[pawn.VerticalPosition + verticalMotion, pawn.HorizontalPosition].ChessPiece.VerticalPosition = pawn.VerticalPosition + verticalMotion; } } }