-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortpool.c
354 lines (331 loc) · 8.02 KB
/
sortpool.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "five_type.h"
#include "five_global.h"
extern inline int16_t get_value(Point p, uint8_t color, Board* local_board);
typedef struct {
Point* point;
int16_t value;
}Point_info;
typedef struct {
Point_info info[255];
}InfoPOOL;
void ssort_pool(InfoPOOL* r, uint8_t color, uint8_t len,uint8_t cnt, Board* local_board){
uint8_t i, j;
Point_info temp;
for (i = 0; i < len; i++)
{
uint8_t max = i;
for (j = i + 1; j < cnt; j++)
{
if ((*r).info[j].value > (*r).info[max].value)
{
max = j;
}
}
if (max != i)
{
temp = (*r).info[max];
(*r).info[max] = (*r).info[i];
(*r).info[i] = temp;
}
}
}
/*
void qsort_pool(uint8_t L, uint8_t R, InfoPOOL* r, uint8_t color, Board* local_board) {
uint8_t index = (L + R) / 2;
int16_t flag = (*r).info[index].value;
int8_t i, j;
i = L; j = R;
do {
while ((*r).info[i].value > flag && i <= R)i++;
while ((*r).info[j].value < flag && j >= L)j--;
if (i <= j) {
Point_info temp = (*r).info[i]; (*r).info[i] = (*r).info[j]; (*r).info[j] = temp;
i++; j--;
}
} while (i <= j);
if (L < j)qsort_pool(L, j, r,color, local_board);
if (i < R)qsort_pool(i, R, r, color, local_board);
}
*/
/*
typedef struct {
uint8_t form[6];
}disvec;
extern inline uint8_t check_ban(disvec* vec, uint8_t color);
extern disvec getvec(Point p, uint8_t color, Board* local_board);
extern uint8_t poolcnt;
void qsort(uint8_t L, uint8_t R, disvec* r)
{
uint8_t index = (L + R) / 2;
uint8_t flag = (*r).form[index];
int8_t i, j;
i = L; j = R;
do {
while ((*r).form[i] < flag && i <= R)i++;
while ((*r).form[j] > flag && j >= L)j--;
if (i <= j) {
uint8_t temp = (*r).form[i]; (*r).form[i] = (*r).form[j]; (*r).form[j] = temp;
i++; j--;
}
} while (i <= j);
if (L < j)qsort(L, j, r);
if (i < R)qsort(i, R, r);
}
*/
/*
uint8_t greater(disvec vecA, disvec vecB, uint8_t color) {
uint8_t i = 0;
if (check_ban(vecA, color)) {
return 0;
}
if (check_ban(vecB, color)) {
return 1;
}
qsort(0, 4, &vecA);
qsort(0, 4, &vecB);
while (vecA.form[i] == vecB.form[i]) {
i++;
if (i == 5) return 0;
}
return vecA.form[i] < vecB.form[i];
}
*/
/*
void qsortp(uint8_t L, uint8_t R, POOL *r,uint8_t color, Board *board)
{
uint8_t index = (L+R)/2;
Point flag = (*r).record[index];
uint8_t i, j;
i = L; j = R;
do {
while (greater(flag,(*r).record[i],color,board))i++;
while (greater((*r).record[i], flag, color, board))j--;
if (i <= j) {
Point temp = (*r).record[i]; (*r).record[i] = (*r).record[j]; (*r).record[j] = temp;
i++; j--;
}
} while (i <= j);
if (L < j)qsortp(L, j, r, color, board);
if (i < R)qsortp(i, R, r, color, board);
}
*/
/*
POOL sort_pool(POOL eva_pool, uint8_t color, Board* local_board) {
POOL sorted_pool = { {{0,0}} };
disvec vec[255];
disvec* sorted_vec[255];
for (uint8_t i = 0; i < poolcnt; i++) {
vec[i] = getvec(eva_pool.record[i], color, local_board);
uint8_t j = 0;
while (j < i) {
if (greater(*sorted_vec[j], vec[i], color))
j++;
else break;
}
uint8_t k = i;
while (k > j) {
sorted_vec[k] = sorted_vec[k - 1];
sorted_pool.record[k] = sorted_pool.record[k - 1];
k--;
}
sorted_vec[j] = &vec[i];
sorted_pool.record[j] = eva_pool.record[i];
}
return sorted_pool;
}
*/
/*
inline Pouint8_t move(Pouint8_t p, uint8_t i, uint8_t j) {
Pouint8_t tmpp = { p.x + dx[i] * direction[j], p.y + dy[i] * direction[j] };
return tmpp;
}
extern disvec getvec(Pouint8_t P, uint8_t color);
POOL pool;
uint8_t get_pool() {
uint8_t i = 0;
Pouint8_t tmpp;
Board inpool = { 0 };
while (i < Round) {
tmpp = PieceOnBoard.record[i];
}
}
*/
/*
typedef struct {
uint16_t id[4];
}pieceid;
typedef struct {
uint8_t distance[4][2];
}distance;
typedef struct {
uint8_t form[5];
}disvec;
typedef struct {
disvec color[2];
}vecpare;
typedef vecpare vecpool[255];
typedef Pouint8_t POOL[225];
void insertion_sort(uint8_t arr[], uint8_t len) {
uint8_t i, j, temp;
for (i = 1; i < len; i++) {
temp = arr[i];
for (j = i; j > 0 && arr[j - 1] > temp; j--)
arr[j] = arr[j - 1];
arr[j] = temp;
}
}
uint8_t smaller(disvec A, disvec B) {
//insertion_sort(A.form, 5);
//insertion_sort(B.form, 5);
if (A.form[0] == 0) return 1;
if (B.form[0] == 0) return 0;
for (uint8_t i = 1; i < 5; i++) {
if (A.form[i] == 0)
return 1;
if (B.form[i] == 0)
return 0;
if (A.form[i] == B.form[i]) {
continue;
}
else return A.form[i] < B.form[i];
}
return A.form[0] < B.form[0];
}
uint8_t get_surroundings(Pouint8_t p, uint8_t color) {
Pouint8_t tmpp;
uint8_t count = 0;
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 2; j++) {
tmpp = move(p, i, j);
if (board[tmpp.x][tmpp.y] == color)
count++;
}
}
return count;
}
inline uint8_t mindis(disvec vec) {
uint8_t mindis = 10;
for (uint8_t i = 0; i < 5; i++) {
if (vec.form[i] < mindis)mindis = vec.form[i];
}
return mindis;
}
uint8_t equal(disvec A, disvec B) {
return mindis(A) == mindis(B);
}
uint8_t cross_compare(disvec mydis, disvec opdis) {
if (mydis.form[0] == 0) return 1;
else if (opdis.form[0] == 0) return 0;
else if (mydis.form[1] == 0 || mydis.form[2] == 0 || mydis.form[3] == 0) return 1;
else if (opdis.form[1] == 0 || opdis.form[2] == 0 || opdis.form[3] == 0) return 0;
else if (mydis.form[4] == 0) return 1;
else if (opdis.form[4] == 0) return 0;
for (uint8_t i = 0; i < 5; i++) {
if (minform(opdis) < 1) return 0;
}
return 1;
}
void sortvecpool(vecpool disvecpool, POOL pool, uint8_t psp, uint8_t color) {
uint8_t i, j;
vecpare tempvec;
Pouint8_t tmpp;
for (i = 0; i < psp; i++) {
tempvec = disvecpool[i];
tmpp = pool[i];
for (j = i; j > 0 && smaller(tempvec.color[color], disvecpool[j - 1].color[color]); j--) {
disvecpool[j] = disvecpool[j - 1];
pool[j] = pool[j - 1];
}
disvecpool[j] = tempvec;
pool[j] = tmpp;
}
}
void updatevec(POOL pool, uint8_t index, vecpool disvecpool) {
disvecpool[index].color[0] = getvec(id2dis(lookup(pool[index], 0)));
disvecpool[index].color[1] = getvec(id2dis(lookup(pool[index], 1)));
}
void add2pool(Pouint8_t last, POOL pool, uint8_t* psp, vecpool disvecpool) {
for (uint8_t i = 0; i < (*psp) + 1; i++) {
//updatevec(pool, i, disvecpool);
if (pool[i].x == last.x && pool[i].y == last.y) {
pool[i] = pool[--(*psp)];
//updatevec(pool, i, disvecpool);
break;
}
}
for (uint8_t i = 0; i < 4; i++) {
for (uint8_t j = 0; j < 2; j++) {
Pouint8_t tmpp = last;
for (uint8_t k = 0; k < 4; k++) {
tmpp = move(tmpp, i, j);
if (verify_location(tmpp) && board[tmpp.x][tmpp.y] > 3) {
uint8_t l = 0;
while (!(pool[l].x == tmpp.x && pool[l].y == tmpp.y) && l < (*psp)) {
l++;
}
if (l == (*psp)) {
(*psp)++;
}
pool[l] = tmpp;
//updatevec(pool, l, disvecpool);
}
else break;
}
}
}
}
Pouint8_t calcboard(uint8_t color, POOL pool, uint8_t* psp, vecpool disvecpool) {
Pouint8_t minmyP = { 7,7 };
disvec minmydis;
Pouint8_t minopP = { 7,7 };
disvec minopdis;
disvec minoppare;
disvec minmypare;
Pouint8_t resP;
for (uint8_t i = 0; i < 5; i++) {
minmydis.form[i] = 10;
minopdis.form[i] = 10;
minoppare.form[i] = 10;
minmypare.form[i] = 10;
}
disvec mydis;
disvec opdis;
for (uint8_t i = 0; i < (*psp) + 1; i++) {
updatevec(pool, i, disvecpool);
}
sortvecpool(disvecpool, pool, *psp, !color);
sortvecpool(disvecpool + 10, pool + 10, (*psp) - 10, color);
for (uint8_t i = 0; i < (*psp); i++) {
Pouint8_t P = pool[i];
mydis = disvecpool[i].color[color];
opdis = disvecpool[i].color[!color];
if (equal(mydis, minmydis)) {
if (get_surroundings(P, color) > get_surroundings(minmyP, color)) {
minmyP = P;
minmydis = mydis;
minmypare = opdis;
}
}
if (smaller(mydis, minmydis)) {
minmyP = P;
minmydis = mydis;
minmypare = opdis;
}
if (equal(opdis, minopdis)) {
if (get_surroundings(P, !color) > get_surroundings(minopP, !color)) {
minopP = P;
minopdis = opdis;
minoppare = mydis;
}
}
if (smaller(opdis, minopdis)) {
minopP = P;
minopdis = opdis;
minoppare = mydis;
}
}
resP = cross_compare(minmydis, minopdis) ? minmyP : minopP;
add2pool(resP, pool, psp, disvecpool);
return resP;
}
*/