forked from bstrds/bckbn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
415 lines (304 loc) · 8.2 KB
/
Player.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
411
412
413
414
415
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Player {
public static final int MAX = 1;
public static final int MIN = -1;
public static final int NEWBOARD = new Board().hashCode();
private byte maxDepth;
private byte playerColor;
private byte d1;
private byte d2;
public Player() {
maxDepth = 2;
playerColor = Board.B;
}
public Player(byte maxDepth, byte playerColor) {
this.maxDepth = maxDepth;
this.playerColor = playerColor;
}
public void roll() {
this.d1 = (byte)((Math.random()*6)+1);
this.d2 = (byte)((Math.random()*6)+1);
}
public byte getD1() {
return this.d1;
}
public byte getD2() {
return this.d2;
}
public byte getDepth() {
return this.maxDepth;
}
public Move[] inputMove(Board b) {
if(b.getChildren(d1, d2, playerColor).isEmpty()) {
if(playerColor==Board.W)
System.out.println("White rolled "+d1+" and "+d2+" , but it's not very effective..");
else
System.out.println("Black rolled "+d1+" and "+d2+" , but it's not very effective..");
return null;
}
Move[] moves;
String fr;
String t;
int from = -1;
int to = -1;
boolean gotit = false;
boolean dubs = (d1==d2);
boolean direction = false;
/* to determine which dice was played */
boolean pD1 = false;
boolean pD2 = false;
/* makes new Move array with length depending on
* whether or not we have doubles
*/
if(dubs)
moves = new Move[4];
else
moves = new Move[2];
/* creates a temporary board so that we can
* display moves while a turn has not finished
*/
Board tempB = new Board(b);
for(int i=0; i<moves.length; i++) {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(read);
if(playerColor==Board.W)
System.out.println("White rolled "+d1+" and "+d2+" .");
else
System.out.println("Black rolled "+d1+" and "+d2+" .");
//Position[] tempP = tempB.getPositions();
/* checking if we are in lastrun mode */
boolean lastrun = tempB.lastrun(playerColor);
while(!gotit) {
try {
System.out.println("Enter where you want to move from :");
fr = in.readLine();
from = Integer.parseInt(fr);
System.out.println("Enter where you want to move to :");
t = in.readLine();
to = Integer.parseInt(t);
/* checking if everyone is moving in the right direction */
direction = (((playerColor==Board.W) && ((to-from)>0)) || ((playerColor==Board.B) && ((to-from)<0)));
if((Math.abs(from-to) == d1) && direction && (!pD1 || dubs) || lastrun) {
/* making the move on the temporary board
* for displaying purposes
*/
if(tempB.moveIsLegal((byte)from, (byte)to, playerColor, d1, d2)) {
if((!dubs && i==0)||(dubs && i!=3)) {
tempB.playMove((byte)from, (byte)to, playerColor);
tempB.print();
}
pD1 = true;
gotit = true;
} else {
System.out.println("\nIllegal move, try again0. \n");
}
} else if((Math.abs(from-to) == d2) && direction && (!pD2 || dubs) || lastrun) {
/* same as above */
if(tempB.moveIsLegal((byte)from, (byte)to, playerColor, d1, d2)) {
if((!dubs && i==0)||(dubs && i!=3)) {
tempB.playMove((byte)from, (byte)to, playerColor);
tempB.print();
}
pD2 = true;
gotit = true;
} else {
System.out.println("\nIllegal move, try again1. \n");
}
}else {
System.out.println("\nIllegal move, try again2. \n");
}
} catch(Exception e) {
System.out.println("\nTry again3.\n");
//e.printStackTrace();
}
}
gotit = false;
moves[i] = new Move((byte)from, (byte)to, playerColor);
if(tempB.isTerminal()) {
Move[] tempM = new Move[i+1];
for(int c=0; c<tempM.length; c++) {
tempM[c] = moves[c];
}
return tempM;
}
}
return moves;
}
public Board MiniMax(Board b, byte d1, byte d2) {
if(playerColor==Board.B) {
Board max = max(new Board(b), 0, d1 ,d2);
/* if there are no legal moves to be played */
if(max.hashCode()==NEWBOARD) {
System.out.println("\n\nFUUUUU\n\n");
return b;
} else {
return max;
}
} else { /* if white player is to play */
Board min = min(new Board(b), 0, d1, d2);
/* if there are no legal moves to be played */
if(min.hashCode()==NEWBOARD) {
System.out.println("\n\nFUUUUU\n\n");
return b;
} else {
return min;
}
}
}
public Board min(Board b, int depth, byte d1, byte d2) {
if(b.isTerminal() || depth>=maxDepth) {
return b;
}
Board minBoard = new Board();
if(depth==0) {
ArrayList<Board> children = b.getChildren(d1, d2, Board.W);
int min = Integer.MAX_VALUE;
for(Board child : children) {
max(child, depth+1, d1, d2);
if(child.getValue() < min) {
min = child.getValue();
minBoard = new Board(child);
}
}
return minBoard;
} else {
ArrayList<Board> children = new ArrayList<Board>();
ArrayList<Board> tmp;
for(byte i=0; i<21; i++) {
if(i<6) {
d1 = 1;
d2 = (byte)(i+1);
} else if(i<11) {
d1 = 2;
d2 = (byte)(i-4);
} else if(i<15) {
d1 = 3;
d2 = (byte)(i-8);
} else if(i<18) {
d1 = 4;
d2 = (byte)(i-11);
} else if(i<20) {
d1 = 5;
d2 = (byte)(i-13);
} else {
d1 = 6;
d1 = 6;
}
tmp = new ArrayList<Board>(b.getChildren(d1, d2, Board.W));
for(Board child : tmp) {
/* setting the dice that were played on the
* board for future use */
child.setd1Pl(d1);
child.setd2Pl(d2);
child.setParent(b);
children.add(child);
}
}
int counter = 0;
int value = 0;
for(Board child : children) {
Board temp = max(child, depth+1, d1, d2);
byte D1 = child.getd1Pl();
byte D2 = child.getd2Pl();
if(temp!=null) {
counter++;
if(D1==D2) {
value += temp.evaluate()*2;
} else {
value += temp.evaluate();
}
} else {
counter++;
if(D1==D2) {
value += child.getValue()*2;
} else {
value += child.getValue();
}
}
}
if(counter!=0)
b.setValue(value/counter);
else
b.setValue(0);
}
return null;
}
public Board max(Board b, int depth, byte d1, byte d2) {
if(b.isTerminal() || depth>=maxDepth) {
return b;
}
Board maxBoard = new Board();
if(depth==0) {
ArrayList<Board> children = b.getChildren(d1, d2, Board.B);
int max = Integer.MIN_VALUE;
for(Board child : children) {
min(child, depth+1, d1, d2);
if(child.getValue() > max) {
max = child.getValue();
maxBoard = new Board(child);
}
}
return maxBoard;
} else {
ArrayList<Board> children = new ArrayList<Board>();
ArrayList<Board> tmp;
for(byte i=1; i<7; i++) {
if(i<6) {
d1 = 1;
d2 = (byte)(i+1);
} else if(i<11) {
d1 = 2;
d2 = (byte)(i-4);
} else if(i<15) {
d1 = 3;
d2 = (byte)(i-8);
} else if(i<18) {
d1 = 4;
d2 = (byte)(i-11);
} else if(i<20) {
d1 = 5;
d2 = (byte)(i-13);
} else {
d1 = 6;
d1 = 6;
}
tmp = new ArrayList<Board>(b.getChildren(d1, d2, Board.B));
for(Board child : tmp) {
child.setd1Pl(d1);
child.setd2Pl(d2);
child.setParent(b);
children.add(child);
}
}
int counter = 0;
int value = 0;
for(Board child : children) {
Board temp = min(child, depth+1, d1, d2);
byte D1 = child.getd1Pl();
byte D2 = child.getd2Pl();
if(temp!=null) {
counter++;
if(D1==D2) {
value += temp.evaluate_modie()*2;
} else {
value += temp.evaluate_modie();
}
} else {
counter++;
if(D1==D2) {
value += child.getValue()*2;
} else {
value += child.getValue();
}
}
}
if(counter!=0)
b.setValue(value/counter);
else
b.setValue(0);
}
return null;
}
}