-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKnight.java
More file actions
47 lines (40 loc) · 1.68 KB
/
Knight.java
File metadata and controls
47 lines (40 loc) · 1.68 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
package Chess;
import java.text.DecimalFormat;
public class Knight extends Piece{
public Knight(int color, Square location) {
super(color,location);
}
//Kinght can move only with a "L" shape. The distance of the target has to be the same for each possible targetLocation.
@Override
public boolean canMove(String to) {
boolean validMove = false;
Square targetLocation = location.getBoard().getSquareAt(to);
int [] coordLoc = {location.row,location.col};
int [] coordTar = {targetLocation.row, targetLocation.col};
int locRow = coordLoc[0];
int locCol = coordLoc[1];
int tarRow = coordTar[0];
int tarCol = coordTar[1];
double distance = Math.sqrt(Math.pow((tarRow - locRow),2) + Math.pow((tarCol - locCol),2));
// DecimalFormat converts distance to string to control with if block.
String dist = new DecimalFormat("##.##").format(distance);
// 2.23607 is a constant for movement of Knight. Target location of knight must be away from 2.23607 unit.
String str224 = new DecimalFormat("##.##").format(2.23607);
if(dist.equals(str224)){
Square [] between = location.getBoard().getSquaresBetweenForKnight(location,targetLocation);
for (Square square : between){
validMove = square.isEmpty();
}
if(!validMove){
if(targetLocation.getPiece() != null && targetLocation.getPiece().isEnemy(this))
validMove = true;
}
return validMove;
}
return false;
}
@Override
public String toString() {
return color == 0 ? "N":"n";
}
}