-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeg.java
More file actions
65 lines (54 loc) · 1.6 KB
/
Peg.java
File metadata and controls
65 lines (54 loc) · 1.6 KB
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
import java.util.*;
public class Peg
{
//class constants
private static final char VIOLET = 'V';
private static final char INDIGO = 'I';
private static final char BLUE = 'B';
private static final char GREEN = 'G';
private static final char YELLOW = 'Y';
private static final char ORANGE = 'O';
private static final char RED = 'R';
private static final char PINK = 'P';
private static final char CYAN = 'C';
private static final char MAROON = 'M';
//instance variables
private char color;
private int numColors;
private Character[] colors = {VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED, PINK, CYAN, MAROON};
//default constructor generates a peg
//with a random color within the range
//of colors for that difficulty level
public Peg(int nc)
{
numColors = nc;
Random rand = new Random();
int randomNum = rand.nextInt(numColors);
color = colors[randomNum];
}
//constructor that assigns color to the peg
public Peg(int nc, Character c)
{
numColors = nc;
for (int i = 0; i < numColors; i++)
{
if (colors[i] == c)
{
color = c;
return;
}
}
throw new IllegalArgumentException(c + " is not a correct peg color.");
}
public char getColor()
{
return color;
}
public boolean equals(Peg other)
{
if (this.color == other.color)
return true;
else
return false;
}
}