-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnight.java
More file actions
57 lines (55 loc) · 1.46 KB
/
Copy pathKnight.java
File metadata and controls
57 lines (55 loc) · 1.46 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
import java.awt.Color;
import java.util.*;
/**
* Represents a knight
* in chess.
*
* @author Linda Zeng
* @version 4.9.23
*/
public class Knight extends Piece
{
/**
* Constructs a knight
* with given color and filename.
* It is valued at 3.
*
* @param col color of knight
* @param fileName file with image of knight
*/
public Knight(Color col, String filename)
{
super(col, filename, 3);
}
/**
* Indicates which locations
* the knight can move to.
* It can move in Ls any direction
*
* @return an ArrayList of
* locations this can
* move to
*/
public ArrayList<Location> destinations()
{
ArrayList<Location> res = new ArrayList<Location>();
int curR = getLocation().getRow();
int curC = getLocation().getCol();
res.add(new Location(curR + 2, curC + 1));
res.add(new Location(curR + 2, curC - 1));
res.add(new Location(curR - 2, curC + 1));
res.add(new Location(curR - 2, curC - 1));
res.add(new Location(curR + 1, curC + 2));
res.add(new Location(curR - 1, curC + 2));
res.add(new Location(curR + 1, curC - 2));
res.add(new Location(curR -1, curC - 2));
for (int i = res.size() - 1; i >= 0; i--)
{
if (!isValidDestination(res.get(i)))
{
res.remove(i);
}
}
return res;
}
}