-
Notifications
You must be signed in to change notification settings - Fork 0
/
Piece.java
71 lines (65 loc) · 1.88 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
71
/* Skeleton Copyright (C) 2015, 2020 Paul N. Hilfinger and the Regents of the
* University of California. All rights reserved. */
package loa;
/** A Piece denotes the contents of a square, or identifies one side
* (Black or White) of a game.
* @author P. N. Hilfinger
*/
enum Piece {
/** The names of the pieces. EMP indicates an empty square. The
* arguments give names to the piece colors. */
BP, WP, EMP, MP;
/** Returns the full name of this piece (black, white, or -). */
String fullName() {
switch (this) {
case BP:
return "black";
case WP:
return "white";
default:
return "-";
}
}
/** Returns the one-character denotation of this piece on the standard
* text display of a board. */
String abbrev() {
switch (this) {
case BP:
return "b";
case WP:
return "w";
default:
return "-";
}
}
/** Return player (white or black piece) for which .fullName()
* returns NAME. Also returns EMP for NAME=="". */
static Piece playerValueOf(String name) {
switch (name.toLowerCase()) {
case "black":
return BP;
case "white":
return WP;
case "-": case "":
return EMP;
default:
throw new IllegalArgumentException("piece name unknown");
}
}
/** Returns Piece with my opposing color (null for EMP). */
Piece opposite() {
switch (this) {
case BP:
return WP;
case WP:
return BP;
default:
return null;
}
}
/** The textual representation of this piece. */
private String _fullName;
/** The one-character abbreviation of this piece, used in printed
* representations ot the board. */
private String _abbrev;
}