-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_algorithm.cpp
More file actions
282 lines (264 loc) · 9.98 KB
/
search_algorithm.cpp
File metadata and controls
282 lines (264 loc) · 9.98 KB
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
#include "search_algorithm.h"
bool Search::compare_score(Process x, Process y)
{
if(x.score>=10 && y.score>=10) return (x.enemy_score > y.enemy_score);
if(x.score>=5 && y.score>=5 && x.score==y.score) return (x.enemy_score < y.enemy_score);
if(x.enemy_score<10 && y.enemy_score<10 && x.enemy_score==y.enemy_score) return (x.score > y.score);
//zone1~4
if(x.score>=10 || y.score>=10) return (x.score > y.score);
if(x.score>=5 || y.score>=5) return (x.score > y.score);
if(x.enemy_score<10 || y.enemy_score<10) return (x.enemy_score < y.enemy_score);
return 0;
}
void Search::find_best_move(uint_fast8_t chessboard[8][8])
{
int direction_x[2]={ 1, 0};
int direction_y[2]={ 0, 1};
uint_fast8_t nx, ny;
uint_fast8_t i, j, k, i2, j2, k2;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
for(k=0;k<2;k++)
{
nx = j + direction_x[k];
ny = i + direction_y[k];
//printf("nx = %u, ny = %u\n",nx,ny);
if(nx>=8 || ny>=8) continue;
swap_runestone(&chessboard[j][i], &chessboard[nx][ny]);
score = 0;
runestone_match(chessboard);
swap_runestone(&chessboard[j][i], &chessboard[nx][ny]);
if(score==0) continue;
//print_board(copy_board);
current_path.x = j;
current_path.y = i;
current_path.direction = k;
current_path.score = score;
memcpy(next_board, copy_board, sizeof(next_board));
int worst_score=0;
for(i2=0;i2<8;i2++) for(j2=0;j2<8;j2++) for(k2=0;k2<2;k2++)
{
nx = j2 + direction_x[k2];
ny = i2 + direction_y[k2];
if(nx>=8 || ny>=8) continue;
swap_runestone(&next_board[j2][i2], &next_board[nx][ny]);
score = 0;
runestone_match(next_board);
swap_runestone(&next_board[j2][i2], &next_board[nx][ny]);
if(score>worst_score)
{
worst_score = score;
}
}
current_path.enemy_score = worst_score;
all_path.push_back(current_path);
}
}
}
sort(all_path.begin(), all_path.end(), compare_score);
}
void Search::runestone_match(uint_fast8_t chessboard[8][8])
{
uint_fast8_t x,y,i,j;
uint_fast8_t runestone;
uint_fast8_t combo_without_drop=0;
//memcpy(copy_board, chessboard, sizeof(copy_board));
memcpy(temp_board, chessboard, sizeof(temp_board));
memcpy(bomb_board, chessboard, sizeof(bomb_board));
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
{
if(temp_board[j][i]=='7') temp_board[j][i]='0';
}
}
memcpy(copy_board, temp_board, sizeof(copy_board));
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
{
if(copy_board[j][i]=='.') continue; //if this coordinate already matched : skip
uint_fast8_t same_color_runestone = 0;
runestone = temp_board[j][i]; //take this color
Queue_init(j,i); //put first member in to the que
while(que_front!=NULL)
{
x = que_front->x; //take first member x
y = que_front->y; //take first member y
if(copy_board[x][y]=='.') //skip the repeat
{
Queue_pop();
continue;
}
bool passport=0;
//judge this runestone : 1 case met then pass other
if(x-1>=0 && temp_board[x-1][y]==runestone && x-2>=0 && temp_board[x-2][y]==runestone) { copy_board[x][y]='.'; passport=1; }
else if(x+1<=7 && temp_board[x+1][y]==runestone && x+2<=7 && temp_board[x+2][y]==runestone) { copy_board[x][y]='.'; passport=1; }
else if(y-1>=0 && temp_board[x][y-1]==runestone && y-2>=0 && temp_board[x][y-2]==runestone) { copy_board[x][y]='.'; passport=1; }
else if(y+1<=7 && temp_board[x][y+1]==runestone && y+2<=7 && temp_board[x][y+2]==runestone) { copy_board[x][y]='.'; passport=1; }
else if(x-1>=0 && x+1<=7 && temp_board[x-1][y]==runestone && temp_board[x+1][y]==runestone) { copy_board[x][y]='.'; passport=1; }
else if(y-1>=0 && y+1<=7 && temp_board[x][y-1]==runestone && temp_board[x][y+1]==runestone) { copy_board[x][y]='.'; passport=1; }
//push possible match to que
if(passport)
{
same_color_runestone++;
if(x-1>=0 && copy_board[x-1][y]==runestone) { Queue_push(x-1,y); }
if(x+1<=7 && copy_board[x+1][y]==runestone) { Queue_push(x+1,y); }
if(y-1>=0 && copy_board[x][y-1]==runestone) { Queue_push(x,y-1); }
if(y+1<=7 && copy_board[x][y+1]==runestone) { Queue_push(x,y+1); }
}
//remove first member
Queue_pop();
//print_board(copy_board);
}
if(same_color_runestone>=3)
{
combo++;
combo_without_drop++;
if(same_color_runestone>=4)
{
if(runestone=='0') score+=100;
else score+=10;
}
else
{
if(runestone=='0') score+=5;
else score+=1;
}
/*switch(runestone)
{
case '0' : head += 1; break;
case '1' : blue += 1; break;
case '2' : Green += 1; break;
case '3' : red += 1; break;
case '4' : yellow += 1; break;
case '5' : purple += 1; break;
case '6' : brown += 1; break;
case '7' : bomb += 1; break;
}*/
}
}
}
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
{
if(copy_board[j][i]=='.' && bomb_board[j][i]=='7')
{
bomb_board[j][i]='.';
printf("first area = %d %d\n",j,i);
bomb_area(j, i);
}
}
}
for(i=0;i<=7;i++) for(j=0;j<=7;j++) if(copy_board[j][i]!='.' && bomb_board[j][i]=='7') copy_board[j][i]=='7';
if(combo_without_drop)
{
//print_board(copy_board);
drop_board(); //drop function call
runestone_match(copy_board);
}
}
void Search::Queue_init(uint_fast8_t x, uint_fast8_t y)
{
free(que_head);//very impotant!!! free last head memory
Queue *first_member;
que_head = (Queue*)malloc(sizeof(Queue));
first_member = (Queue*)malloc(sizeof(Queue));
first_member->x = x;
first_member->y = y;
first_member->next = NULL;
que_head->next = first_member;
que_front = first_member;
que_back = first_member;
}
void Search::Queue_push(uint_fast8_t x, uint_fast8_t y)
{
Queue *new_que_member; //declare a new que member
new_que_member = (Queue*)malloc(sizeof(Queue));
new_que_member->x = x;
new_que_member->y = y;
new_que_member->next = NULL;
que_back->next = new_que_member;
que_back = new_que_member;
}
void Search::Queue_pop()
{
Queue *del_que_member;
del_que_member = que_front;
que_front = que_front->next;
free(del_que_member);
}
void Search::print_board(uint_fast8_t chessboard[8][8])
{
printf("========\n");
uint_fast8_t i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%c ",chessboard[j][i]);
}
putchar(10);
}
}
void Search::drop_board()
{
//copy previous match board
//memcpy(copy_board_2, copy_board, sizeof(copy_board));
uint_fast8_t x, y;
for(x=0;x<=7;x++)
{
uint_fast8_t buffer[8], runes_in_the_air=0;
for(y=0;y<=7;y++)
{
if(copy_board[x][y]!='.')
{
buffer[runes_in_the_air]=copy_board[x][y];
copy_board[x][y] = '.';
runes_in_the_air++;
}
}
switch(runes_in_the_air)
{
case 1 : memcpy(copy_board[x]+7,buffer,sizeof(uint_fast8_t[1])); break;
case 2 : memcpy(copy_board[x]+6,buffer,sizeof(uint_fast8_t[2])); break;
case 3 : memcpy(copy_board[x]+5,buffer,sizeof(uint_fast8_t[3])); break;
case 4 : memcpy(copy_board[x]+4,buffer,sizeof(uint_fast8_t[4])); break;
case 5 : memcpy(copy_board[x]+3,buffer,sizeof(uint_fast8_t[5])); break;
case 6 : memcpy(copy_board[x]+2,buffer,sizeof(uint_fast8_t[6])); break;
case 7 : memcpy(copy_board[x]+1,buffer,sizeof(uint_fast8_t[7])); break;
case 8 : memcpy(copy_board[x],buffer,sizeof(uint_fast8_t[8])); break;
}
}
//print_board(copy_board);
memcpy(temp_board, copy_board, sizeof(copy_board));
}
void Search::bomb_area(uint_fast8_t x, uint_fast8_t y)
{
int direction_x[8]={-1, 0, 1,-1, 1,-1, 0, 1};
int direction_y[8]={-1,-1,-1, 0, 0, 1, 1, 1};
int i,j,k;
uint_fast8_t nx, ny;
for(i=0;i<8;i++)
{
nx = x + direction_x[i];
ny = y + direction_y[i];
if(nx>=8 || ny>=8) continue;
copy_board[nx][ny]='.';
if(copy_board[nx][ny]=='.' && bomb_board[nx][ny]=='7')
{
printf("area = %u %u\n",nx,ny);
bomb_board[nx][ny]='.';
bomb_area(nx, ny);
}
}
}
void Search::swap_runestone(uint_fast8_t *pos1, uint_fast8_t *pos2)
{
uint_fast8_t t = *pos1;
*pos1 = *pos2;
*pos2 = t;
}