forked from husujo/Mastermind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMastermindGame.java
410 lines (309 loc) · 9.19 KB
/
MastermindGame.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
public class MastermindGame {
Token[] firstGuess;
Token[] lastGuess; // the most recent guess guessed
int np;
Token[][] allPossibleCombos; // all possible token combos
Token[][] remainingCombos; // token combos remaining in the set
// CONSTRUCTOR
MastermindGame(Token[] tokens, int np) {
// get all possible code combos based on np, nc
this.np = np;
allPossibleCombos = generateAllPossibleCombos(tokens.length, tokens); // get all possible combinations of the tokens
remainingCombos = allPossibleCombos.clone();
firstGuess = new Token[np];
// set first guess to the best guess if the size of the code is 4.
if (np == 4) {
firstGuess[0] = new Token(tokens[0].toString());
firstGuess[1] = new Token(tokens[0].toString());
firstGuess[2] = new Token(tokens[1].toString());
firstGuess[3] = new Token(tokens[1].toString());
} else {
firstGuess = allPossibleCombos[0];
}
}
/******************************************************************************/
/***************************THE GUESSING ALGORITHM*****************************/
// given black and white pegs of the previous guess, formulate a new guess
Token[] playGuess(int black, int white) {
if (black == -1 && white == -1) {
// it's the first guess
lastGuess = firstGuess.clone();
return firstGuess;
}
// wikipedia mastermind algorithm step 5. this works perfectly
for (int i=0; i<remainingCombos.length; i++) {
if (tokenArrayEquals(remainingCombos[i], lastGuess)) {
remainingCombos[i] = null;
}
if (remainingCombos[i] == null) {
continue; // skip null entries
}
int[] temp = countPegs(lastGuess, remainingCombos[i]);
if (temp[1] != black || temp[0] != white) {
remainingCombos[i] = null;
}
}
int[] scores = new int[allPossibleCombos.length];
// wikipedia mastermind algorithm step 6
for (int i=0; i<allPossibleCombos.length; i++) {
scores[i] = calculateMinScore(allPossibleCombos[i], black, white); // working here
}
// RETURN THE NEXT GUESS
scores = removeNonMaxScores(scores);
Token[] nextGuess = isThereAnElementInS(scores);
// if there is an element in remaining
if (nextGuess != null) {
// choose the first one in S you find
lastGuess = nextGuess.clone();
return nextGuess;
} else {
// choose the first one you find
nextGuess = firstElement(scores);
lastGuess = nextGuess.clone();
return nextGuess;
}
}
Token[] firstElement(int[] scores) {
for (int i=0; i<scores.length; i++) {
if (scores[i] != 0) {
return allPossibleCombos[i];
}
}
// if get to this point, every score is 0
return null;
}
// if finds an element of allPossibleCombos in remainingCombos, returns it, else return null.
Token[] isThereAnElementInS(int[] scores) {
for (int i=0; i<scores.length; i++) {
if (scores[i] > 0) {
if (contains(allPossibleCombos[i], remainingCombos)) {
return allPossibleCombos[i];
}
}
}
return null;
}
// is t in S?
boolean contains(Token[] t, Token[][] S) {
for (int i=0; i<S.length; i++) {
if (equals(t, S[i])) {
return true;
}
}
return false;
}
// keep only the highest scores
int[] removeNonMaxScores(int[] scores) {
int max = 0;
for (int i=0; i<scores.length; i++) {
if (max<scores[i]) {
max = scores[i];
}
}
for (int i=0; i<scores.length; i++) {
if (scores[i] != max) {
scores[i] = 0;
}
}
return scores;
}
// min number of entries in remainingCombos will be eliminated by guess for each possible black/white score
int calculateMinScore(Token[] code, int black, int white) {
int[][] allbwcombos = allBWcombos(np);
int[] scores = new int[allbwcombos.length];
int count = 0;
for (int i=0; i<scores.length; i++) { // for every b/w combination, how many entries are eliminated?
for (int j=0; j<remainingCombos.length; j++) { // which entries in remainingCombos are eliminated?
// skip previously eliminated entries
if (remainingCombos[j] == null) {
continue;
}
int[] temp = countPegs(code, remainingCombos[j]);
// if the black and white pegs match exactly
if (temp[0] != allbwcombos[i][0] || temp[1] == allbwcombos[i][1]) {
// if temp doesnt match white/black, 'remove' (increment counter)
count++;
}
if (tokenArrayEquals(code, remainingCombos[j])) {
count++;
}
}
scores[i] = count;
count = 0;
}
// THE MIN IS ALWAYS ZERO
return getMin(scores);
}
/******************************************************************************/
/*****************************COUNTING THE PEGS********************************/
/******************************************************************************/
// counts the white and black pegs of a guess and code
int[] countPegs(Token[] code, Token[] guess) {
int[] bw = new int[2];
bw[1] = countBlack(code, guess);
bw[0] = Math.max(countWhite(code, guess) - bw[1], 0); // make sure white pegs stay >=0.
return bw;
}
int countBlack(Token[] code, Token[] guess) {
int black = 0;
for (int i=0; i<code.length; i++) {
if (code[i].equals(guess[i])) {
black++;
}
}
return black;
}
int countWhite(Token[] code, Token[] guess) {
int white = 0;
Token[] clone = code.clone();
for (int i=0; i<code.length; i++) {
for (int j=0; j<clone.length; j++) {
if (guess[i].equals(clone[j])) {
white++;
clone[j] = null;
break;
}
}
}
return white;
}
// generates all possible black/white peg combos
int[][] allBWcombos(int np) {
int size = getSize(np);
int[][] bwcombos = new int[size][2];
int white = 0;
int black = 0;
int index = 0;
for (int i=0; i<=np; i++) {
black = 0;
white = i;
while (white >= 0) {
bwcombos[index][0] = white;
bwcombos[index][1] = black;
black++;
white--;
index++;
}
}
return bwcombos;
}
int getSize(int np) {
if (np == 1) {
return 3;
} else {
return 1+np+getSize(np-1);
}
}
/**************************************************************************************************/
/*******************CODE FOR GETTING ALL POSSIBLE COMBINATIONS OF CODES****************************/
/**************************************************************************************************/
// takes np=numPositions, and nc=numColors
Token[][] generateAllPossibleCombos(int nc, Token[] tokens) {
int[][] list = new int[(int) Math.pow(nc, np)][np]; // make the array the right size
// set the first possible combo = 0 0 0 0....
for (int i=0; i<np; i++) {
list[0][i] = 0;
}
// generate the rest of the combos
for (int i=1; i<list.length; i++) {
list[i] = incrementCombo(list[i-1], np, nc);
}
// turn the int[][] into a Token[][]
Token[][] allCombos = new Token[list.length][np];
// for each combo:
for (int i=0; i<list.length; i++) {
// for each position in the combo list[i]:
for (int j=0; j<np; j++) {
allCombos[i][j] = new Token(tokens[ list[i][j] ].toString());
}
}
return allCombos;
}
// np=number of positions, nc = number of colors
int[] incrementCombo(int[] oldCombo, int np, int nc) {
int[] newCombo = oldCombo.clone();
for (int i=np-1; i>0; i--) {
if (newCombo[i] == nc-1) {
if (newCombo[i-1] == nc-1) {
continue;
}
newCombo[i-1]++;
// reset the previous digits to 0
for (int j=i; j<np; j++) {
newCombo[j] = 0;
}
return newCombo;
}
newCombo[i]++;
return newCombo;
}
return newCombo;
}
// returns the 'max' combo given the #positions(int[] length) and #colors
int[] maxCombo(int[] combo, int nc) {
int[] max = new int[combo.length];
for (int i=0; i<max.length; i++) {
max[i] = nc-1;
}
return max;
}
/*******************************************************************************************/
/**************************************GENERAL PURPOSE**************************************/
/*******************************************************************************************/
public int getMin(int[] a) {
int min = Integer.MAX_VALUE;
for (int i=0; i<a.length; i++) {
if (min > a[i]) {
min = a[i];
}
}
return min;
}
public void printArray(Object[] a) {
for (int i=0; i<a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
}
public void printArray(int[] a) {
for (int i=0; i<a.length; i++) {
System.out.print(a[i] + "\t");
}
System.out.println();
}
public void print2DArray(Object[][] a) {
for (int i=0; i<a.length; i++) {
printArray(a[i]);
}
}
public void print2DArray(int[][] a) {
for (int i=0; i<a.length; i++) {
printArray(a[i]);
}
}
boolean tokenArrayEquals(Token[] one, Token[] two) {
if (one == null || two == null) {
return false;
}
if (one.length != two.length) {
return false;
}
for (int i=0; i<one.length; i++) {
if (!one[i].equals(two[i])) {
return false;
}
}
return true;
}
boolean equals(Token[] one, Token[] two) {
if (one == null || two == null) {
return false; // why do i need this????
}
for (int i=0; i<one.length; i++) {
if (!one[i].equals(two[i])) {
return false;
}
}
return true;
}
}