forked from denkspuren/LiveViewProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNim.java
368 lines (324 loc) · 8.72 KB
/
Nim.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
// Den Code bespreche ich ausführlich im Podcast "Herzbergs Hörsaal" in der Episode
// https://anchor.fm/dominikusherzberg/episodes/PiS-Das-Nim-Spiel-in-Java-programmiert-edks2t
//
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Move {
final int row, number;
static Move of(int row, int number) {
return new Move(row, number);
}
private Move(int row, int number) {
if (row < 0 || number < 1)
throw new IllegalArgumentException();
this.row = row;
this.number = number;
}
public String toString() {
return "(" + row + ", " + number + ")";
}
}
class MoveSim {
private Move move;
private int wins;
private int losses;
private int result;
public MoveSim(Move move) {
this.move = move;
this.wins = 0;
this.losses = 0;
this.result = 0;
}
public int calcWin() {
return result = wins - losses;
}
public Move getMove() {
return move;
}
public void inkrementWins() {
wins++;
}
public void inkrementLosses() {
losses++;
}
public int getWins() {
return wins;
}
public int getLosses() {
return losses;
}
public int getResult() {
return result;
}
}
interface NimGame {
static boolean isWinning(int... numbers) {
return Arrays.stream(numbers).reduce(0, (i, j) -> i ^ j) != 0;
// klassische Variante:
// int res = 0;
// for(int number : numbers) res ^= number;
// return res != 0;
}
NimGame play(Move... moves);
Move randomMove();
Move bestMove();
boolean isGameOver();
String toString();
}
class Nim implements NimGame {
private Random r = new Random();
int[] rows;
public static Nim of(int... rows) {
return new Nim(rows);
}
protected Nim(int... rows) {
if (rows.length > 5)
throw new IllegalArgumentException("Es sind max. 5 Reihen erlaubt");
for (int i : rows) {
if (i > 7) {
throw new IllegalArgumentException("Es sind max. 7 Stäbchen pro Reihe erlaubt");
}
}
assert rows.length >= 1;
assert Arrays.stream(rows).allMatch(n -> n >= 0);
this.rows = Arrays.copyOf(rows, rows.length);
}
protected Nim play(Move m) {
assert !isGameOver();
assert m.row < rows.length && m.number <= rows[m.row];
Nim nim = Nim.of(rows);
nim.rows[m.row] -= m.number;
return nim;
}
public Nim play(Move... moves) {
Nim nim = this;
for (Move m : moves)
nim = nim.play(m);
return nim;
}
public Move randomMove() {
assert !isGameOver();
int row;
do {
row = r.nextInt(rows.length);
} while (rows[row] == 0);
int number = r.nextInt(rows[row]) + 1;
return Move.of(row, number);
}
public Move bestMove() {
assert !isGameOver();
if (!NimGame.isWinning(rows))
return randomMove();
Move m;
do {
m = randomMove();
} while (NimGame.isWinning(play(m).rows));
return m;
}
public boolean isGameOver() {
return Arrays.stream(rows).allMatch(n -> n == 0);
}
public String toString() {
String s = "";
for (int n : rows)
s += "\n" + "I ".repeat(n);
return s;
}
@Override
public boolean equals(Object object) {
if (object == null)
return false;
if (object == this)
return true;
if (object.getClass() != this.getClass())
return false;
Nim other = (Nim) object;
return Arrays.equals(this.rows, other.rows);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(rows);
return result;
}
public ArrayList possibleMoves() {
ArrayList<Move> moves = new ArrayList<>();
for (int i = 0; i < rows.length; i++) {
for (int j = 1; j <= rows[i]; j++) {
moves.add(Move.of(i, j));
}
}
return moves;
}
public Move mcMove() {
ArrayList<Move> moves = new ArrayList<>();
for (int i = 0; i < rows.length; i++) {
for (int j = 1; j <= rows[i]; j++) {
moves.add(Move.of(i, j));
}
}
ArrayList<MoveSim> bestMoves = new ArrayList<>();
for (Move move : moves) {
bestMoves.add(new MoveSim(move));
}
int simLength = 100;
for (int i = 0; i < simLength; i++) {
for (MoveSim simulation : bestMoves) {
Nim copyNim = new Nim(this.rows);
// copyNim = copyNim.play(simulation.getMove());
while (!copyNim.isGameOver()) {
copyNim = copyNim.play(copyNim.randomMove());
}
if (NimGame.isWinning(copyNim.rows)) {
simulation.inkrementWins();
} else {
simulation.inkrementLosses();
}
}
}
bestMoves.sort((a, b) -> b.calcWin() - a.calcWin());
int highestWinValue = bestMoves.get(0).calcWin();
List<MoveSim> highestWinMoves = bestMoves.stream()
.filter(move -> move.calcWin() == highestWinValue)
.collect(Collectors.toList());
if (highestWinMoves.size() > 1) {
Random r = new Random();
MoveSim randomBestMove = highestWinMoves.get(r.nextInt(highestWinMoves.size()));
return randomBestMove.getMove();
}
return bestMoves.get(0).getMove();
}
public String evaluateMcMove() {
//Ich habe jetzt viele Sachen versucht aber irgendwie kriege ich immer 0% als Ergebnis
int wins = 0;
String result = "";
// min 5 mal spielen
for (int i = 0; i < 5; i++) {
//100 durchläufe
for (int j = 0; j < 100; j++) {
Nim randomNim = randomSetup();
randomNim.toString();
while (!randomNim.isGameOver()) {
randomNim = randomNim.play(randomNim.mcMove());
}
if (NimGame.isWinning(randomNim.rows)) {
wins++;
}
}
}
double totalWinRate = (double) wins / (5 * 100) * 100;
return totalWinRate + "%\n";
}
public Nim randomSetup() {
Random r = new Random();
int numRows = r.nextInt(5) + 1;
int totalSticks;
do {
rows = new int[numRows];
totalSticks = 0;
for (int i = 0; i < rows.length; i++) {
rows[i] = r.nextInt(6) + 2;
totalSticks += rows[i];
}
} while (totalSticks < 5);
return Nim.of(rows);
}
}
/*
* Nim nim = Nim.of(2,3,4);assert
* nim!=nim.play(Move.of(1,2)):"Return a new Nim instance";
*
* int[] randomSetup(int... maxN) {
* Random r = new Random();
* int[] rows = new int[maxN.length];
* for (int i = 0; i < maxN.length; i++) {
* rows[i] = r.nextInt(maxN[i]) + 1;
* }
* return rows;
* }
*
* ArrayList<Move> autoplay(NimGame nim) {
* ArrayList<Move> moves = new ArrayList<>();
* while (!nim.isGameOver()) {
* Move m = nim.bestMove();
* moves.add(m);
* nim = nim.play(m);
* }
* return moves;
* }
*
* boolean simulateGame(int... maxN) {
* Nim nim = Nim.of(randomSetup(maxN));
* // System.out.println(nim);
* // System.out.println((NimGame.isWinning(nim.rows) ? "first" : "second") +
* " to win");
* ArrayList<Move> moves = autoplay(nim);
* // System.out.println(moves);
* return (NimGame.isWinning(nim.rows) && (moves.size() % 2) == 1) ||
* (!NimGame.isWinning(nim.rows) && (moves.size() % 2) == 0);
* }
*
* assert IntStream.range(0,100).allMatch(i->
*
* simulateGame(3,4,5));
* assert IntStream.range(0,100).allMatch(i -> simulateGame(3,4,6,8));
*/
/*
* // Beispielhaftes Spiel über JShell
*
* jshell> Nim n = Nim.of(2,3,4)
* n ==>
* I I
* I I I
* I I I I
*
* jshell> n = n.play(n.bestMove())
* n ==>
* I I
* I I I
* I
*
* jshell> n = n.play(Move.of(2,1))
* n ==>
* I I
* I I I
*
*
* jshell> n = n.play(n.bestMove())
* n ==>
* I I
* I I
*
*
* jshell> n = n.play(Move.of(1,1))
* n ==>
* I I
* I
*
*
* jshell> n = n.play(n.bestMove())
* n ==>
* I
* I
*
*
* jshell> n = n.play(Move.of(1,1))
* n ==>
* I
*
*
*
* jshell> n = n.play(n.bestMove())
* n ==>
*
*
*
*
* jshell> n.isGameOver()
* $25 ==> true
*/