forked from sagnew/Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKing.java
More file actions
58 lines (43 loc) · 1.17 KB
/
Copy pathKing.java
File metadata and controls
58 lines (43 loc) · 1.17 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
public class King extends Piece{
public String color;
public boolean hasMoved;
public boolean castled;
public King(String color){
this.color = color;
this.hasMoved = false;
this.castled = false;
}
@Override
public boolean validateMove(Piece[][] board, int currentRow, int currentCol, int newRow, int newCol) {
if(Math.abs(newRow - currentRow) > 1 || Math.abs(newCol - currentCol) > 1){
if(hasMoved){
return false;
}
//Do castling logic here
if(newCol - currentCol == 2 && currentRow == newRow){
//Castle kingside
if(board[newRow][currentCol + 1] != null || board[newRow][currentCol + 2] != null){
castled = false;
return false;
}
}else if(currentCol - newCol == 3 && currentRow == newRow){
if(board[newRow][currentCol - 1] != null || board[newRow][currentCol - 2] != null || board[newRow][currentCol - 3] != null){
castled = false;
return false;
}
}else{
castled = false;
return false;
}
castled = true;
}
//hasMoved = true;
return true;
}
public String getColor(){
return this.color;
}
public String toString(){
return color.charAt(0) + "K";
}
}