-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMastermind.java
175 lines (142 loc) · 6.47 KB
/
Mastermind.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
package mastermind_solution;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
class Mastermind {
private ArrayList<State> possibilities;
private int positionCount, colorCount;
public enum GameState {
OK, ERROR_INCORRECT_STATE, ERROR_INCORRECT_POINTS, FOUND_SOLUTION, NO_SOLUTION
};
public static Mastermind createGame(int positionCount, int colorCount) {
if (positionCount <= 0 || colorCount <= 0) return null;
return new Mastermind(positionCount, colorCount);
}
private Mastermind(int positionCount, int colorCount) {
possibilities = new ArrayList<State>();
this.positionCount = positionCount;
this.colorCount = colorCount;
initializePossibilities(positionCount-1, colorCount, new int[positionCount]);
}
private void initializePossibilities(int positionIndex, int colorCount, int[] positions) {
if (positionIndex < 0) {
possibilities.add(new State(positions, colorCount));
return;
}
for (int color = 0; color < colorCount; color++) {
positions[positionIndex] = color;
initializePossibilities(positionIndex-1, colorCount, positions);
}
}
public int getPositionCount() {
return positionCount;
}
public int getColorCount() {
return colorCount;
}
public State getGuess() {
if (possibilities.size() == 0) return null;
int randomIndex = (int) (Math.random()*possibilities.size());
return possibilities.get(randomIndex);
}
public GameState rateGuess(State guess, int positionPoints, int colorPoints) {
if (possibilities.indexOf(guess) == -1) {
System.err.println("Specified guess is not possible! (you need to send the object you recieve from getGuess() method)");
return GameState.ERROR_INCORRECT_STATE;
}
if (positionPoints < 0 || colorPoints < 0 || positionPoints + colorPoints > positionCount) {
System.err.println("Points incorrect! (too small or too high)");
return GameState.ERROR_INCORRECT_POINTS;
}
Iterator possibilitiesIterator = possibilities.iterator();
while (possibilitiesIterator.hasNext()) {
State possibility = (State) possibilitiesIterator.next();
int possibilityColorPoints = 0, possibilityPositionPoints = 0;
for (int i = 0; i < positionCount; i++) if (possibility.getPositionValue(i) == guess.getPositionValue(i)) possibilityPositionPoints++;
for (int i = 0; i < colorCount; i++) possibilityColorPoints += min(possibility.getColorCount(i), guess.getColorCount(i));
possibilityColorPoints -= possibilityPositionPoints;
if (colorPoints != possibilityColorPoints || positionPoints != possibilityPositionPoints) possibilitiesIterator.remove();
}
if (possibilities.size() == 0) return GameState.NO_SOLUTION;
else if (possibilities.size() == 1) return GameState.FOUND_SOLUTION;
return GameState.OK;
}
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int positionCount = getPositiveInteger(scanner, "Enter position count: ");
int colorCount = getPositiveInteger(scanner, "Enter color count: ");
System.out.println("---------------------------");
Mastermind game = createGame(positionCount, colorCount);
while (true) {
State guess = game.getGuess();
System.out.print("Guess: ");
for (int i = 0; i < game.positionCount; i++) System.out.print(guess.getPositionValue(i) + " ");
System.out.println("\nRate the guess:");
GameState gameState;
do {
int positionPoints = getNonNegativeInteger(scanner, "\tEnter \"correct position\" count: ");
int colorPoints = getNonNegativeInteger(scanner, "\tEnter \"correct color\" count: ");
gameState = game.rateGuess(guess, positionPoints, colorPoints);
} while (gameState == GameState.ERROR_INCORRECT_POINTS);
System.out.println("---------------------------");
if (gameState == GameState.NO_SOLUTION) {
System.out.println("There is no solution.");
break;
} else if (gameState == GameState.FOUND_SOLUTION) {
System.out.println("Found solution!");
State solution = game.getGuess();
System.out.print("\tSolution is ");
for (int i = 0; i < game.positionCount; i++) System.out.print(solution.getPositionValue(i) + " ");
break;
}
}
scanner.close();
}
public static int getPositiveInteger(Scanner scanner, String message) {
System.out.print(message);
int retVal = -1;
while (retVal <= 0) {
try {
retVal = Integer.parseInt(scanner.nextLine());
} catch(Exception e) {}
if (retVal <= 0) {
System.out.println("Please enter positive integer!");
System.out.print(message);
}
}
return retVal;
}
public static int getNonNegativeInteger(Scanner scanner, String message) {
System.out.print(message);
int retVal = -1;
while (retVal < 0) {
try {
retVal = Integer.parseInt(scanner.nextLine());
} catch(Exception e) {}
if (retVal < 0) {
System.out.println("Please enter non-negative integer!");
System.out.print(message);
}
}
return retVal;
}
public static int min(int a, int b) {
return (a<b)?a:b;
}
class State {
private int[] positions;
private int[] colors;
public State(int[] values, int colorCount) {
positions = new int[values.length];
colors = new int[colorCount];
for (int i = 0; i < positions.length; i++) positions[i] = values[i];
for (int i = 0; i < positions.length; i++) colors[positions[i]]++;
}
public int getPositionValue(int index) {
return positions[index];
}
public int getColorCount(int color) {
return colors[color];
}
}
}