-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathultimatettt.cpp
483 lines (432 loc) · 10.6 KB
/
ultimatettt.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define N 3
#define INF 0x3f3f3f3f
#define MAX_DEPTH 7
#define score 100
#define PII pair < int , int >
char board[N][N][N][N]; // Complete board
char Mboard[N][N]; // Main Board
char eval[N][N]; // temporary Board
// Sub Board score
int SubBoard() {
bool flag;
char c;
// left to right
for(int i=0;i<N;i++) {
c = eval[i][0];
flag = 1;
for(int j=0;j<N;j++){
if(eval[i][j]!=c) {
flag = 0;
break;
}
}
if(flag) {
if(c=='X') return score;
else if(c=='O') return -score;
}
}
// top to bottom
for(int i=0;i<N;i++) {
c = eval[0][i];
flag = 1;
for(int j=0;j<N;j++) {
if(eval[j][i]!=c) {
flag = 0;
break;
}
}
if(flag){
if(c=='X') return score;
else if(c=='O') return -score;
}
}
// diagnol - 1
c = eval[0][0];
flag = 1;
for(int i=0;i<N;i++) {
if(eval[i][i]!=c) {
flag = 0;
break;
}
}
if(flag){
if(c=='X') return score;
else if(c=='O') return -score;
}
// diagnol - 2
c = eval[0][N-1];
flag = 1;
for(int i=0;i<N;i++) {
if(eval[i][N-i-1]!=c) {
flag = 0;
break;
}
}
if(flag){
if(c=='X') return score;
else if(c=='O') return -score;
}
// done
// if still no return
// still we can play
for(int i=0;i<N;i++) for(int j=0;j<N;j++) if(eval[i][j]=='_') return -1;
// no we cant - its a TIE
return 0;
}
// initially all set to '_'
void init() {
for(int i=0;i<N;i++) for(int j=0;j<N;j++) for(int k=0;k<N;k++) for(int l=0;l<N;l++) board[i][j][k][l] = '_';
for(int i=0;i<N;i++) for(int j=0;j<N;j++) Mboard[i][j] = '_';
for(int i=0;i<N;i++) for(int j=0;j<N;j++) eval[i][j] = '_';
}
// give it to temporary board
void GiveEval(int x , int y) { // x=-1 && y=-1 -> main Board
if(x==-1 && y==-1) for(int i=0;i<N;i++) for(int j=0;j<N;j++) eval[i][j] = Mboard[i][j];
else for(int i=0;i<N;i++) for(int j=0;j<N;j++) eval[i][j] = board[x][y][i][j];
}
// evaluate winner of a sub-board & Main Board
char GetWinner(int x , int y) { // x=-1 && y=-1 -> main Board
GiveEval(x , y);
int sc = SubBoard();
if(sc==score) return 'X';
else if(sc==-score) return 'O';
else if(sc==0) return 'T';
else if(sc==-1) return '_';
cerr<< "HELLL NOO!" << endl;
return '_';
}
// update Main Board whenever sub board is updated
void UpdateMainBoard(int x , int y) {
Mboard[x][y] = GetWinner(x , y);
}
// Read Input
void ReadBoard() {
for(int i=0;i<N;i++) for(int j=0;j<N;j++) for(int k=0;k<N;k++) for(int l=0;l<N;l++) cin >> board[i][k][j][l];
// converting board to my format
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[i][j][k][l]=='x') board[i][j][k][l] = 'X';
else if(board[i][j][k][l]=='o') board[i][j][k][l] = 'O';
else if(board[i][j][k][l]=='-') board[i][j][k][l] = '_';
}
}
}
}
}
// Each sub - board of size N x N
void PrintSubBoard(int x, int y) {
cout << endl << "Sub Board " << x << "," << y << " :" << endl;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++){
cout << board[x][y][i][j];
}cout << endl;
}
}
// ultimate board of size N x N
void PrintMainBoard() {
cout << endl << "Main Board :" << endl;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
cout << Mboard[i][j];
}cout << endl;
}
}
// Complete Board of size N x N x N x N
void PrintFullBoard() {
cout << endl << "Full Board :" << endl;
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
cout << board[i][k][j][l];
}
}cout << endl;
}
}
}
// swap players if current player is 'O'
void SwapPlayers() {
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[i][j][k][l]=='X') board[i][j][k][l] = 'O';
else if(board[i][j][k][l]=='O') board[i][j][k][l] = 'X';
}
}
}
}
}
int scoreline(int me , int opp) {
if(opp==0) return (int)(pow(10 , me - 1));
if(me==0) return (int)(pow(10 , opp - 1));
return 0;
}
// Evaluate Half Played Board
int EvaluateMainBoard() {
bool flag;
int sc = 0 , me , opp;
// left to right
for(int i=0;i<N;i++) {
flag = 0;
me = 0;
opp = 0;
for(int j=0;j<N;j++) {
if(eval[i][j]=='T') {
flag = 1;
break;
}
else if(eval[i][j]=='X') me++;
else if(eval[i][j]=='O') opp++;
}
if(!flag) sc += scoreline(me , opp);
}
// top to bottom
for(int j=0;j<N;j++) {
flag = 0;
me = 0;
opp = 0;
for(int i=0;i<N;i++) {
if(eval[i][j]=='T') {
flag = 1;
break;
}
else if(eval[i][j]=='X') me++;
else if(eval[i][j]=='O') opp++;
}
if(!flag) sc += scoreline(me , opp);
}
// diagnol - 1
me = 0;
opp = 0;
flag = 0;
for(int i=0;i<N;i++) {
if(eval[i][i]=='T') {
flag = 1;
break;
}
else if(eval[i][i]=='X') me++;
else if(eval[i][i]=='O') opp++;
}
if(!flag) sc += scoreline(me , opp);
// diagnol - 2
me = 0;
opp = 0;
flag = 0;
for(int i=0;i<N;i++) {
if(eval[i][N-i-1]=='T') {
flag = 1;
break;
}
else if(eval[i][N-i-1]=='X') me++;
else if(eval[i][N-i-1]=='O') opp++;
}
if(!flag) sc += scoreline(me , opp);
return sc;
}
// minimax with alpha-beta pruning
int minimax(int x , int y , int turn , int depth , int alpha , int beta) {
char MainWinner = GetWinner(-1 , -1);
if(MainWinner=='_') { // no winner
if(depth==MAX_DEPTH) { // no particular winner but Max depth reached so evaluate half played board
GiveEval(-1 , -1);
int sc = EvaluateMainBoard();
return sc;
}
else { // max depth not reached continue
if(turn) { // maximizer
int best = -INF;
if((x==-1 && y==-1) || (Mboard[x][y]=='X') || (Mboard[x][y]=='O') || (Mboard[x][y]=='T')) {
// now he can go to any empty space
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[i][j][k][l]=='_') {
board[i][j][k][l] = 'X';
UpdateMainBoard(i , j);
best = max(best , minimax(k , l , turn^1 , depth + 1 , alpha , beta));
alpha = max(alpha , best);
board[i][j][k][l] = '_';
UpdateMainBoard(i , j);
if(beta <= alpha) break;
}
}
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
return best;
}
else { // fixed
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[x][y][k][l]=='_') {
board[x][y][k][l] = 'X';
UpdateMainBoard(x , y);
best = max(best , minimax(k , l , turn^1 , depth + 1 , alpha , beta));
alpha = max(alpha , best);
board[x][y][k][l] = '_';
UpdateMainBoard(x , y);
if(beta <= alpha) break;
}
}
if(beta <= alpha) break;
}
return best;
}
}
else { // minimizer
int best = INF;
if((x==-1 && y==-1) || (Mboard[x][y]=='X') || (Mboard[x][y]=='O') || (Mboard[x][y]=='T')) {
// now he can go to any empty space
for(int i=0;i<N;i++) {
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[i][j][k][l]=='_') {
board[i][j][k][l] = 'O';
UpdateMainBoard(i , j);
best = min(best , minimax(k , l , turn^1 , depth + 1 , alpha , beta));
beta = min(beta , best);
board[i][j][k][l] = '_';
UpdateMainBoard(i , j);
if(beta <= alpha) break;
}
}
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
if(beta <= alpha) break;
}
return best;
}
else { // fixed
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[x][y][k][l]=='_') {
board[x][y][k][l] = 'O';
UpdateMainBoard(x , y);
best = min(best , minimax(k , l , turn^1, depth + 1 , alpha , beta));
beta = min(beta , best);
board[x][y][k][l] = '_';
UpdateMainBoard(x , y);
if(beta <= alpha) break;
}
}
if(beta <= alpha) break;
}
return best;
}
}
}
}
else { // we got some winner or tie
if(MainWinner=='T') return 0;
else {
GiveEval(-1 , -1);
int sc = SubBoard();
if(sc==score) return sc - depth;
else return sc + depth;
}
}
}
// Get Best Move for 'X'
pair < PII , PII > GetBestMove(int x , int y) {
int best = -INF , tmpbest;
pair < int , int > Mans; // Main block ans
pair < int , int > Sans; // Sub block ans
if((x==-1 && y==-1) || (Mboard[x][y]=='X') || (Mboard[x][y]=='O') || (Mboard[x][y]=='T')) {
// now he can go to any empty space
for(int i=0;i<N;i++){
for(int j=0;j<N;j++) {
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[i][j][k][l]=='_') {
board[i][j][k][l] = 'X';
UpdateMainBoard(i, j); // update Main board for every sub board change
tmpbest = minimax(k , l , 0 , 0 , -INF , INF);
if(best < tmpbest) {
best = tmpbest;
Mans = {i , j};
Sans = {k , l};
}
board[i][j][k][l] = '_';
UpdateMainBoard(i , j); // update Main board for every sub board change
}
}
}
}
}
}
else { // we should go to suggested block
Mans = {x , y}; // fixed
for(int k=0;k<N;k++) {
for(int l=0;l<N;l++) {
if(board[x][y][k][l]=='_') {
board[x][y][k][l] = 'X';
UpdateMainBoard(x , y); // update Main board for every sub board change
tmpbest = minimax(k , l , 0 , 0 , -INF , INF);
if(best < tmpbest) {
best = tmpbest;
Sans = {k , l};
}
board[x][y][k][l] = '_';
UpdateMainBoard(x , y); // update Main board for every sub board change
}
}
}
}
//cout << endl << "score for position = " << best << endl;
board[Mans.first][Mans.second][Sans.first][Sans.second] = 'X';
return {Mans , Sans};
}
/* STEPS:
1. Read Board
2. Player
3. Start Block
*/
int main(int argc, char const *argv[])
{
//freopen("input.txt" ,"r" , stdin);
//freopen("output.txt" , "w", stdout);
init();
// read player
bool PlayersSwapped = 0;
char player;
cin >> player;
// Start block for X
int x, y;
cin >> x >> y;
// Read Board
ReadBoard();
// Print Board before best move
//PrintFullBoard();
// if X then OK else change
if(player=='O' || player=='o') {
SwapPlayers();
PlayersSwapped = 1;
}
// now get Main board for given board
for(int i=0;i<N;i++) for(int j=0;j<N;j++) UpdateMainBoard(i , j);
// now get best move for X
pair < PII , PII > bstmove = GetBestMove(x , y);
//cout << endl << "BEST move is : " << endl;
cout << bstmove.first.first << " " << bstmove.first.second << " ";
cout << bstmove.second.first << " " << bstmove.second.second << endl;
// if players are swapped return to normal
if(PlayersSwapped) {
SwapPlayers();
PlayersSwapped = 0;
}
// Print Full Board after best move
//PrintFullBoard();
return 0;
}