-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecretPattern.java
More file actions
230 lines (191 loc) · 4.21 KB
/
SecretPattern.java
File metadata and controls
230 lines (191 loc) · 4.21 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
*@param args
*@author Mirghani
*@course CPSC 220
*@section 1
*@description This program is a game in which the user must select
*the correct order and color of the pegs
*/
import java.util.*;
class SecretPattern {
// Data members
char peg1, peg2, peg3, peg4;
// Consructors
public SecretPattern() {
peg1 = 'R';
peg2 = 'R';
peg3 = 'R';
peg4 = 'R';
}
// Convert an interger to its corresponding colour letter
public char intToColour(int i) {
char c = ' ';
switch (i) {
case 0: c = 'R';
break; // red
case 1: c = 'G';
break; // green
case 2: c = 'B';
break; // blue
case 3: c = 'C';
break; // cyan
case 4: c = 'Y';
break; // yellow
case 5: c = 'M';
break; // magenta
default: System.err.println("Error: no such colour!");
System.exit(1);
}
return c;
}
// Generate four pegs for the secret code
public void generateSecretCode() {
Random random = new Random();
int num = random.nextInt(6);
char c = intToColour(num);
setColorPeg1(c);
num = random.nextInt(6);
c = intToColour(num);
setColorPeg2(c);
num = random.nextInt(6);
c = intToColour(num);
setColorPeg3(c);
num = random.nextInt(6);
c = intToColour(num);
setColorPeg4(c);
}
public char getColorPeg1()
{
return peg1;
}
public char getColorPeg2()
{
return peg2;
}
public char getColorPeg3()
{
return peg3;
}
public char getColorPeg4()
{
return peg4;
}
public void setColorPeg1(char c)
{
peg1 = c;
}
public void setColorPeg2(char c)
{
peg2 = c;
}
public void setColorPeg3(char c)
{
peg3 = c;
}
public void setColorPeg4(char c)
{
peg4 = c;
}
//Returns string representation of a SecretPattern object
public String toString() {
return peg1+ " " +
peg2+ " " +
peg3+ " " +
peg4;
}
//Returns the number of patternCorrect between object and guessPegs
public int countPattern(SecretPattern guessPegs) {
int patternCorrect = 0;
if(peg1 == guessPegs.getColorPeg1())
{
patternCorrect += 1;
}
if(peg2 == guessPegs.getColorPeg2())
{
patternCorrect += 1;
}
if(peg3 == guessPegs.getColorPeg3())
{
patternCorrect += 1;
}
if(peg4 == guessPegs.getColorPeg4())
{
patternCorrect += 1;
}
return patternCorrect;
}
public int countColors(SecretPattern guessPegs) {
int hits = 0;
char guessPeg;
guessPeg = guessPegs.getColorPeg1();
if(peg1 == guessPeg)
{
//this is pattern correct not color correct
//Don't count this toward color correct!
}
else if(peg2 == guessPeg)
{
hits += 1;
}
else if(peg3 == guessPeg)
{
hits += 1;
}
else if(peg4 == guessPeg)
{
hits += 1;
}
guessPeg = guessPegs.getColorPeg2();
if(peg2 == guessPeg)
{
//Don't count this toward color correct!
}
else if(peg1 == guessPeg)
{
hits += 1;
}
else if(peg3 == guessPeg)
{
hits += 1;
}
else if(peg4 == guessPeg)
{
hits += 1;
}
guessPeg = guessPegs.getColorPeg3();
if(peg3 == guessPeg)
{
//Don't count count this toward color correct!
}
else if(peg1 == guessPeg)
{
hits += 1;
}
else if(peg2 == guessPeg)
{
hits += 1;
}
else if(peg4 == guessPeg)
{
hits += 1;
}
guessPeg = guessPegs.getColorPeg4();
if(peg4 == guessPeg)
{
//Don't count this toward color correct!
}
else if(peg1 == guessPeg)
{
hits += 1;
}
else if(peg2 == guessPeg)
{
hits += 1;
}
else if(peg3 == guessPeg)
{
hits += 1;
}
return hits;
}
}