forked from sagnew/Chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
68 lines (52 loc) · 1.4 KB
/
Copy pathRook.java
File metadata and controls
68 lines (52 loc) · 1.4 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
public class Rook extends Piece{
public String color;
public Rook(String color){
this.color = color;
}
public boolean validateMove(Piece[][] board, int currentRow, int currentCol, int newRow, int newCol) {
// TODO Auto-generated method stub
//System.out.println("currentRow: " + currentRow + " currentCol: " + currentCol + " newRow: " + newRow + " newCol: " + newCol);
if(currentRow != newRow && currentCol != newCol){
//Did not move along one rank/file
return false;
}
//First I will assumed the Rook is moving along the rows.
int offset;
if(currentRow != newRow){
if(currentRow < newRow){
offset = 1;
}else{
offset = -1;
}
for(int x = currentRow + offset; x != newRow; x += offset){
//Go from currentRow to newRow, and check every space
if(board[x][currentCol] != null){
//System.out.println("1 " + x);
return false;
}
}
}
//Now do the same for columns
if(currentCol != newCol){
if(currentCol < newCol){
offset = 1;
}else{
offset = -1;
}
for(int x = currentCol + offset; x != newCol; x += offset){
//Go from currentCol to newCol, and check every space
if(board[currentRow][x] != null){
//System.out.println("2");
return false;
}
}
}
return true;
}
public String getColor(){
return this.color;
}
public String toString(){
return color.charAt(0) + "R";
}
}