-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
389 lines (324 loc) · 10.6 KB
/
Board.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
/**
*
* Author: Ananya Kumar
* Institution: NUS High School
* 2011
*
* The Board class represents the Board data structure, representing the core of this project. Includes
* methods to add pieces, find entanglement, collapse entanglements, evaluate the winner.
*
* Note all data structures are 0-based. For displaying purposes you (probably) need to transform them.
* Eg. Player 1s pieces are 0, 2, 4, ...
*
**/
import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class Board
{
ArrayList<Integer>[] b; //b[i] stores the list of pieces currently on the i^th square
boolean[] isClassical; //isCLassical[i] = is square i is classical?
ArrayList<Integer>[] p; //p[i] stores the list of squares the i^th piece was played on
Stack<ArrayList<Integer>> strace; //Top of stack stores latest pieces that have been collapsed
int numSquares; //Number of squares in the game board
int piecesPerTurn; //Pieces played per turn at start of game
int turn; //The current turn number (0-based)
int squaresLeft; //Number of non-classical squares on the board
boolean cycle; //Is there an entanglement
boolean justCounting; //True if this board is used only for game tree enumeration
public Board ( int m, int n ) //Initialize the board to have m squares and n pieces played per turn
{
b = new ArrayList[m];
for ( int i = 0; i < m; i++ ) b[i] = new ArrayList<Integer>();
isClassical = new boolean[m];
Arrays.fill(isClassical,false);
p = new ArrayList[m];
for ( int i = 0; i < m; i++ ) p[i] = new ArrayList<Integer>();
strace = new Stack<ArrayList<Integer>>();
numSquares = m;
piecesPerTurn = n;
turn = 0;
squaresLeft = numSquares;
}
public Board ( int m, int n, boolean countop ) //Additional option, true if board is being used only for counting
{
this(m,n);
justCounting = countop;
}
public Board ( int m ) { this(m,2); } //Initialize the board to have m squares and 2 pieces played per turn
public Board () { this(9,2); } //Initialize the board to have 9 squares and 2 pieces played per turn
/**
* Methods for adding pieces onto the board
**/
public int howManyPieces () //How many pieces should a player play per turn
{
if ( squaresLeft < piecesPerTurn ) return 1;
else return piecesPerTurn;
}
public int howManyPiecesLeft () //How many pieces are yet to be played in the current turn
{
return howManyPieces() - p[turn].size();
}
public boolean addPiece ( int square ) //Add quantum piece to specified square, if move is valid
{
if ( isClassical[square] ) return false;
for ( int i = 0; i < p[turn].size(); i++ ) if ( square == p[turn].get(i) ) return false;
p[turn].add(square);
b[square].add(turn);
if ( p[turn].size() == howManyPieces() ) finalizeMove();
return true;
}
private void finalizeMove () //When all quantum moves this turn have been played, the move will be finalized
{
strace.push(new ArrayList<Integer>());
if ( p[turn].size() == 1 ) collapseTo(turn,p[turn].get(0));
turn++;
checkEntanglement();
}
public void undoAdd () //Undo the addition of one of the moves (without undoing collapse)
{
if ( turn == 0 && p[turn].size() == 0 ) return;
if ( turn >= numSquares || p[turn].size() == 0 )
{
turn--;
strace.pop();
}
ArrayList<Integer> cursqr = b[p[turn].get(p[turn].size()-1)];
cursqr.remove(cursqr.size()-1);
p[turn].remove(p[turn].size()-1);
cycle = false;
}
/**
* Methods for checking whether there is an entanglement. Consider moving these to separate class.
**/
public boolean isEntangled () //Return true if and only if there is an entanglement in the board
{
return cycle;
}
private void checkEntanglement () //Checks if there is an entanglement in the board
{
if ( piecesPerTurn == 2 ) checkCycleEntanglement();
else if ( justCounting ) checkCountingEntanglement();
else checkMatchingEntanglement();
}
private void checkCycleEntanglement () //Checks if there is a cyclic entanglement in the board (piecesPerTurn must be 2)
{
cycle = false;
if ( turn == 0 ) return;
if ( isClassical[p[turn-1].get(0)] ) return; //If it has been collapsed then there isn't a cycle
cycleEntanglement(turn-1,p[turn-1].get(0),new boolean[numSquares]);
}
private void cycleEntanglement ( int cpiece, int csquare, boolean[] visited ) //Recursive depth-first search function
{
if ( visited[csquare] ) { cycle = true; return; }
ArrayList<Integer> sqr = b[csquare];
int npiece, nsquare;
visited[csquare] = true;
for ( int i = 0; i < sqr.size(); i++ )
{
npiece = sqr.get(i);
if ( npiece == cpiece ) continue;
nsquare = p[npiece].get(0);
if ( nsquare == csquare ) nsquare = p[npiece].get(1);
cycleEntanglement(npiece,nsquare,visited);
if ( cycle ) break;
}
}
private void checkCountingEntanglement ()
{
}
private void checkMatchingEntanglement () //Custom algorithm to find if there is an entanglement (got any piecesPerTurn value)
{
}
/**
* Methods for collapsing an entanglement. Only works for 2-pieces per turn at the moment
**/
public boolean collapse ( int sqrnum ) //Main collapse function: to collapse the latest piece to sqrnum
{
boolean validCollapse = false;
for (int i = 0; i < p[turn-1].size(); i++)
{
if (p[turn-1].get(i) == sqrnum) validCollapse = true;
}
if (validCollapse) {
cycleCollapse(turn-1,sqrnum);
cycle = false;
}
return validCollapse;
}
public void collapse ( int piecenum, boolean extra ) //This collapse method allows you to scan through all possible collapse options for the latest piece by specifying piecenum = 1...n
{
cycleCollapse(turn-1,p[turn-1].get(piecenum));
cycle = false;
}
private void cycleCollapse ( int cpiece, int csquare ) //Recursive method for collapse
{
if ( isClassical[csquare] ) return;
(strace.peek()).add(cpiece);
isClassical[csquare] = true;
squaresLeft--;
ArrayList<Integer> sqr = b[csquare];
int newpiece, newsqr;
for ( int i = 0; i < sqr.size(); i++ )
{
newpiece = sqr.get(i);
if ( newpiece == cpiece ) continue;
else newsqr = p[newpiece].get(0);
if ( newsqr == csquare ) newsqr = p[newpiece].get(1);
cycleCollapse(newpiece,newsqr);
}
b[csquare] = new ArrayList<Integer>();
b[csquare].add(cpiece);
}
private void collapseTo ( int pnum, int sqrnum ) //Collapse piece pnum to square sqrnum (depracated)
{
if ( isClassical[sqrnum] ) return;
b[sqrnum] = new ArrayList<Integer>();
b[sqrnum].add(pnum);
(strace.peek()).add(pnum);
isClassical[sqrnum] = true;
squaresLeft--;
}
public boolean hasCollapsed () //Has a collapse just occured?
{
if ( strace.empty() ) return false;
else return (turn == numSquares || (p[turn].size() == 0)) && ((strace.peek()).size() > 0) && isClassical[p[strace.peek().get(0)].get(0)];
}
public void undoCollapse () //Undo the collapse that occured at start of the current turn (if there was no collapse it does nothing)
{
ArrayList<Integer> collapsed = strace.peek();
if ( collapsed.size() > 0 ) cycle = true;
Collections.sort(collapsed);
int i, j, m, curp;
for ( i = 0; i < collapsed.size(); i++ ) //For each piece
{
curp = collapsed.get(i); //Get the piece index
for ( j = 0; j < p[curp].size(); j++ ) //For each square piece was placed on
{
m = p[curp].get(j); //Find the square index
if ( isClassical[m] ) //Restore the square if not done so before
{
isClassical[m] = false;
b[m].remove(b[m].size()-1);
squaresLeft++;
}
b[m].add(curp); //Restore the piece to the square
}
}
(strace.peek()).clear();
}
/*******************
* Utility methods
*******************/
public int getNumSquares ()
{
return numSquares;
}
public int numSquarePieces ( int sqrnum ) //Returns the number of pieces on the square
{
if ( sqrnum < 0 || sqrnum >= numSquares ) return 0;
else return b[sqrnum].size();
}
public boolean isSquareClassical ( int sqrnum )
{
if ( sqrnum < 0 || sqrnum >= numSquares ) return false;
return isClassical[sqrnum];
}
public int pieceAt ( int sqrnum, int subsqrnum )
{
if ( sqrnum < 0 || sqrnum >= numSquares ) return -1;
if ( subsqrnum >= (b[sqrnum]).size() ) return -1;
else return (b[sqrnum]).get(subsqrnum);
}
public int getTurn ()
{
return turn;
}
public boolean gameOver () //Returns true iff the game is over
{
return squaresLeft == 0;
}
public double getWinner () //Returns player 1's score - player 2's score
{
ArrayList<Integer> p1lines = new ArrayList<Integer>();
ArrayList<Integer> p2lines = new ArrayList<Integer>();
int pline;
int maxTurn;
int curpiece;
int i, j;
double winMargin = 0;
int n = (int)(Math.sqrt(numSquares));
for ( i = 0; i < n; i++ ) //There are actually more elegant ways to do this, but they are slower
{
pline = 0;
maxTurn = 0;
for ( j = 0; j < n; j++ )
{
curpiece = (b[i*n+j]).get(0);
maxTurn = Math.max(curpiece,maxTurn);
pline += curpiece%2;
if ( 0 < pline && pline <= j ) break;
}
if ( pline == 0 ) p1lines.add(maxTurn);
else if ( pline == n ) p2lines.add(maxTurn);
pline = 0;
maxTurn = 0;
for ( j = 0; j < n; j++ )
{
curpiece = (b[i+j*n]).get(0);
maxTurn = Math.max(curpiece,maxTurn);
pline += curpiece%2;
if ( 0 < pline && pline <= j ) break;
}
if ( pline == 0 ) p1lines.add(maxTurn);
else if ( pline == n ) p2lines.add(maxTurn);
}
pline = 0;
maxTurn = 0;
for ( j = 0; j < n; j++ )
{
curpiece = (b[j*n+j]).get(0);
maxTurn = Math.max(curpiece,maxTurn);
pline += curpiece%2;
if ( 0 < pline && pline <= j ) break;
}
if ( pline == 0 ) p1lines.add(maxTurn);
else if ( pline == n ) p2lines.add(maxTurn);
pline = 0;
maxTurn = 0;
for ( j = 0; j < n; j++ )
{
curpiece = (b[j*n+n-j-1]).get(0);
maxTurn = Math.max(curpiece,maxTurn);
pline += curpiece%2;
if ( 0 < pline && pline <= j ) break;
}
if ( pline == 0 ) p1lines.add(maxTurn);
else if ( pline == n ) p2lines.add(maxTurn);
p1lines.add(1000000);
p2lines.add(1000000);
Collections.sort(p1lines);
Collections.sort(p2lines);
i = j = pline = 0;
maxTurn = -1;
while ( i+j < p1lines.size() + p2lines.size() - 2 )
{
if ( p1lines.get(i) <= p2lines.get(j) )
{
if ( p1lines.get(i) > maxTurn ) pline++;
winMargin += 1.0/pline;
maxTurn = p1lines.get(i);
i++;
}
else if ( p2lines.get(j) < p1lines.get(i) )
{
if ( p2lines.get(j) > maxTurn ) pline++;
winMargin -= 1.0/pline;
maxTurn = p2lines.get(j);
j++;
}
}
return winMargin;
}
}