/
Piece.java
70 lines (54 loc) · 1.33 KB
/
Piece.java
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
65
66
67
68
69
70
import java.util.*;
import java.io.*;
public abstract class Piece {
protected String color;
protected Position currentPosition;
protected List<Position> nextPositions;
protected Piece(String color) {
this.color = color;
nextPositions = new ArrayList<Position>();
}
protected Piece(String color, int x, int y) {
this(color);
currentPosition = new Position(x, y);
}
protected Piece(String color, int x) {
this(color);
initializePosition(x);
}
protected void initializePosition(int x) {
int y;
if (color == "white")
y = 0;
else
y = 7;
currentPosition = new Position(x, y);
}
public String getColor() {
return color;
}
public Position getPosition() {
return currentPosition;
}
public Position getNextPosition(int index) {
if (index < nextPositions.size())
return nextPositions.get(index);
else
return null;
}
protected void addNextPosition(Position p) {
nextPositions.add(p);
}
protected void addNextPosition(int x, int y) {
Position pos = new Position(x, y);
if (pos.valid())
addNextPosition(pos);
}
public void moveTo(Position pos) {
currentPosition = pos;
nextPositions.clear();
}
public abstract void findNextPositions();
public abstract String getName();
public abstract String getRep();
}