-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardCell.java
54 lines (44 loc) · 1.23 KB
/
BoardCell.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
package model;
import java.awt.*;
import java.util.Random;
/**
* This enumerated type represents a board cell. A board cell has a
* color (based on Color) and a name (e.g., "R").
*/
public enum BoardCell {
RED(Color.RED, "R"),
GREEN(Color.GREEN, "G"),
BLUE(Color.BLUE, "B"),
YELLOW(Color.YELLOW, "Y"),
EMPTY(Color.WHITE, ".");
private final Color color;
private final String name;
private static int totalColors = BoardCell.values().length;
private BoardCell(Color color, String name) {
this.color = color;
this.name = name;
}
public Color getColor() {
return color;
}
public String getName() {
return name;
}
public static int getTotalColors() {
return totalColors;
}
/** Generates a random BoardCell using the specified Random object.
* @param random
* @return random BoardCell
*/
public static BoardCell getNonEmptyRandomBoardCell(Random random) {
int target = random.nextInt(totalColors);
for (BoardCell boardCell : BoardCell.values()) {
if (boardCell == BoardCell.EMPTY)
return BoardCell.RED;
if (target == boardCell.ordinal())
return boardCell;
}
throw new IllegalArgumentException("Invalid random number generated");
}
}