-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmablin.c
262 lines (213 loc) · 6.18 KB
/
mmablin.c
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
// Order and Chaos AI: Minimax/alpha-beta pruning. - Tom Smeding
#include "params.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include "ai.h"
#ifndef ASSERTS
#define NDEBUG
#endif
#include <assert.h>
#define INF 1000000000 //1e9
#include "winmasks.h"
const uint64_t fullmask=(1ULL<<(N*N))-1;
#define APPLY(bitmap,idx) do {(bitmap)|=1ULL<<(idx);} while(0)
#define REMOVE(bitmap,idx) do {(bitmap)&=~(1ULL<<(idx));} while(0)
#define ISEMPTY(bitmap,idx) (!((bitmap)&(1ULL<<(idx))))
static int max(int a,int b){return b>a?b:a;}
static int min(int a,int b){return b<a?b:a;}
static int reduceabs(int a,int d){return a>d?a-d:a<-d?a+d:0;}
Board* makeboard(void){
return calloc(1,sizeof(Board));
}
void applymove(Board *board,Move mv){
APPLY(board->b[mv.stone],mv.pos);
}
bool isempty(const Board *board,int pos){
return ISEMPTY(board->b[0]|board->b[1],pos);
}
void printboard(const Board *board){
uint64_t b0=board->b[0],b1=board->b[1];
for(int y=0;y<N;y++){
for(int x=0;x<N;x++){
fputc(".OX"[(b0&1)+2*(b1&1)],stderr);
fputc(' ',stderr);
b0>>=1;
b1>>=1;
}
fputc('\n',stderr);
}
}
int checkwin(const Board *board){
for(int i=0;i<nwinmasks;i++){
if((board->b[0]&winmasks[i])==winmasks[i]||
(board->b[1]&winmasks[i])==winmasks[i])return ORDER;
}
if(((board->b[0]|board->b[1])&fullmask)==fullmask)return CHAOS;
return -1;
}
// High scores are good for Order, low are good for Chaos.
static int evaluate(const Board *board,int win){
if(win!=-1){
return 200*(1-2*win); // positive for Order, negative for Chaos
}
int x,y,i,co1,cx1,co2,cx2;
uint64_t m;
int score=0;
// Horizontal (1), vertical (2)
for(y=0;y<N;y++)for(x=0;x<2;x++){
co1=cx1=co2=cx2=0;
for(i=0;i<N-1;i++){
m=1ULL<<(N*y+x+i); // horizontal
co1+=(board->b[0]&m)!=0;
cx1+=(board->b[1]&m)!=0;
m=1ULL<<(N*(x+i)+y); //vertical
co2+=(board->b[0]&m)!=0;
cx2+=(board->b[1]&m)!=0;
}
if(co1==0)score+=cx1;
if(cx1==0)score+=co1;
if(co2==0)score+=cx2;
if(cx2==0)score+=co2;
}
// Diagonal \ (1), / (2)
for(y=0;y<2;y++)for(x=0;x<2;x++){
co1=cx1=co2=cx2=0;
for(i=0;i<N-1;i++){
m=1ULL<<(N*(y+i)+x+i); // \.
co1+=(board->b[0]&m)!=0;
cx1+=(board->b[1]&m)!=0;
m=1ULL<<(N*(N-1-y-i)+x+i); // /
co2+=(board->b[0]&m)!=0;
cx2+=(board->b[1]&m)!=0;
}
if(co1==0)score+=cx1;
if(cx1==0)score+=co1;
if(co2==0)score+=cx2;
if(cx2==0)score+=co2;
}
return score;
}
#define HASHSZ 1000003
typedef struct Hashitem{
Board board;
int player,alpha,beta,depth;
int score;
} Hashitem;
Hashitem boardhash[HASHSZ]={{{{0,0}},0,0,0,0,0}};
static int hash(const Board *board){
return (board->b[0]^(board->b[1]<<28))%HASHSZ;
}
// From Wikipedia: https://en.wikipedia.org/wiki/Alpha–beta_pruning#Pseudocode
static int alphabeta(Board *board,int player,int alpha,int beta,int depth){
assert(board->b[0]!=0||board->b[1]!=0);
const int hh=hash(board);
Hashitem *hi=boardhash+hh;
if(hi->board.b[0]==board->b[0]&&hi->board.b[1]==board->b[1]&&
hi->player==player&&hi->alpha==alpha&&hi->beta==beta&&hi->depth==depth){
return hi->score;
}
#define RETURN_STORE(sc) do { \
hi->board=*board; \
hi->player=player; hi->alpha=alpha; hi->beta=beta; hi->depth=depth; \
return hi->score=sc; \
} while(0)
//#define RETURN_STORE(sc) return sc
int win=checkwin(board);
if(depth==0||win!=-1){
RETURN_STORE(evaluate(board,win));
}
int best;
if(player==ORDER){
best=-INF;
for(int p=0;p<N*N;p++){
if(!ISEMPTY(board->b[0]|board->b[1],p))continue;
APPLY(board->b[0],p);
best=max(best,reduceabs(alphabeta(board,!player,alpha,beta,depth-1),1));
REMOVE(board->b[0],p);
alpha=max(alpha,best);
if(beta<=alpha)RETURN_STORE(INF); // exact value doesn't matter, but >beta
APPLY(board->b[1],p);
best=max(best,reduceabs(alphabeta(board,!player,alpha,beta,depth-1),1));
REMOVE(board->b[1],p);
alpha=max(alpha,best);
if(beta<=alpha)RETURN_STORE(INF); // exact value doesn't matter, but >beta
}
} else {
best=INF;
for(int p=0;p<N*N;p++){
if(!ISEMPTY(board->b[0]|board->b[1],p))continue;
APPLY(board->b[0],p);
best=min(best,reduceabs(alphabeta(board,!player,alpha,beta,depth-1),1));
REMOVE(board->b[0],p);
beta=min(beta,best);
if(beta<=alpha)RETURN_STORE(-INF); // exact value doesn't matter, but <alpha
APPLY(board->b[1],p);
best=min(best,reduceabs(alphabeta(board,!player,alpha,beta,depth-1),1));
REMOVE(board->b[1],p);
beta=min(beta,best);
if(beta<=alpha)RETURN_STORE(-INF); // exact value doesn't matter, but <alpha
}
}
RETURN_STORE(best);
#undef RETURN_STORE
}
typedef struct Moveorderitem {
int p,stone,score;
} Moveorderitem;
static int moveorderitem_compare(const void *a,const void *b){
return ((Moveorderitem*)b)->score-((Moveorderitem*)a)->score;
}
Move calcmove(Board *board,int player){
/*if(board->b[0]==0&&board->b[1]==0){
Move mv={player==ORDER?N*(N/2)+N/2:1,XX};
return mv;
}*/
Moveorderitem mvs[72];
int nmvs=0;
int win;
for(int p=0;p<N*N;p++){
if(!ISEMPTY(board->b[0]|board->b[1],p))continue;
for(int stone=0;stone<2;stone++){
mvs[nmvs].p=p;
mvs[nmvs].stone=stone;
APPLY(board->b[stone],p);
win=checkwin(board);
if(win==player){
REMOVE(board->b[stone],p);
Move mv={p,stone};
return mv;
}
mvs[nmvs].score=evaluate(board,win)*(1-2*player);
REMOVE(board->b[stone],p);
nmvs++;
}
}
qsort(mvs,nmvs,sizeof(Moveorderitem),moveorderitem_compare);
/*Moveorderitem mvs[6]={{12,0,0},{0,0,0},{1,0,0},{2,0,0},{6,0,0},{7,0,0}};
int nmvs=6;
Moveorderitem mvs[3]={{0,0,0},{1,0,0},{5,0,0}};
int nmvs=3;*/
// FILE *f=fopen("log.txt","w");
int bestscore=player==ORDER?-INF:INF,bestat=-1,beststone=-1;
int score;
for(int i=0;i<nmvs;i++){
int p=mvs[i].p;
int stone=mvs[i].stone;
// fprintf(f,"%d %c: ",p,"OX"[stone]);
APPLY(board->b[stone],p);
score=alphabeta(board,!player,player==ORDER?bestscore:-INF,player==CHAOS?bestscore:INF,MMAB_MAXDEPTH);
// score=alphabeta(board,!player,-INF,INF,MMAB_MAXDEPTH);
REMOVE(board->b[stone],p);
// fprintf(f,"%03d (p=%d, s=%c)\n",score,p,"OX"[stone]); fflush(f);
if(player==ORDER?score>bestscore:score<bestscore){
bestat=p;
beststone=stone;
bestscore=score;
}
}
// fclose(f);
Move mv={bestat,beststone};
return mv;
}