-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.java
42 lines (33 loc) · 1.26 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
import java.util.*;
abstract public class Piece {
public static HashMap<Character, PieceFactory> pf_map = new HashMap<>();
protected Color color;
public static void registerPiece (PieceFactory pf) {
Character pf_key = pf.symbol();
pf_map.put(pf_key, pf);
// throw new UnsupportedOperationException();
}
public static Piece createPiece(String name) {
// check length
if(name.length()!=2)
throw new UnsupportedOperationException();
Character pf_key = name.charAt(1);
Color color = Helper.getColor(name.charAt(0)); // check & get color
// check the name format
if(!pf_map.containsKey(pf_key)){
throw new UnsupportedOperationException();
}
return pf_map.get(pf_key).create(color);
// throw new UnsupportedOperationException();
}
public Color color() {
// You should write code here and just inherit it in
// subclasses. For this to work, you should know
// that subclasses can access superclass fields.
if (color==null)
throw new UnsupportedOperationException();
return color;
}
abstract public String toString();
abstract public List<String> moves(Board b, String loc);
}