-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPawn.java#backup
72 lines (62 loc) · 3.08 KB
/
Pawn.java#backup
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
/**
* Write a description of class Pawn here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pawn extends ChessPiece
{
public Pawn(Player player) {
super(player);
}
public String type() {
return "Pawn";
}
public boolean isValidMove(Move move,IChessPiece[][] board) {
if (!super.isValidMove(move,board))
return false;
boolean valid = false;
if (board[move.fromRow][move.fromColumn].player() == Player.BLACK) {
// checks to see if the piece is being moved diagonally down and over one space
if (((move.toRow - 1 == move.fromRow) && (move.toColumn + 1 == move.fromColumn))
|| ((move.toRow - 1 == move.fromRow) && (move.toColumn - 1 == move.fromColumn)))
// checks to see if this piece is taking another player's piece
if (board[move.toRow][move.toColumn] != null)
valid = true;
// checks to see if this piece is a black pawn in row 2
if (move.fromRow == 1)
// checks to see if this piece is being moved 2 spaces forward
if ((move.toColumn == move.fromColumn) && (move.toRow == 3))
// checks to see if this piece has a clear path
if ((board[2][move.fromColumn]) == null && (board[3][move.fromColumn]) == null)
valid = true;
// checks to see if this piece is being moved down one row
if ((move.toColumn == move.fromColumn) && (move.toRow - 1 == move.fromRow))
// checks to see if this piece has a clear path
if ((board[move.toRow][move.toColumn]) == null)
valid = true;
}
// checks to see if it is white's turn
else if (board[move.fromRow][move.fromColumn].player() == Player.WHITE) {
// checks to see if the piece is being moved diagonally up and over one space
if (((move.toRow + 1 == move.fromRow) && (move.toColumn + 1 == move.fromColumn))
|| ((move.toRow + 1 == move.fromRow) && (move.toColumn - 1 == move.fromColumn)))
// checks to see if this piece is taking another player's piece
if (board[move.toRow][move.toColumn] != null)
valid = true;
// checks to see if this piece is a white pawn in row 7
if (move.fromRow == 6)
// checks to see if this piece is being moved 2 spaces forward
if ((move.toColumn == move.fromColumn) && (move.toRow == 4))
// checks to see if this piece has a clear path
if ((board[5][move.fromColumn]) == null && (board[4][move.fromColumn]) == null)
valid = true;
// checks to see if this piece is being moved up one row
if ((move.toColumn == move.fromColumn) && (move.toRow + 1 == move.fromRow))
// checks to see if this piece has a clear path
if ((board[move.toRow][move.toColumn]) == null)
valid = true;
}
return valid;
}
}