-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceColor.java
75 lines (62 loc) · 1.72 KB
/
PieceColor.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
72
73
74
75
/* Skeleton code copyright (C) 2008, 2022 Paul N. Hilfinger and the
* Regents of the University of California. Do not distribute this or any
* derivative work without permission. */
package ataxx;
import static ataxx.GameException.*;
/** Describes the classes of Piece on an Ataxx board.
* @author P. N. Hilfinger
*/
enum PieceColor {
/** EMPTY: no piece.
* BLOCKED: square contains a block.
* RED, BLUE: piece colors. */
EMPTY, BLOCKED,
RED {
@Override
PieceColor opposite() {
return BLUE;
}
@Override
boolean isPiece() {
return true;
}
},
BLUE {
@Override
PieceColor opposite() {
return RED;
}
@Override
boolean isPiece() {
return true;
}
};
/** Return the piece color of my opponent, if defined. */
PieceColor opposite() {
throw new UnsupportedOperationException();
}
/** Return true iff I denote a piece rather than an empty square or
* block. */
boolean isPiece() {
return false;
}
@Override
public String toString() {
return capitalize(super.toString().toLowerCase());
}
/** Return WORD with first letter capitalized. */
static String capitalize(String word) {
return Character.toUpperCase(word.charAt(0)) + word.substring(1);
}
/** Return the PieceColor denoted by COLOR. */
static PieceColor parseColor(String color) {
switch (color.toLowerCase()) {
case "red": case "r":
return RED;
case "blue": case "b":
return BLUE;
default:
throw error("invalid piece color: %s", color);
}
}
}