-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
141 lines (117 loc) · 4.21 KB
/
Pawn.java
File metadata and controls
141 lines (117 loc) · 4.21 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**************************************************************
* Functionality for each Pawn Piece
*
* @author Richard
* @author Steven
* @version 3/13/2022
**************************************************************/
public class Pawn extends ChessPiece {
/*************************************************************
* Default constructor calls the ChessPiece Constructor
*************************************************************/
public Pawn(Player player) {
super(player);
}
/*************************************************************
* Returns the type of chesspiece
*************************************************************/
public String type() {
return "Pawn";
}
/*************************************************************
* Verifies there is no piece in the way
*************************************************************/
private boolean isForwardPositionEmpty(Move move, ChessPiece[][] board) {
if (board[move.toRow][move.toColumn] == null) {
return true;
}
return false;
}
/*************************************************************
* Checks if the pawn is moving up/down depending on the color
*************************************************************/
private boolean isPieceMovingForward(Move move, ChessPiece[][] board) {
if (super.player() == Player.BLACK
&& move.toRow - move.fromRow == 1
&& move.toColumn - move.fromColumn == 0) {
return true;
}
if (super.player() == Player.WHITE
&& move.toRow - move.fromRow == -1
&& move.toColumn - move.fromColumn == 0) {
return true;
}
return false;
}
/*************************************************************
* Checks if the pawn can move two spaces for an opener
*************************************************************/
private boolean isFirstMove2Spaces(Move move, ChessPiece[][] board) {
if (super.player() == Player.BLACK
&& move.toRow - move.fromRow == 2
&& move.toColumn - move.fromColumn == 0
&& move.fromRow == 1) {
if (board[move.toRow - 1][move.toColumn] == null) {
return true;
}
}
if (super.player() == Player.WHITE
&& move.toRow - move.fromRow == -2
&& move.toColumn - move.fromColumn == 0
&& move.fromRow == 6) {
if (board[move.toRow + 1][move.toColumn] == null) {
return true;
}
}
return false;
}
/*************************************************************
* Checks if the pawn is moving diagonally depending on the color
*************************************************************/
private boolean isPieceMovingDiagonally(Move move, ChessPiece[][] board) {
if (super.player() == Player.BLACK
&& move.toRow - move.fromRow == 1
&& Math.abs(move.toColumn - move.fromColumn) == 1) {
return true;
}
if (super.player() == Player.WHITE
&& move.toRow - move.fromRow == -1
&& Math.abs(move.toColumn - move.fromColumn) == 1) {
return true;
}
return false;
}
/*************************************************************
* Checks if selected diagonal space is empty and thus invalid
*************************************************************/
private boolean isDiagonalEmpty(Move move, ChessPiece[][] board) {
if (board[move.toRow][move.toColumn] == null) {
return true;
}
if (board[move.fromRow][move.fromColumn].player() !=
board[move.toRow][move.toColumn].player()) {
return (false);
}
if (board[move.fromRow][move.fromColumn].player() ==
board[move.toRow][move.toColumn].player()) {
return false;
}
return true;
}
/*************************************************************
* Determines if the move is valid for a pawn piece
*************************************************************/
public boolean isValidMove(Move move, ChessPiece[][] board) {
if (!super.isValidMove(move, board))
return false;
if (isFirstMove2Spaces(move, board) == true)
return true;
if (isPieceMovingForward(move, board)) {
if (isForwardPositionEmpty(move, board))
return true;
} else if (isPieceMovingDiagonally(move, board))
if (isDiagonalEmpty(move, board) == false)
return true;
return false;
}
}