-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
90 lines (80 loc) · 2.86 KB
/
Copy pathPawn.java
File metadata and controls
90 lines (80 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Bishop.java
*
* @author Deep Patel
*
* REMARKS: This is a class which stores information about the pawn
* piece and how it moves, which is one square at a time in the forward direction,
* and can move diagonally one square to capture a piece. It also has a method for pawn promotion
* which occurs when the pawn reaches the other side of the board so that the user can promote
* it to a new piece
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Pawn extends Piece {
//Instance Variables
private boolean turn;
//Constructor
public Pawn(boolean myPiece) {
super(myPiece);
}
/**
* This method is used to check if the move made by the pawn piece is a valid
* move for the piece
*
* Parameters:
* Board board - The method take in the current state of the board to validate the move
* Move move - The method takes in the move being made
*
* Returns a boolean to indicate whether the move was valid
*/
public boolean isValidMove(Board board, Move move) {
int direction;
if (turn) {
direction = -1;
} else {
direction = 1;
}
if (move.getToCol() == move.getFromCol()) { // Moving straight
if (move.getToRow() - move.getFromRow() == direction) {
return board.getSymbol(move.getToRow() - 1, move.getToCol() - 1) == ' '; // if destination is empty
}
} else if (Math.abs(move.getToCol() - move.getFromCol()) == 1
&& move.getToRow() - move.getFromRow() == direction) { // Capturing diagonally
return Character.isUpperCase(board.getSymbol(move.getToRow() - 1, move.getToCol() - 1)) != turn; // if capturing opponent's piece
}
return false;
}
//Set turn to whose turn it is
public void setTurn(boolean t) {
turn = t;
}
/**
* This method is used to promote a pawn once it reaches the other side of the board. It prompts the
* user to pick a new piece to promote the pawn to
*
* Returns an int of the piece you want to promote to
*/
public int promotePawn() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
int choice = -1;
System.out.println(
"Your pawn is ready to promote. Please enter the desired type of piece: 0 for Queen, 1 for Bishop, 2 for Rook, 3 for Knight");
try {
s = br.readLine();
choice = Integer.parseInt(s);
} catch (IOException e) {
System.out.println("Error");
}
if (turn) {
if (choice < 0 || choice > 3) {
System.out.println("Enter valid number for promotion: ");
choice = promotePawn();
}
} else {
}
return choice;
}
}