forked from sagnew/Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPawn.java
More file actions
105 lines (87 loc) · 2.85 KB
/
Copy pathPawn.java
File metadata and controls
105 lines (87 loc) · 2.85 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
public class Pawn extends Piece {
public String color;
public boolean hasMoved;
//Is the piece allowed to be taken via en passante?
public boolean ep_able;
public Pawn(String color){
this.color = color;
this.hasMoved = false;
}
public boolean validateMove(Piece[][] board, int currentRow, int currentCol, int newRow, int newCol) {
if(color.equals("white")){
if(currentRow > newRow){
return false;
}
}else{
if(newRow > currentRow){
return false;
}
}
if(currentCol == newCol){
//Not taking a piece
if(color.equals("white")){
if(board[currentRow + 1][currentCol] != null){
return false;
}
}else{
if(board[currentRow - 1][currentCol] != null){
return false;
}
}
if(Math.abs(newRow - currentRow) > 2){
return false;
}else if(Math.abs(newRow - currentRow) == 2){
//Advancing two spaces at beginning
if(hasMoved){
return false;
}
if(color.equals("white")){
if(board[currentRow + 2][currentCol] != null){
return false;
}
}else{
if(board[currentRow - 2][currentCol] != null){
return false;
}
}
//En passante
if(newCol + 1 < 8){
if(board[newRow][newCol + 1] != null){
if(board[newRow][newCol + 1].getClass().isInstance(new Pawn("white"))){
ep_able = true;
}
}
}else if(newCol - 1 > 0){
if(board[newRow][newCol - 1] != null){
if(board[newRow][newCol - 1].getClass().isInstance(new Pawn("white"))){
ep_able = true;
}
}
}
}
}else{
//Taking a piece
if(Math.abs(newCol - currentCol) != 1 || Math.abs(newRow - currentRow) != 1){
return false;
}
if(board[newRow][newCol] == null){
/*if(newRow - 1 > 0){
if(newCol - 1 > 0){
if(board[newRow - 1][newCol - 1] != null){
if(){
}
}
}
}*/
return false;
}
}
return true;
}
public String getColor(){
return this.color;
}
public String toString(){
return color.charAt(0) + "p";
}
}