-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOg.java
1082 lines (934 loc) · 33 KB
/
Og.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
//
//
//Og.java by christopher fuentes
//
//HUID 60857326
//
//
//
//
*
*
* Og.java sets up a DIM by DIM board and then prompts the user
* for their play preferences (human vs human, computer vs computer etc...).
*
* By default, it uses an Alpha Beta Pruning MiniMax search to
* determine the best moves for the computer players.
*
* You can turn this off by uncommenting the appropriate line in the
* class ComputerPlayer.
*
* Several classes are implemented to play the game:
*
* Og - the actual game play, calls on Players to return move coordinates.
* Player - parent of HumanPlayer and ComputerPlayer
* HumanPlayer - prompts the human user for input on each ply
* ComputerPlayer - calculates moves using minimax search
* BoardState - a theoretical board configuration.
* Point - standard geometric point
* Action - a Point and an Integer value associated with moving on that point.
* TTable - a transposition table which stores all explored BoardStates and
* all of their symmetries. Used to check if an identical node or
* node symmetrical to a node has been explored already and if so
* get that node's final value.
*
* Rules:
*
* Each player chooses one square to fill on their ply.
*
* A square is "captured" by a player if it is surrounded
* on all four cardinal sides by either edges of the board
* or filled squares of that same player.
*
* Players can not capture squares that have already been filled
* by either player.
*
* If by filling that square a player has captured other squares,
* those squares will automatically be filled.
*
* Additionally, the player will get another move on that ply.
* If more squares are captured they too will be filled,
* but the player will end their ply immediately thereafter.
* (I.E. this is not an infinite loop).
*
* The game ends when the board is full.
*
* The player controlling the most squares at the end wins.
*
*
*/
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
public class Og
{
//board dimensions
public static final int DIM = 4;
public static final char NO_WINNER = '_';
public static final char PLAYER_1 = 'X';
public static final char PLAYER_2 = 'O';
public static final char TIE = '3';
//game board
private static char[][] board;
//= { {'O', '_', 'O', '_' }, { 'X','_','_','X'},{'_','X','_','O'},{'_','_','_','_'}};
private static Player player1;
private static Player player2;
//a switch used to determine whose turn it is.
private static boolean flag = true;
public static void main(String[] args)
{
board = new char[DIM][DIM];
//init board to '0'
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
board[i][j] = NO_WINNER;
board[0][0] = NO_WINNER;
println("Welcome to Og!");
println("Who is playing?");
//determine HVH, CVC, HVC or CVH
int choice;
String prompt1 = "(Type 1 for Human v Human, 2 for Comp v "
+ "Comp, 3 for Comp v Human) ";
choice = getValidChoice(1, 3, prompt1);
switch (choice)
{
case 1:
println("Ok, human 1 is X, human 2 is O");
printBoard();
playHVH();
break;
case 2:
println("Ok, comp 1 is X, comp 2 is O");
printBoard();
playCVC();
break;
case 3:
String prompt2 = "Who goes first?\n(type 1 for human, "
+"2 for computer)";
choice = getValidChoice(1, 2, prompt2);
switch (choice)
{
case 1:
println("Ok, human is X, comp is O");
printBoard();
playHVC();
break;
case 2:
println("Ok, comp is X, human is O");
printBoard();
playCVH();
break;
}
}
}
//Get a move from a player.
//check if new squares are captured.
//if so, fill them, get another move.
//check if more squares are captured.
//if so, fill them. Player's ply ends.
public static void play()
{
Point move;
char winner;
//Player 1
do
{
if (flag)
{
println("Player 1's Turn");
move = player1.move(board);
board[move.x][move.y] = PLAYER_1;
//if spots are captured, give player 1 free turn
if (spotsHaveJustBeenCaptured(board, PLAYER_1))
{
println("Captured a spot!");
fillCapturedSpots(board, PLAYER_1);
if ((winner = winner(board)) != NO_WINNER)
{
printBoard();
break;
}
printBoard();
println("Player 1 moves again");
move = player1.move(board);
board[move.x][move.y] = PLAYER_1;
if (spotsHaveJustBeenCaptured(board, PLAYER_1))
{
println("Captured more spots!");
fillCapturedSpots(board, PLAYER_1);
}
}
flag = false;
}
//Player 2
else
{
println("Player 2's Turn");
move = player2.move(board);
board[move.x][move.y] = PLAYER_2;
flag = true;
//if spots are captured, give player 1 free turn
if (spotsHaveJustBeenCaptured(board, PLAYER_2))
{
println("Captured a spot!");
fillCapturedSpots(board, PLAYER_2);
if ((winner = winner(board)) != NO_WINNER)
{
printBoard();
break;
}
printBoard();
println("Player 2 moves again");
move = player2.move(board);
board[move.x][move.y] = PLAYER_2;
if (spotsHaveJustBeenCaptured(board, PLAYER_2))
{
println("Captured more spots!");
fillCapturedSpots(board, PLAYER_2);
}
}
}
printBoard();
}
while( (winner = winner(board)) == NO_WINNER);
//declare winner
switch (winner)
{
case TIE:
println("Nobody won....");
break;
case PLAYER_1:
println("Player 1 wins!");
break;
case PLAYER_2:
println("Player 2 wins!");
break;
}
}
public static void playHVH()
{
player1 = new HumanPlayer();
player2 = new HumanPlayer();
play();
}
public static void playCVC()
{
player1 = new ComputerPlayer(PLAYER_1, PLAYER_2);
player2 = new ComputerPlayer(PLAYER_2, PLAYER_1);
play();
}
public static void playHVC()
{
player1 = new HumanPlayer();
player2 = new ComputerPlayer(PLAYER_2, PLAYER_1);
play();
}
public static void playCVH()
{
player1 = new ComputerPlayer(PLAYER_1, PLAYER_2);
player2 = new HumanPlayer();
play();
}
//get a valid choice from human between the range lolim and hilim, inclusive
public static int getValidChoice(int loLim, int hiLim, String prompt)
{
Scanner input = new Scanner(System.in);
int choice;
do
{
print(prompt);
try {
choice = input.nextInt();
} catch (InputMismatchException e) {
System.out.println("Looking for more of a number....");
input.next();
choice = -1;
}
}
while (choice < loLim || choice > hiLim);
return choice;
}
//return number of spots the winner controlls
public static int evalWinner(char[][] aBoard, char player)
{
int pCount = 0;
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
{
if (aBoard[i][j] == player) pCount++;
}
return pCount;
}
//if board is full, game is over
public static boolean terminalTest(char[][] board)
{
int voidCount = 0;
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
if (board[i][j] == NO_WINNER) voidCount++;
return (voidCount == 0);
}
//determine who won
public static char winner(char[][] aBoard)
{
int p1Count = 0;
int p2Count = 0;
//make sure board is full
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
{
if (aBoard[i][j] == NO_WINNER) return NO_WINNER;
if (aBoard[i][j] == PLAYER_1) p1Count++;
if (aBoard[i][j] == PLAYER_2) p2Count++;
}
if (p1Count == p2Count) return TIE;
return ((p1Count > p2Count) ? PLAYER_1 : PLAYER_2);
}
//
//Helper methods
//
//printing methods
public static void print(String s)
{
System.out.print(s);
}
public static void println(String s)
{
System.out.println(s);
}
public static void newLine()
{
System.out.println();
}
public static void printBar()
{
print("__");
for (int i = 0; i < DIM; i++)
print("___");
print("__");
newLine();
}
public static void printBoard()
{
System.out.println("Here is the current state of the board:");
printBar();
for (int i = 0; i < DIM; i++)
{
print("|");
for (int j = 0; j < DIM; j++)
{
print("|" + board[i][j] + "|");
}
print("|");
newLine();
printBar();
}
newLine();
}
public static void printBoard(char[][] b)
{
System.out.println("Here is the current state of the board:");
printBar();
for (int i = 0; i < DIM; i++)
{
print("|");
for (int j = 0; j < DIM; j++)
{
print("|" + b[i][j] + "|");
}
print("|");
newLine();
printBar();
}
newLine();
}
//see if spots have just been captured
public static boolean spotsHaveJustBeenCaptured(char[][] b, char player)
{
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
if (board[i][j] == NO_WINNER &&
spotIsCaptured(new Point(i, j), b, player))
return true;
return false;
}
//check if a single spot is captured
private static boolean spotIsCaptured(Point spot, char[][] b, char player)
{
if (b[spot.x][spot.y] != NO_WINNER) return false;
//4 conditions must be true in order to be captured
boolean[] checks = new boolean[4];
if (spot.x > 0) checks[0] = (b[spot.x - 1][spot.y] == player);
else checks[0] = true; //it's against a wall on the left
if (spot.x < DIM - 1) checks[1] = (b[spot.x + 1][spot.y] == player);
else checks[1] = true; //it's against a wall on the right
if (spot.y > 0) checks[2] = (b[spot.x][spot.y - 1] == player);
else checks[2] = true; //it's against a wall on the top
if (spot.y < DIM - 1) checks[3] = (b[spot.x][spot.y + 1] == player);
else checks[3] = true; //it's against a wall on the right
for (int i = 0; i < 4; i ++)
if (!checks[i]) return false;
return true;
}
//fill captured spots
public static void fillCapturedSpots(char[][] b, char player)
{
for (int i = 0; i < DIM; i++)
for (int j = 0; j < DIM; j++)
if (spotIsCaptured(new Point(i, j), b, player))
b[i][j] = player;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// POINT /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//A standard geometric Point containing int x and int y
class Point
{
public int x;
public int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
//returns "x = #, y = #"
public String toString()
{
return "x = " + this.x + ", y = " + this.y;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// PLAYER /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//ADT of a player.
//Given a board, player can return a move.
class Player
{
public Point move(char[][] board)
{
//should be overridden
//default return result is invalid
return new Point(-1, -1);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// HUMAN PLAYER /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//Human player works by asking a human for the move.
class HumanPlayer extends Player
{
public Point move(char[][] board)
{
int row;
int col;
do
{
row = Og.getValidChoice(0, Og.DIM - 1, "Which row?");
col = Og.getValidChoice(0, Og.DIM - 1, "Which column?");
}
while (board[row][col] != Og.NO_WINNER);
return new Point(row, col);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// COMPUTER PLAYER /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//Computer Player uses miniMax to calculate move
class ComputerPlayer extends Player
{
//"infinity" values (that is, unreachable utility values)
public static final int INFINITY = Og.DIM * Og.DIM;
public static final int NEGATIVE_INFINITY = (-1 * Og.DIM * Og.DIM);
// ivars
private char playerChar;
private char otherPlayer;
private int count;
private int uniqueCount;
private static TTable tTable = new TTable();
public ComputerPlayer(char c, char o)
{
this.uniqueCount = 0;
this.count = 0;
this.otherPlayer = o;
this.playerChar = c;
}
public Point move(char[][] board)
{
boolean alphaBeta = true;
/* ********************************************************** *
* Uncomment the following line to turn OFF AlphaBeta Pruning *
* ********************************************************** */
//alphaBeta = false;
this.count = 0;
this.uniqueCount = 0;
Point p;
if (alphaBeta)
p = abMiniMaxDecision(board);
else
p = miniMaxDecision(board);
Og.println("Checked " + count + " states");
Og.println("Checked " + uniqueCount + " unique states");
return p;
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// NORMAL MINIMAX DECISION ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
private Point miniMaxDecision(char[][] board)
{
BoardState b;
List<Action> actions = getActions(new BoardState(board), this.playerChar);
for (Action a : actions)
{
b = new BoardState(board, a.p, this.playerChar);
BoardState match;
if ((match = tTable.containsBoard(b)) != null)
{
a.v = tTable.valForBoardState(match);
count++;
continue;
}
else
{
a.v = minValue(b);
tTable.addBoard(b, a.v);
count++;
uniqueCount++;
//if (a.v > WIN) return a.p;
}
}
return getMax(actions).p;
}
public int minValue(BoardState b)
{
//Og.println("in Min");
if (Og.terminalTest(b.board))
return utility(b.board);
int val = Og.DIM * Og.DIM;
for (Action a : getActions(b, this.otherPlayer))
{
BoardState next = new BoardState(b.board, a.p, this.otherPlayer);
BoardState match;
if ((match = tTable.containsBoard(next)) != null)
{
count++;
return tTable.valForBoardState(match);
}
a.v = val = Math.min(val, maxValue(next));
tTable.addBoard(next, a.v);
count++;
uniqueCount++;
}
return val;
}
public int maxValue(BoardState b)
{
//Og.println("in Max");
if (Og.terminalTest(b.board))
return utility(b.board);
int val = -(Og.DIM * Og.DIM);
for (Action a : getActions(b, this.playerChar))
{
BoardState next = new BoardState(b.board, a.p, this.playerChar);
BoardState match;
if ((match = tTable.containsBoard(next)) != null)
{
count++;
return tTable.valForBoardState(match);
}
a.v = val = Math.max(val, minValue(next));
tTable.addBoard(next, a.v);
count++;
uniqueCount++;
}
return val;
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// END NORMAL MINIMAX ////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Alpha Beta MINIMAX DECISION /////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
private Point abMiniMaxDecision(char[][] board)
{
BoardState b;
int alpha = NEGATIVE_INFINITY;
int beta = INFINITY;
List<Action> actions = getActions(new BoardState(board), this.playerChar);
for (Action a : actions)
{
count++;
b = new BoardState(board, a.p, this.playerChar);
BoardState match;
if ((match = tTable.containsBoard(b)) != null)
{
a.v = tTable.valForBoardState(match);
continue;
}
else
{
uniqueCount++;
a.v = minValue(b, alpha, beta);
tTable.addBoard(b, a.v);
}
}
return getMax(actions).p;
}
public int minValue(BoardState b, int alpha, int beta)
{
if (Og.terminalTest(b.board))
return utility(b.board);
int val = INFINITY;
for (Action a : getActions(b, this.otherPlayer))
{
count++;
BoardState next = new BoardState(b.board, a.p, this.otherPlayer);
BoardState match;
if ((match = tTable.containsBoard(next)) != null)
{
return tTable.valForBoardState(match);
}
uniqueCount++;
a.v = val = Math.min(val, maxValue(next, 0, 0));
tTable.addBoard(next, a.v);
if (val <= alpha)
return val;
beta = Math.min(beta, val);
}
return val;
}
public int maxValue(BoardState b, int alpha, int beta)
{
if (Og.terminalTest(b.board))
return utility(b.board);
int val = NEGATIVE_INFINITY;
for (Action a : getActions(b, this.playerChar))
{
count++;
BoardState next = new BoardState(b.board, a.p, this.playerChar);
BoardState match;
if ((match = tTable.containsBoard(next)) != null)
{
return tTable.valForBoardState(match);
}
uniqueCount++;
a.v = val = Math.max(val, minValue(next, 0, 0));
tTable.addBoard(next, a.v);
if (val >= beta)
return val;
alpha = Math.max(alpha, val);
}
return val;
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// END Alpha Beta MINIMAX///////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//return how many of your piece is on the
//final (full) board
private int utility(char[][] board)
{
//Og.println("in Utility");
int boardLength = board.length;
int count = 0;
for (int i = 0; i < boardLength; i++)
for (int j = 0; j < boardLength; j++)
{
if (board[i][j] == this.playerChar)
count++;
}
return count;
}
private List<Action> getActions(BoardState s, char player)
{
//Og.println("in GetActions");
List<Action> a = new CopyOnWriteArrayList<Action>();
int boardLength = s.board.length;
Point p1 = null;
for (int i = 0; i < boardLength; i++)
{
for (int j = 0; j < boardLength; j++)
{
if (s.board[i][j] != Og.NO_WINNER) continue;
Point p2;
boolean sym = false;
if (a.size() == 0)
a.add(new Action(new Point(i, j)));
for (Action act : a)
{
p2 = act.p;
if (TTable.areSymmetries(s.board, player,
(p1 = new Point(i,j)), p2))
{
sym = true;
break;
}
}
if (!sym)
a.add(new Action(p1));
}
}
return a;
}
public Action getMin(List<Action> list)
{
//collect garbage
System.gc();
Action act = null;
int lowest = (Og.DIM * Og.DIM);
for (Action a : list)
{
if (a.v < lowest)
{
lowest = a.v;
act = a;
}
}
return act;
}
public Action getMax(List<Action> list)
{
//collect garbage
System.gc();
Action act = null;
int highest = -(Og.DIM * Og.DIM);
for (Action a : list)
{
if (a.v > highest)
{
highest = a.v;
act = a;
}
}
return act;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// BOARD STATE /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//A board state takes either a board with no action,
//or a board with a given move played.
//It then checks to see if that move would allow
//any more spaces to become captured and if so
//processes subsequent capturing.
//The resulting board config is stored as a char[][]
class BoardState
{
public char[][] board;
public BoardState(char[][] b, Point p, char player)
{
int l = b.length;
this.board = new char[l][l];
for (int i = 0; i < l; i++)
for (int j = 0; j < l; j++)
this.board[i][j] = b[i][j];
this.board[p.x][p.y] = player;
if (Og.spotsHaveJustBeenCaptured(this.board, player))
{
Og.fillCapturedSpots(this.board, player);
//player gets 1 free move
boolean shouldBreak = false;
for (int i = 0; i < l; i++)
{
if (shouldBreak) break;
for (int j = 0; j < l; j++)
if (this.board[i][j] == Og.NO_WINNER)
{
this.board[i][j] = player;
shouldBreak = true;
break;
}
}
}
//fill any new captured spots, if any
if (Og.spotsHaveJustBeenCaptured(this.board, player))
Og.fillCapturedSpots(this.board, player);
}
public String toString()
{
String s = "";
int l = this.board.length;
for (int i = 0; i < l; i++)
for (int j = 0; j < l; j++)
s += this.board[i][j];
return s;
}
public BoardState(char[][] b)
{
int l = b.length;
this.board = new char[l][l];
for (int i = 0; i < l; i++)
for (int j = 0; j < l; j++)
this.board[i][j] = b[i][j];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// T TABLE /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
//Helper class containing a list of board state (char[][])
//and values associated with them stored as type Integer
class TTable
{
public List<BoardState> states;
public List<Integer> utility;
public List<String> stateStrings;
public TTable()
{
states = new CopyOnWriteArrayList<BoardState>();
utility = new CopyOnWriteArrayList<Integer>();
stateStrings = new CopyOnWriteArrayList<String>();
}
//check a point to see if adding it produces a symmetry
public static boolean areSymmetries(char[][] b, char player, Point p1, Point p2)
{
String s = (new BoardState(b, p1, player).toString());
for (String sym : getSymmetries(new BoardState(b, p2, player)))
if (sym.equals(s)) return true;
return false;
}
//checks all current board states and returns
//any that have identical board configurations
public BoardState containsBoard(BoardState b)
{
String s = b.toString();
//Og.println("in Max");
for (String state : this.stateStrings)
{
if (s.equals(state))
return this.states.get(this.stateStrings.indexOf(state));
}
return null;
}
//get all symmetries associated with a board state
public static List<String> getSymmetries(BoardState b)
{
List<String> symmetries = new CopyOnWriteArrayList<String>();
BoardState b2;
b2 = sym(b.board);
symmetries.add(b2.toString());
b2 = rotate(b.board);
symmetries.add(b2.toString());
b2 = trans(b.board);
symmetries.add(b2.toString());
b2 = symTrans(b.board);
symmetries.add(b2.toString());
b2 = rotTrans(b.board);
symmetries.add(b2.toString());
b2 = rotSym(b.board);
symmetries.add(b2.toString());
return symmetries;
}
//Add a board, associate a value with it,
//and generate all symmetries.
public void addBoard(BoardState b, int val)
{
BoardState b2;
this.states.add(b);
this.stateStrings.add(b.toString());
b2 = sym(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
b2 = rotate(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
b2 = trans(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
b2 = symTrans(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
b2 = rotTrans(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
b2 = rotSym(b.board);
this.states.add(b2);
this.stateStrings.add(b2.toString());
for (int i = 0; i < 7; i++)
this.utility.add(val);
}