-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup_table.cc
358 lines (320 loc) · 8.02 KB
/
lookup_table.cc
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
#include <iostream>
#include <algorithm>
#include <functional>
#include <fstream>
using namespace std;
#define PILE_LENGTH_MAX 40
// Datastructure in which the state of the game is saved
struct Board {
int subboard[PILE_LENGTH_MAX];
void print( ostream& );
int cut( int i );
int find( int i );
int add( int i );
void serialize();
Board();
};
// Removes a subboard with index i from the board
// Preserves canonical representation
int Board::cut( int i ) {
int temp = subboard[i];
while( subboard[i] != 0 ) {
subboard[i] = subboard[i+1];
i++;
}
return temp;
}
// Finds the index of a subboard of given size s
int Board::find( int s ) {
int j = 0;
while( subboard[j] > s ) j++;
if( subboard[j] == s )
return j;
else
return -1;
}
// Adds a subboard of size s to the board
// Preserves canonical representation
int Board::add( int s ) {
int j = 0;
while( subboard[j] != 0 ) j++;
while( subboard[j] < s && j >= 0 ) {
subboard[j] = subboard[j-1];
j--;
}
subboard[j+1] = s;
return j+1;
}
// Converts the current board to its canonical representation
// Canonical representation is descending order of subboard sizes
void Board::serialize() {
sort( subboard, subboard+PILE_LENGTH_MAX, greater<int>() );
}
// Prints the board to the output stream os
// e.g.: print(std::cout)
void Board::print( ostream& os ) {
os << "[" << subboard[0];
for( int i = 1; i < PILE_LENGTH_MAX && subboard[i] > 0; i++ )
os << "," << subboard[i];
os << "]";
}
// Constructor
Board::Board() {
for( int i = 0; i < PILE_LENGTH_MAX; i++ )
subboard[i] = 0;
}
// Datastructure in which the results of known games is stored
class Tree {
struct Node {
int win;
int child_count;
struct Child {
int n;
Node* ch;
};
Child* child;
int findChild( int i );
int addChild( int i );
int find( Board, int );
void add( Board, int, int );
void print( ostream&, int );
Node();
};
Node head;
public:
int find( Board );
void add( Board, int );
void print( ostream& );
};
// Add a child of value v to the node
int Tree::Node::addChild( int v ) {
int j;
int k;
Child* temp = child;
child = new Child[ child_count + 1 ];
for( j = 0; j < child_count; j++ )
if( temp[j].n < v )
child[j] = temp[j];
else
break;
child[j].n = v;
child[j].ch = new Node;
k = j;
for(; j < child_count; j++ )
child[j+1] = temp[j];
delete [] temp;
child_count++;
return k;
}
// Find the index of the child with value v
int Tree::Node::findChild( int v ) {
int j;
for( j = 0; j < child_count; j++ )
if( child[j].n >= v )
break;
if( j == child_count || child[j].n != v )
return -1;
else
return j;
}
// Find the the result of the game board in the tree recursively
int Tree::Node::find( Board board, int depth ) {
if( board.subboard[depth] == 0 ) {
return win;
} else {
int c = findChild( board.subboard[depth] );
if( c == -1 )
return 0;
else
return child[c].ch->find( board, depth + 1 );
}
}
// Add a game board with result w to the tree
void Tree::Node::add( Board board, int w, int depth ) {
if( board.subboard[ depth ] == 0 )
win = w;
else {
int c = findChild( board.subboard[ depth ] );
if( c == -1 )
c = addChild( board.subboard[ depth ] );
child[ c ].ch->add( board, w, depth + 1 );
}
}
// Print a node to the output stream os
void Tree::Node::print( ostream& os, int depth ) {
os << "(" << win << ")";
for( int i = 0; i < child_count; i++ ) {
os << "{" << child[i].n << ":";
child[i].ch->print( os, depth+1 );
os << "}";
}
}
// Constructor
Tree::Node::Node() {
child_count = 0;
child = nullptr;
win = 0;
}
// Find the result of game board
int Tree::find( Board board ) {
return head.find( board, 0 );
}
// Add the result w of a game board to the tree
void Tree::add( Board board, int w ) {
head.add( board, w, 0 );
}
// Print the tree to the output stream os
void Tree::print( ostream& os ) {
head.print( os, 0 );
os << endl;
}
// Global lookup tree, because me lazy
// srsly, wadja gonna do bout it
Tree lookup[2];
// Calculate the number of unique moves on a subboard of size s
int getSubmoveCount( int s ) {
return ( s - 1 )/2 + 1;
}
// Converts a submove number to its resulting subboards a and b
void getMoveResult( int s, int submove, int& a, int& b ) {
a = max( 0, submove - 1 );
b = s - min( submove + 2, s );
}
// Recursively determine whether the board is winning
// Returns 1 for winning and -1 for losing
template< bool force_lose = false >
int recursive( Board board, int& moveA, int& moveB ) {
// check if result of board is known
int result = lookup[force_lose].find( board );
if( result != 0 )
return result;
if( force_lose )
result = 1;
else
result = -1;
for( int subboard = 0; subboard < PILE_LENGTH_MAX; subboard++ ) {
if( board.subboard[subboard] == 0 )
break;
if( subboard == 0 || board.subboard[subboard] != board.subboard[subboard-1] ) {
// do move
int subboard_size = board.cut( subboard );
// calculate submoves
int submove_count = getSubmoveCount( subboard_size );
for( int submove = 0; submove < submove_count; submove++ ) {
int a = 0;
int b = 0;
// do submove
getMoveResult( subboard_size, submove, a, b );
board.add(a);
board.add(b);
// play recursively
result = -recursive< force_lose >( board, moveA, moveB );
// undo submove
board.cut( board.find(a) );
board.cut( board.find(b) );
if( result > 0 ) {
// if player can win, return a win
moveA = subboard_size;
moveB = submove;
board.add( subboard_size );
lookup[force_lose].add( board, 1 );
return result;
}
}
// undo move
board.add( subboard_size );
}
}
// player can't win, return a loss
lookup[force_lose].add( board, result );
return result;
}
int main( int argc, char** argv ) {
Board generate;
int a = 0, b = 0;
// read parameters
bool data_output = false;
bool force_lose = false;
bool query_interface = false;
string output_file_name;
for( int i = 1; i < argc; i++ ) {
if( argv[i][0] == '-' ) {
switch( argv[i][1] ) {
case 'd':
data_output = true;
if( i + 1 < argc )
output_file_name = argv[i+1];
else
output_file_name = "output.csv";
break;
case 'l':
force_lose = true;
break;
case 'q':
query_interface = true;
break;
case '?': case 'h':
cout << "usage: lookup_table [-d|-q] [-l]" << endl << endl;
cout << " -d, generate datafile" << endl;
cout << " -l, consider losing the desirable outcome" << endl;
cout << " -q, enter query interface" << endl;
return 0;
break;
default:
cerr << "Invalid parameter \"" << argv[i] << "\"" << endl;
}
} else
cerr << "Expecting parameter instead of \"" << argv[i] << "\"" << endl;
}
if( query_interface && data_output ) {
cerr << "Error: Can't both generate datafile and enter query interface" << endl;
return -1;
}
// execute function
if( data_output ) {
// generate data
ofstream dataout( output_file_name, fstream::trunc );
for( int i = 1; i < 200; i++ ) {
cout << i << endl;
for( int j = 0; j < i; j++ ) {
getMoveResult( i, j, generate.subboard[0], generate.subboard[1] );
generate.serialize();
if( force_lose )
dataout << (-recursive<true>( generate, a, b )) << " ";
else
dataout << (-recursive( generate, a, b )) << " ";
}
dataout << endl;
}
} else if ( query_interface ) {
// enter query interface
int n;
char l;
while(true) {
cin >> l;
cin >> n;
for( int i = 0; i < n; i++ )
cin >> generate.subboard[i];
for( int i = n; i < PILE_LENGTH_MAX; i++ )
generate.subboard[i] = 0;
generate.serialize();
if( l == 'l' )
cout << recursive<true>( generate, a, b );
else
cout << recursive( generate, a, b );
cout << "(" << a << "," << b << ")" << endl;
}
} else {
// play game for i = 0 ... 199
for( int i = 0; i < 200; i++ ) {
generate.subboard[0] = i;
generate.serialize();
if( force_lose )
cout << i << ":" << recursive<true>( generate, a, b );
else
cout << i << ":" << recursive( generate, a, b );
cout << "(" << a << "," << b << ")" << endl;
}
}
return 0;
}