-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrand.c
79 lines (64 loc) · 1.58 KB
/
rand.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
// Order and Chaos AI: Random player. - Tom Smeding
#include "params.h"
#define _XOPEN_SOURCE 700 // random
#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))))
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;
}
Move calcmove(Board *board,int player){
(void)player;
Move poss[2*N*N];
int nposs=0;
for(int p=0;p<N*N;p++){
for(int stone=0;stone<2;stone++){
if(!ISEMPTY(board->b[0]|board->b[1],p))continue;
poss[nposs].pos=p;
poss[nposs++].stone=stone;
}
}
if(nposs==0){
Move mv={-1,-1};
return mv;
}
return poss[random()%nposs];
}