-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
36 lines (27 loc) · 976 Bytes
/
Knight.java
File metadata and controls
36 lines (27 loc) · 976 Bytes
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
import java.util.Objects;
import static java.lang.Math.abs;
public class Knight extends Piece{
public Knight(String colour) {
super(colour);
}
public static boolean isLShapeMove(Tile from, Tile to){
int diffRow = abs(to.getRow() - from.getRow());
int diffCol = abs(to.getColumn() - from.getColumn());
return (diffRow == 2 && diffCol == 1) || (diffRow == 1 && diffCol == 2);
}
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 isLShapeMove(currentTile,targetTile) && isEnemyOrEmpty(currentTile,targetTile);
}
@Override
public String getName() {
if(this.getColour().equals("white")){
return "wN";
}
return "bN";
}
}