-
Notifications
You must be signed in to change notification settings - Fork 1
/
Nomoku.cpp
123 lines (105 loc) · 1.95 KB
/
Nomoku.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
#include "pisqpipe.h"
#include <windows.h>
const char *infotext = "name=\"Nomuku\", author=\"Nathaniel Lin & Howard Liu\", version=\"0.1 pre alpha\", country=\"China\", www=\"http://blog.nathaniel-lin.me";
#define MAX_BOARD 100
int board[MAX_BOARD][MAX_BOARD];
static unsigned seed;
void brain_init()
{
if (width<5 || height<5){
pipeOut("ERROR size of the board");
return;
}
if (width>MAX_BOARD || height>MAX_BOARD){
pipeOut("ERROR Maximal board size is %d", MAX_BOARD);
return;
}
seed = start_time;
pipeOut("OK");
}
void brain_restart()
{
int x, y;
for (x = 0; x<width; x++){
for (y = 0; y<height; y++){
board[x][y] = 0;
}
}
pipeOut("OK");
}
int isFree(int x, int y)
{
return x >= 0 && y >= 0 && x<width && y<height && board[x][y] == 0;
}
void brain_my(int x, int y)
{
if (isFree(x, y)){
board[x][y] = 1;
}
else{
pipeOut("ERROR my move [%d,%d]", x, y);
}
}
void brain_opponents(int x, int y)
{
if (isFree(x, y)){
board[x][y] = 2;
}
else{
pipeOut("ERROR opponents's move [%d,%d]", x, y);
}
}
void brain_block(int x, int y)
{
if (isFree(x, y)){
board[x][y] = 3;
}
else{
pipeOut("ERROR winning move [%d,%d]", x, y);
}
}
int brain_takeback(int x, int y)
{
if (x >= 0 && y >= 0 && x<width && y<height && board[x][y] != 0){
board[x][y] = 0;
return 0;
}
return 2;
}
unsigned rnd(unsigned n)
{
seed = seed * 367413989 + 174680251;
return (unsigned)(UInt32x32To64(n, seed) >> 32);
}
void brain_turn()
{
int x, y, i;
i = -1;
do{
x = rnd(width);
y = rnd(height);
i++;
if (terminateAI) return;
} while (!isFree(x, y));
if (i>1) pipeOut("DEBUG %d coordinates didn't hit an empty field", i);
do_mymove(x, y);
}
void brain_end()
{
}
#ifdef DEBUG_EVAL
#include <windows.h>
void brain_eval(int x, int y)
{
HDC dc;
HWND wnd;
RECT rc;
char c;
wnd = GetForegroundWindow();
dc = GetDC(wnd);
GetClientRect(wnd, &rc);
c = (char)(board[x][y] + '0');
TextOut(dc, rc.right - 15, 3, &c, 1);
ReleaseDC(wnd, dc);
}
#endif