-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGOLAD.cpp
287 lines (272 loc) · 10.3 KB
/
GOLAD.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
#include <stdio.h>
#include <utility>
#include <algorithm>
#define NumGenerations 3
struct MoveStruct{
std::pair<int,int> Changed;
//FirstSource and SecondSource don't matter if the location in Changed isn't empty
std::pair<int,int> FirstSource;
std::pair<int,int> SecondSource;
};
int Width;
int Height;
int BirthBM;
int SurviveBM;
int Board[20][20]; //Maxsize
bool MyTurn = true;
bool FirstPlayer = true;
MoveStruct Move;
int CalculateNextGens(int*** NextGenerations){
for (int CurrentGen = 0;CurrentGen < (NumGenerations);CurrentGen++){
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
int FirstCount = 0;
int SecondCount = 0;
int NoneCount = 0;
if (a>0 and b>0 and NextGenerations[CurrentGen][a-1][b-1] == 1){FirstCount++;}
if (a>0 and NextGenerations[CurrentGen][a-1][b] == 1){FirstCount++;}
if (a>0 and b<Width-1 and NextGenerations[CurrentGen][a-1][b+1] == 1){FirstCount++;}
if (b>0 and NextGenerations[CurrentGen][a][b-1] == 1){FirstCount++;}
if (b<Width-1 and NextGenerations[CurrentGen][a][b+1] == 1){FirstCount++;}
if (a<Height-1 and b>0 and NextGenerations[CurrentGen][a+1][b-1] == 1){FirstCount++;}
if (a<Height-1 and NextGenerations[CurrentGen][a+1][b] == 1){FirstCount++;}
if (a<Height-1 and b<Width-1 and NextGenerations[CurrentGen][a+1][b+1] == 1){FirstCount++;}
if (a>0 and b>0 and NextGenerations[CurrentGen][a-1][b-1] == 2){SecondCount++;}
if (a>0 and NextGenerations[CurrentGen][a-1][b] == 2){SecondCount++;}
if (a>0 and b<Width-1 and NextGenerations[CurrentGen][a-1][b+1] == 2){SecondCount++;}
if (b>0 and NextGenerations[CurrentGen][a][b-1] == 2){SecondCount++;}
if (b<Width-1 and NextGenerations[CurrentGen][a][b+1] == 2){SecondCount++;}
if (a<Height-1 and b>0 and NextGenerations[CurrentGen][a+1][b-1] == 2){SecondCount++;}
if (a<Height-1 and NextGenerations[CurrentGen][a+1][b] == 2){SecondCount++;}
if (a<Height-1 and b<Width-1 and NextGenerations[CurrentGen][a+1][b+1] == 2){SecondCount++;}
if (a>0 and b>0 and NextGenerations[CurrentGen][a-1][b-1] == 3){NoneCount++;}
if (a>0 and NextGenerations[CurrentGen][a-1][b] == 3){NoneCount++;}
if (a>0 and b<Width-1 and NextGenerations[CurrentGen][a-1][b+1] == 3){NoneCount++;}
if (b>0 and NextGenerations[CurrentGen][a][b-1] == 3){NoneCount++;}
if (b<Width-1 and NextGenerations[CurrentGen][a][b+1] == 3){NoneCount++;}
if (a<Height-1 and b>0 and NextGenerations[CurrentGen][a+1][b-1] == 3){NoneCount++;}
if (a<Height-1 and NextGenerations[CurrentGen][a+1][b] == 3){NoneCount++;}
if (a<Height-1 and b<Width-1 and NextGenerations[CurrentGen][a+1][b+1] == 3){NoneCount++;}
if (NextGenerations[CurrentGen][a][b] == 0 and ((BirthBM & (1<<(FirstCount+SecondCount+NoneCount))) != 0)){
if (FirstCount > SecondCount){NextGenerations[CurrentGen+1][a][b] = 1;}
if (FirstCount == SecondCount){NextGenerations[CurrentGen+1][a][b] = 3;}
if (FirstCount < SecondCount){NextGenerations[CurrentGen+1][a][b] = 2;}
}
else if (NextGenerations[CurrentGen][a][b] != 0 and ((SurviveBM & (1<<(FirstCount+SecondCount+NoneCount))) == 0)){
NextGenerations[CurrentGen+1][a][b] = 0;
}
else {
NextGenerations[CurrentGen+1][a][b] = NextGenerations[CurrentGen][a][b];
}
}
}
}
return 0;
}
int GenerateMove(){
//Making NextGenerations in such an manner that it's usable as argument
int** NextGenerations[NumGenerations+1];
for (int a = 0;a<NumGenerations+1;a++){
NextGenerations[a] = new int*[Height];
for (int b = 0;b<Height;b++){
NextGenerations[a][b] = new int[Width];
}
}
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
NextGenerations[0][a][b] = Board[a][b];
}
}
CalculateNextGens(NextGenerations);
int Improvement[Height][Width];
for (int a = 0;a<Width;a++){
for (int b = 0;b<Height;b++){
int CurrentScore = 0;
for (int c = std::max(a-NumGenerations,0);c<std::min(a+NumGenerations+1,Height);c++){
for (int d = std::max(b-NumGenerations,0);d<std::min(b+NumGenerations+1,Width);d++){
if (NextGenerations[NumGenerations][c][d] == 1){CurrentScore++;}
if (NextGenerations[NumGenerations][c][d] == 2){CurrentScore--;}
}
}
if (!FirstPlayer){CurrentScore = -CurrentScore;}
int NewScore = 0;
if (Board[a][b] == 0){
if (FirstPlayer){NextGenerations[0][a][b] = 1;}
else{NextGenerations[0][a][b] = 2;}
}
else{
NextGenerations[0][a][b] = 0;
}
CalculateNextGens(NextGenerations);
for (int c = std::max(a-NumGenerations,0);c<std::min(a+NumGenerations+1,Height);c++){
for (int d = std::max(b-NumGenerations,0);d<std::min(b+NumGenerations+1,Width);d++){
if (NextGenerations[NumGenerations][c][d] == 1){NewScore++;}
if (NextGenerations[NumGenerations][c][d] == 2){NewScore--;}
}
}
if (!FirstPlayer){NewScore = -NewScore;}
Improvement[a][b] = NewScore - CurrentScore;
NextGenerations[0][a][b] = Board[a][b];
CalculateNextGens(NextGenerations);
fprintf(stderr, "%d ", Improvement[a][b]);
}
fprintf(stderr, "\n");
}
int Largest = -500;
int LargestA = 0;
int LargestB = 0;
for (int a = 0;a<Width;a++){
for (int b = 0;b<Height;b++){
if (Improvement[a][b] > Largest and Board[a][b]>0){
LargestA = a;
LargestB = b;
Largest = Improvement[a][b];
}
}
}
Move.Changed.first = LargestA;
Move.Changed.second = LargestB;
return 0;
}
int DoMove(){
if (Board[Move.Changed.first][Move.Changed.second] == 0){
if (not MyTurn xor FirstPlayer){
Board[Move.Changed.first][Move.Changed.second] = 1;
}
else{
Board[Move.Changed.first][Move.Changed.second] = 2;
}
Board[Move.FirstSource.first][Move.FirstSource.second] = 0;
Board[Move.SecondSource.first][Move.SecondSource.second] = 0;
}
else{
Board[Move.Changed.first][Move.Changed.second] = 0;
}
short int NewBoard[20][20];
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
int FirstCount = 0;
int SecondCount = 0;
int NoneCount = 0;
if (a>0 and b>0 and Board[a-1][b-1] == 1){FirstCount++;}
if (a>0 and Board[a-1][b] == 1){FirstCount++;}
if (a>0 and b<Width-1 and Board[a-1][b+1] == 1){FirstCount++;}
if (b>0 and Board[a][b-1] == 1){FirstCount++;}
if (b<Width-1 and Board[a][b+1] == 1){FirstCount++;}
if (a<Height-1 and b>0 and Board[a+1][b-1] == 1){FirstCount++;}
if (a<Height-1 and Board[a+1][b] == 1){FirstCount++;}
if (a<Height-1 and b<Width-1 and Board[a+1][b+1] == 1){FirstCount++;}
if (a>0 and b>0 and Board[a-1][b-1] == 2){SecondCount++;}
if (a>0 and Board[a-1][b] == 2){SecondCount++;}
if (a>0 and b<Width-1 and Board[a-1][b+1] == 2){SecondCount++;}
if (b>0 and Board[a][b-1] == 2){SecondCount++;}
if (b<Width-1 and Board[a][b+1] == 2){SecondCount++;}
if (a<Height-1 and b>0 and Board[a+1][b-1] == 2){SecondCount++;}
if (a<Height-1 and Board[a+1][b] == 2){SecondCount++;}
if (a<Height-1 and b<Width-1 and Board[a+1][b+1] == 2){SecondCount++;}
if (a>0 and b>0 and Board[a-1][b-1] == 3){NoneCount++;}
if (a>0 and Board[a-1][b] == 3){NoneCount++;}
if (a>0 and b<Width-1 and Board[a-1][b+1] == 3){NoneCount++;}
if (b>0 and Board[a][b-1] == 3){NoneCount++;}
if (b<Width-1 and Board[a][b+1] == 3){NoneCount++;}
if (a<Height-1 and b>0 and Board[a+1][b-1] == 3){NoneCount++;}
if (a<Height-1 and Board[a+1][b] == 3){NoneCount++;}
if (a<Height-1 and b<Width-1 and Board[a+1][b+1] == 3){NoneCount++;}
fprintf(stderr, "%d ", FirstCount+SecondCount+NoneCount);
if (Board[a][b] == 0 and ((BirthBM & (1<<(FirstCount+SecondCount+NoneCount))) != 0)){
if (FirstCount > SecondCount){NewBoard[a][b] = 1;}
if (FirstCount == SecondCount){NewBoard[a][b] = 3;}
if (FirstCount < SecondCount){NewBoard[a][b] = 2;}
}
else if (Board[a][b] != 0 and ((SurviveBM & (1<<(FirstCount+SecondCount+NoneCount))) == 0)){
NewBoard[a][b] = 0;
}
else {
NewBoard[a][b] = Board[a][b];
}
}
fprintf(stderr, "\n");
}
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
Board[a][b] = NewBoard[a][b];
}
}
return 0;
}
bool GameFinished(){
int Player1Pieces = 0;
int Player2Pieces = 0;
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
switch(Board[a][b]){
case 1: Player1Pieces++;break;
case 2: Player2Pieces++;break;
}
}
}
if (Player1Pieces == 0 or Player2Pieces == 0){
return true;
}
else{
return false;
}
}
int main(){
/*
Import Rulesets
BirthBM and SurviveBM are both Bitmasks
for both: 0 live around has value 1, 8 live around has value 512
BirthBM describes how many alive cells has to be around an empty cell for it to come alive
SurviveBM describes how many alive cells has to be around an alive cell for it to stay alive
*/
scanf("%d %d",&BirthBM,&SurviveBM);
//Import size of board
scanf("%d %d",&Width,&Height);
//Import board itself
for (int a = 0;a<Height;a++){
for (int b = 0;b<Width;b++){
scanf("%1d",&Board[a][b]);
}
}
//Import if starting player
char PlayerNumber[10];
scanf("%s",PlayerNumber);
switch(PlayerNumber[0]){
case '1': MyTurn = true;FirstPlayer = true;break;
case '2': MyTurn = false;FirstPlayer = false;break;
}
while (GameFinished() == false){
if (MyTurn){
GenerateMove();
printf("%d %d\n",Move.Changed.first,Move.Changed.second);
if (Board[Move.Changed.first][Move.Changed.second] == 0){
printf("%d %d\n",Move.FirstSource.first,Move.FirstSource.second);
printf("%d %d\n",Move.SecondSource.first,Move.SecondSource.second);
}
DoMove();
}
else{
scanf("%d %d",&Move.Changed.first,&Move.Changed.second);
if (Board[Move.Changed.first][Move.Changed.second] == 0){
scanf("%d %d",&Move.FirstSource.first,&Move.FirstSource.second);
scanf("%d %d",&Move.SecondSource.first,&Move.SecondSource.second);
}
DoMove();
}
MyTurn = not MyTurn;
// for (int a = 0;a<Height;a++){
// for (int b = 0;b<Width;b++){
// switch(Board[a][b]){
// case 0: fprintf(stderr, "\x1b[40m \x1b[0m");break;
// case 1: fprintf(stderr, "\x1b[41m \x1b[0m");break;
// case 2: fprintf(stderr, "\x1b[44m \x1b[0m");break;
// case 3: fprintf(stderr, "\x1b[47m \x1b[0m");break;
// }
// }
// fprintf(stderr, "\n");
// }
// fprintf(stderr, "----------\n");
}
return 0;
}