-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKnight.java
More file actions
26 lines (20 loc) · 743 Bytes
/
Knight.java
File metadata and controls
26 lines (20 loc) · 743 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
import java.util.*;
class Knight extends ChessPiece {
public ArrayList<ChessCoordinate> validMoves(ChessCoordinate startPosition)
{
//Will build up list of valid co-ordinates for next move
ArrayList<ChessCoordinate> validCoordiantes = new ArrayList<ChessCoordinate>();
int[] moves = {-2, -1, 1, 2};
for (int xMove : moves) {
for (int yMove : moves) {
if (Math.abs(xMove) != Math.abs(yMove)) { //A Knight can only move 1 space in one direction and 2 in the other
if (ChessCoordinate.isValidCoordinate(startPosition.x + xMove, startPosition.y + yMove))
{
validCoordiantes.add((new ChessCoordinate((startPosition.x + xMove),(startPosition.y + yMove))));
}
}
}
}
return validCoordiantes;
}
}