-
Notifications
You must be signed in to change notification settings - Fork 0
/
c4.cpp
223 lines (189 loc) · 3.8 KB
/
c4.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
#include "c4.h"
c4::c4() {
curr = 0;
mask = 0;
moves = 0;
std::memset(track, 0, sizeof(track));
zug = false;
}
c4::~c4() {
}
U64 c4::top_mask(U8 col) {
return (U64(1) << (HEIGHT - 1) << col*(HEIGHT + 1));
}
U64 c4::bottom_mask(U8 col) {
return (U64(1) << col*(HEIGHT + 1));
}
bool c4::playable(U8 col) {
return !(mask & top_mask(col));
}
bool c4::alignment(U64 c) {
// Checks if state c is a win
U64 w = 0;
// Vertical
w = c & (c >> 1);
if (w & (w >> 2)) return true;
// Horizontal
w = c & (c >> (HEIGHT + 1));
if (w & (w >> (2 * (HEIGHT + 1)))) return true;
// Diagonals
w = c & (c >> HEIGHT);
if (w & (w >> (2 * HEIGHT))) return true;
w = c & (c >> (HEIGHT + 2));
if (w & (w >> (2 * (HEIGHT + 2)))) return true;
return false;
}
bool c4::draw(U64 c) {
if (moves >= HEIGHT*WIDTH && !alignment(c))
return true;
return false;
}
void c4::getZug() {
if (!zug) {
zug = true;
//std::cout << "AI gain control of Zugswang" << std::endl;
}
}
void c4::giveZug() {
if (zug) {
zug = false;
//std::cout << "AI lost control of Zugswang" << std::endl;
}
}
void c4::setZug(bool z) {
zug = z;
}
U8 c4::getMoves() {
return moves;
}
void c4::play(U8 col) {
curr ^= mask;
mask |= mask + bottom_mask(col);
moves++;
track[col]++;
}
int c4::result(bool t, bool p) {
U64 c = curr^mask;
if (alignment(c)) {
if (moves%2 == t) {
if (p) std::cout << "AI wins." << std::endl;
return 3000;
}
else {
if (p) std::cout << "Player wins." << std::endl;
return -5000;
}
} else if (draw(c)) {
if (p) std::cout << "Draw." << std::endl;
return 1000;
}
return 0;
}
int c4::heuristic_fp() {
U64 c = curr^mask;
int score = 0;
//std::cout << "First player heurtistic" << std::endl;
if (threat(mask, c, moves)) {
getZug();
score += 50;
}
else {
giveZug();
}
if (threat(mask, c^mask, moves-1)) {
score -= 50;
}
for (int i = 0; i < WIDTH; i++) {
score += vertical(mask, c, i);
score -= vertical(mask, c^mask, i);
// score += claimEven(i, mask, zug);
// score += lowInverse(mask, i, zug);
}
if (alignment(c)) return 1000;
if (alignment(c^mask)) return -1000;
return score;
}
int c4::heuristic_sp() {
U64 c = curr;
int score = 0;
//std::cout << "Second player heurtistic" << std::endl;
if (threat(mask, c, moves-1)) {
giveZug();
score += 50;
}
else {
getZug();
score -= 50;
}
if (threat(mask, c^mask, moves)) {
score -= 50;
}
for (int i = 0; i < WIDTH; i++) {
score += vertical(mask, c^mask, i);
score -= vertical(mask, c, i);
score -= claimEven(i, mask, zug);
score -= lowInverse(mask, i, zug);
}
if (alignment(c^mask)) return -1000;
if (alignment(c)) return 1000;
return score;
}
int c4::heuristic() {
if (moves%2) return heuristic_fp();
return heuristic_sp();
}
std::vector<U8> c4::possible() {
U8 all[7] = {3,4,2,5,1,6,0};
std::vector<U8> moves;
for (U8 i = 0; i < 7; i++) {
if (playable(all[i])) {
moves.push_back(all[i]);
}
}
return moves;
}
void c4::display() {
/*
Prints bitboard to console
. . . . . . .
5 12 19 26 33 40 47
4 11 18 25 32 39 46
3 10 17 24 31 38 45
2 9 16 23 30 37 44
1 8 15 22 29 36 43
0 7 14 21 28 35 42
*/
if (moves)
std::cout << "Turn: " << moves << std::endl;
else
std::cout << "Game Start" << std::endl;
std::string bin_c = "", bin_o = "";
U64 c, o;
if (moves%2)
c = curr^mask, o = curr;
else
c = curr, o = curr^mask;
int length = 64;
while (length--) {
bin_c += (c%2 == 0 ? '0' : '1');
bin_o += (o%2 == 0 ? '0' : '1');
c/=2;
o/=2;
}
for (int i = 6; i >= 0; i--) {
for (int j = 0; j < WIDTH; j++) {
if (i == 6)
std::cout << j << " ";
else {
if (bin_c[i+j*WIDTH] == '1')
std::cout << "O ";
else if(bin_o[i+j*WIDTH] == '1')
std::cout << "X ";
else
std::cout << "- ";
}
}
std::cout << std::endl;
}
std::cout << std::endl;
}