-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRook.java
More file actions
64 lines (54 loc) · 1.99 KB
/
Rook.java
File metadata and controls
64 lines (54 loc) · 1.99 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
import java.util.Objects;
public class Rook extends Piece{
public Rook(String colour) {
super(colour);
}
public static boolean isStraightMove(Tile from, Tile to){
return(from.getRow() == to.getRow() || from.getColumn() == to.getColumn());
}
public static boolean isPieceInWayHorizontal(Tile from, Tile to){
int step = Integer.compare(to.getColumn(), from.getColumn());
int diffCol = to.getColumn() - from.getColumn();
int rowIndex = from.getRow() -1;
int columnIndex = from.getColumn() - 'A';
for(int i=step;i<diffCol;i+=step){
if (Game.board[rowIndex][columnIndex+i].getPiece() != null) {
return true;
}
}
return false;
}
public static boolean isPieceInWayVertical(Tile from, Tile to){
int step = Integer.compare(to.getRow(), from.getRow());
int diffRow = to.getRow() - from.getRow();
int rowIndex = from.getRow() -1;
int columnIndex = from.getColumn() - 'A';
for(int i=step;i<diffRow;i+=step){
if (Game.board[rowIndex+i][columnIndex].getPiece() != null) {
return true;
}
}
return false;
}
public static boolean isPathClearStraight(Tile from, Tile to){
return !isPieceInWayHorizontal(from, to)
&& !isPieceInWayVertical(from, to);
}
public static boolean isEnemyOrEmpty(Tile from, Tile to){
return to.getPiece() == null || !Objects.equals(to.getPiece().getColour(), from.getPiece().getColour());
}
@Override
public boolean isLegalMove(Tile targetTile) {
Tile currentTile = this.getTile();
return isStraightMove(currentTile,targetTile)
&& isPathClearStraight(currentTile,targetTile)
&& isEnemyOrEmpty(currentTile, targetTile);
}
@Override
public String getName() {
if(this.getColour().equals("white")){
return "wR";
}
return "bR";
}
}