-
Notifications
You must be signed in to change notification settings - Fork 0
/
procon26_modlib.cpp
851 lines (770 loc) · 26.8 KB
/
procon26_modlib.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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
#include <vector>
#include "procon26_modlib.hpp"
#include "procon26_modio.hpp"
const Stone *EMPTY_STONE = getStoneByString("0000000000000000000000000000000000000000000000000000000000000000");
const Board *EMPTY_BOARD = getBoardByString(
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
);
/* Implementation */
int countBit(unsigned char bit)
{
bit = ((bit & 0xAA) >> 1) + (bit & 0x55);
bit = ((bit & 0xCC) >> 2) + (bit & 0x33);
bit = ((bit & 0xF0) >> 4) + (bit & 0x0F);
return bit;
}
int countBitOfStone(const Stone *stone)
{
int sum = 0;
for(int i = 0; i < STONE_SIZE; i ++){
sum += countBit(stone->zuku[i]);
}
return sum;
}
int countBitOfBoard(const Board *board)
{
int sum = 0;
for (int y = 0; y < BOARD_SIZE ; y++)
{
for (int i = 0; i < 4; i++)
{
sum += countBit(board->block[i + y * 4]);
}
}
return sum;
}
Stone *quarryStone(const Board *board, int x, int y, bool filler)
{
int qX = x, qY = y;
Stone *tmp;
if(x < 0) qX = 0;
else if(x > BOARD_SIZE - STONE_SIZE) qX = BOARD_SIZE - STONE_SIZE;
if(y < 0) qY = 0;
else if(y > BOARD_SIZE - STONE_SIZE) qY = BOARD_SIZE - STONE_SIZE;
Stone *quarried = new Stone;
for(int i = 0; i < STONE_SIZE; i++)
{
quarried->zuku[i] = board->block[qX / STONE_SIZE + qY * (STONE_SIZE / 2) + (i * (STONE_SIZE / 2))] << (qX % STONE_SIZE) | board->block[qX / STONE_SIZE + qY * (STONE_SIZE / 2) + (i * (STONE_SIZE / 2)) + 1] >> (STONE_SIZE - (qX % STONE_SIZE));
}
if(x < 0){ tmp = shiftRight(quarried, -x, filler); delete quarried; quarried = tmp;}
else if(x > BOARD_SIZE - STONE_SIZE){ tmp = shiftLeft(quarried, x - BOARD_SIZE + STONE_SIZE, filler); delete quarried; quarried = tmp;}
if(y < 0){ tmp = shiftDown(quarried, -y, filler); delete quarried; quarried = tmp;}
else if(y > BOARD_SIZE - STONE_SIZE){ tmp = shiftUp(quarried, y - BOARD_SIZE + STONE_SIZE, filler); delete quarried; quarried = tmp;}
return quarried;
}
Stone *shiftUp(const Stone *stone, int times, int filler)
{
Stone *dist = new Stone;
for (int i = 0; i < STONE_SIZE - times; i++)
{
dist->zuku[i] = stone->zuku[i + times];
}
for (int i = STONE_SIZE - times; i < STONE_SIZE; i++)
{
dist->zuku[i] = (filler == 0) ? 0 : (unsigned char)0xFF;
}
return dist;
}
Stone *shiftDown(const Stone *stone, int times, int filler)
{
Stone *dist = new Stone;
for (int i = times; i < STONE_SIZE; i++)
{
dist->zuku[i] = stone->zuku[i - times];
}
for (int i = 0; i < times; i++)
{
dist->zuku[i] = (filler == 0) ? 0 : (unsigned char)0xFF;
}
return dist;
}
Stone *shiftRight(const Stone *stone, int times, int filler)
{
Stone *dist = new Stone;
unsigned char filler_char = (filler == 0) ? 0 : (unsigned char)0xFF << (STONE_SIZE - times);
for (int i = 0; i < STONE_SIZE; i++)
{
dist->zuku[i] = (stone->zuku[i] >> times) | filler_char;
}
return dist;
}
Stone *shiftLeft(const Stone *stone, int times, int filler)
{
Stone *dist = new Stone;
unsigned char filler_char = (filler == 0) ? 0 : (unsigned char)0xFF >> (STONE_SIZE - times);
for (int i = 0; i < STONE_SIZE; i++)
{
dist->zuku[i] = (stone->zuku[i] << times) | filler_char;
}
return dist;
}
Stone *rotate(const Stone *stone, int n)
{
switch(n % 4)
{
case 1:
// rotate90
return rotate90(stone);
case 2:
// rotate180
return rotate180(stone);
case 3:
// rotate270
return rotate270(stone);
default:
return cloneStone(stone);
}
}
Stone *rotate90(const Stone *stone)
{
Stone *dist = new Stone;
for (int i = 0; i < STONE_SIZE; i++) dist->zuku[i] = 0;
for (int i = 0; i < 64; i++)
{
int x = i % STONE_SIZE;
int y = i / STONE_SIZE;
int nx = STONE_SIZE - 1 - y;
int ny = x;
if ((stone->zuku[y] << x) & 128)
{
dist->zuku[ny] += 128 >> nx;
}
}
return dist;
}
Stone *rotate180(const Stone *stone)
{
Stone *dist = new Stone;
for (int i = 0; i < STONE_SIZE; i++) dist->zuku[i] = 0;
for (int i = 0; i < 64; i++)
{
int x = i % STONE_SIZE;
int y = i / STONE_SIZE;
int nx = STONE_SIZE - 1 - x;
int ny = STONE_SIZE - 1 - y;
if ((stone->zuku[y] << x) & 128)
{
dist->zuku[ny] += 128 >> nx;
}
}
return dist;
}
Stone *rotate270(const Stone *stone)
{
Stone *dist = new Stone;
for (int i = 0; i < STONE_SIZE; i++) dist->zuku[i] = 0;
for (int i = 0; i < 64; i++)
{
int x = i % STONE_SIZE;
int y = i / STONE_SIZE;
int nx = y;
int ny = STONE_SIZE - 1 - x;
if ((stone->zuku[y] << x) & 128)
{
dist->zuku[ny] += 128 >> nx;
}
}
return dist;
}
Stone *flip(const Stone *stone)
{
Stone *dist = new Stone;
for (int i = 0; i < STONE_SIZE; i++) dist->zuku[i] = 0;
for (int i = 0; i < 64; i++)
{
int x = i % STONE_SIZE;
int y = i / STONE_SIZE;
int nx = STONE_SIZE - 1 - x;
int ny = y;
if ((stone->zuku[y] << x) & 128)
{
dist->zuku[ny] += 128 >> nx;
}
}
return dist;
}
Stone *NOT(const Stone *stone)
{
Stone *resultStone = new Stone;
for(int i = 0; i < STONE_SIZE; i++)
{
resultStone->zuku[i] = ~stone->zuku[i];
}
return resultStone;
}
Stone *AND(const Stone *stone1, const Stone *stone2)
{
Stone *resultStone = new Stone;
for(int i = 0; i < STONE_SIZE; i++)
{
resultStone->zuku[i] = stone1->zuku[i] & stone2->zuku[i];
}
return resultStone;
}
Stone *OR(const Stone *stone1, const Stone *stone2)
{
Stone *resultStone = new Stone;
for(int i = 0; i < STONE_SIZE; i++)
{
resultStone->zuku[i] = stone1->zuku[i] | stone2->zuku[i];
}
return resultStone;
}
Stone *XOR(const Stone *stone1, const Stone *stone2)
{
Stone *resultStone = new Stone;
for(int i = 0; i < STONE_SIZE; i++)
{
resultStone->zuku[i] = stone1->zuku[i] ^ stone2->zuku[i];
}
return resultStone;
}
bool isEmptyStone(const Stone *stone)
{
for(int i = 0; i < STONE_SIZE; i ++)
{
if(stone->zuku[i] != 0) return false;
}
return true;
}
bool isEmptyBoard(const Board *board)
{
for (int y = 0; y < BOARD_SIZE ; y++)
{
for (int i = 0; i < 4; i++)
{
if(board->block[i + y * 4] != 0) return false;
}
}
return true;
}
Stone *getTouchingStone(const Board *board, const Stone *stone, int x, int y, bool filler)
{
Stone *a, *b, *c, *d, *e, *f, *g;
Stone *result = AND(
g = OR(
e = OR(
a = quarryStone(board, x - 1, y, filler),
b = quarryStone(board, x + 1, y, filler)),
f = OR(
c = quarryStone(board, x, y - 1, filler),
d = quarryStone(board, x, y + 1, filler))),
stone);
delete a; delete b; delete c; delete d; delete e; delete f; delete g;
return result;
}
Board *placeStone(const Board *board, const Stone *stone, int x, int y)
{
Stone *stone_copy = cloneStone(stone);
Stone *tmp;
if(x < 0){ tmp = shiftLeft(stone_copy, -x); delete stone_copy; stone_copy = tmp; x = 0;}
else if(x > BOARD_SIZE - STONE_SIZE){ tmp = shiftRight(stone_copy, x - BOARD_SIZE + STONE_SIZE); delete stone_copy; stone_copy = tmp; x = BOARD_SIZE - STONE_SIZE;}
if(y < 0){ tmp = shiftUp(stone_copy, -y); delete stone_copy; stone_copy = tmp; y = 0;}
else if(y > BOARD_SIZE - STONE_SIZE){ tmp = shiftDown(stone_copy, y - BOARD_SIZE + STONE_SIZE); delete stone_copy; stone_copy = tmp; y = BOARD_SIZE - STONE_SIZE;}
Board *new_board = cloneBoard(board);
for(int i = 0; i < STONE_SIZE; i++)
{
new_board->block[x / STONE_SIZE + y * (STONE_SIZE / 2) + (i * (STONE_SIZE / 2))] |= stone_copy->zuku[i] >> (x % STONE_SIZE);
new_board->block[x / STONE_SIZE + y * (STONE_SIZE / 2) + (i * (STONE_SIZE / 2)) + 1] |= stone_copy->zuku[i] << (STONE_SIZE - (x % STONE_SIZE));
}
delete stone_copy;
return new_board;
}
bool canPlace(const Board *board, const Board *board_diff, const Stone *stone, int x, int y, bool first)
{
Stone *a, *b, *c;
if(isEmptyStone(a = AND(stone, b = quarryStone(board, x, y)))){
if(first){
delete a; delete b;
return true;
}else if(! isEmptyStone(c = getTouchingStone(board_diff, stone, x, y, 0))){
delete a; delete b; delete c;
return true;
}else{
delete c;
}
}
delete a; delete b;
return false;
}
/*
* その場所にブロックが置ける時はいくつのブロックが触れているかを返す
* おけない時には-1を返す
* board_diffにはこれまで置いたブロックのみのボードを渡す
*/
int checkPlacingStone(const Board *board, const Board *board_diff, const Stone *stone, int x, int y)
{
if(! canPlace(board, board_diff, stone, x, y)) return -1;
Stone *a;
int tmp = countBitOfStone(a = getTouchingStone(board, stone, x, y));
delete a;
return tmp;
}
bool isEqualStone(const Stone *stone1, const Stone *stone2)
{
for(int i = 0; i < STONE_SIZE; i ++)
{
if(stone1->zuku[i] != stone2->zuku[i]) return false;
}
return true;
}
bool isEqualBoard(const Board *board1, const Board *board2)
{
for (int y = 0; y < BOARD_SIZE ; y++)
{
for (int i = 0; i < 4; i++)
{
if(board1->block[i + y * 4] != board2->block[i + y * 4]) return false;
}
}
return true;
}
Stone *cloneStone(const Stone *stone)
{
Stone *clone = new Stone;
for(int i = 0; i < STONE_SIZE; i ++)
{
clone->zuku[i] = stone->zuku[i];
}
return clone;
}
Board *cloneBoard(const Board *board)
{
Board *new_board = new Board;
for (int y = 0; y < BOARD_SIZE ; y++)
{
for (int i = 0; i < 4; i++)
{
new_board->block[i + y * 4] = board->block[i + y * 4];
}
}
return new_board;
}
inline bool getCellOfStone(const Stone *stone, int x, int y)
{
return (bool)(stone->zuku[y] & 0x80 >> x);
}
inline bool getCellOfBoard(const Board *board, int x, int y)
{
return (bool)(board->block[x / 8 + y * 4] & 0x80 >> (x % 8));
}
inline void setCellOfStone(Stone *stone, int x, int y, bool value)
{
if(value)
stone->zuku[y] |= 0x80 >> x;
else
stone->zuku[y] &= ~(0x80 >> x);
}
inline void setCellOfBoard(Board *board, int x, int y, bool value)
{
if(value)
board->block[x / 8 + y * 4] |= 0x80 >> (x % 8);
else
board->block[x / 8 + y * 4] &= ~(0x80 >> (x % 8));
}
void getGroupsCountStone(Stone *stone, bool target, int *groups_count, int *count)
{
*groups_count = *count = 0;
Stone *done = cloneStone(EMPTY_STONE);
int tmp_count;
for(int x = 0; x < STONE_SIZE; x ++){
for(int y = 0; y < STONE_SIZE; y ++){
tmp_count = getGroupsCountStoneInternal(stone, done, target, x, y);
if(tmp_count != 0){
(*groups_count) ++;
(*count) += tmp_count;
}
}
}
delete done;
}
int getGroupsCountStoneInternal(Stone *stone, Stone *done, bool target, int x, int y)
{
if(x < 0 || y < 0 || x >= STONE_SIZE || y >= STONE_SIZE) return 0;
if(getCellOfStone(done, x, y)) return 0;
setCellOfStone(done, x, y, true);
if(getCellOfStone(stone, x, y) == target)
return 1 +
getGroupsCountStoneInternal(stone, done, target, x - 1, y) +
getGroupsCountStoneInternal(stone, done, target, x + 1, y) +
getGroupsCountStoneInternal(stone, done, target, x, y - 1) +
getGroupsCountStoneInternal(stone, done, target, x, y + 1);
else
return 0;
}
void getGroupsCountBoard(Board *board, bool target, int *groups_count, int *count)
{
*groups_count = *count = 0;
Board *done = cloneBoard(EMPTY_BOARD);
int tmp_count;
for(int x = 0; x < BOARD_SIZE; x ++){
for(int y = 0; y < BOARD_SIZE; y ++){
tmp_count = getGroupsCountBoardInternal(board, done, target, x, y);
if(tmp_count != 0){
(*groups_count) ++;
(*count) += tmp_count;
}
}
}
delete done;
}
int getGroupsCountBoardInternal(Board *board, Board *done, bool target, int x, int y)
{
if(x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE) return 0;
if(getCellOfBoard(done, x, y)) return 0;
setCellOfBoard(done, x, y, true);
if(getCellOfBoard(board, x, y) == target)
return 1 +
getGroupsCountBoardInternal(board, done, target, x - 1, y) +
getGroupsCountBoardInternal(board, done, target, x + 1, y) +
getGroupsCountBoardInternal(board, done, target, x, y - 1) +
getGroupsCountBoardInternal(board, done, target, x, y + 1);
else
return 0;
}
void getGroupsStone(Stone *stone, bool target, std::vector<Stone *> &stones, int *groups_count, int *count)
{
*groups_count = *count = 0;
Stone *done = cloneStone(EMPTY_STONE);
Stone *result = cloneStone(EMPTY_STONE);
int tmp_count;
for(int x = 0; x < STONE_SIZE; x ++){
for(int y = 0; y < STONE_SIZE; y ++){
tmp_count = getGroupsStoneInternal(stone, done, result, target, x, y);
if(tmp_count != 0){
stones.push_back(result);
result = cloneStone(EMPTY_STONE);
(*groups_count) ++;
(*count) += tmp_count;
}
}
}
delete done;
if(isEmptyStone(result)) delete result;
}
int getGroupsStoneInternal(Stone *stone, Stone *done, Stone *result, bool target, int x, int y)
{
if(x < 0 || y < 0 || x >= STONE_SIZE || y >= STONE_SIZE) return 0;
if(getCellOfStone(done, x, y)) return 0;
setCellOfStone(done, x, y, true);
if(getCellOfStone(stone, x, y) == target){
setCellOfStone(result, x, y, true);
return 1 +
getGroupsStoneInternal(stone, done, result, target, x - 1, y) +
getGroupsStoneInternal(stone, done, result, target, x + 1, y) +
getGroupsStoneInternal(stone, done, result, target, x, y - 1) +
getGroupsStoneInternal(stone, done, result, target, x, y + 1);
}else{
return 0;
}
}
void getGroupsBoard(Board *board, bool target, std::vector<Board *> &boards, int *groups_count, int *count)
{
*groups_count = *count = 0;
Board *done = cloneBoard(EMPTY_BOARD);
Board *result = cloneBoard(EMPTY_BOARD);
int tmp_count;
for(int x = 0; x < BOARD_SIZE; x ++){
for(int y = 0; y < BOARD_SIZE; y ++){
tmp_count = getGroupsBoardInternal(board, done, result, target, x, y);
if(tmp_count != 0){
boards.push_back(result);
result = cloneBoard(EMPTY_BOARD);
(*groups_count) ++;
(*count) += tmp_count;
}
}
}
delete done;
if(isEmptyBoard(result)) delete result;
}
int getGroupsBoardInternal(Board *board, Board *done, Board *result, bool target, int x, int y)
{
if(x < 0 || y < 0 || x >= BOARD_SIZE || y >= BOARD_SIZE) return 0;
if(getCellOfBoard(done, x, y)) return 0;
setCellOfBoard(done, x, y, true);
if(getCellOfBoard(board, x, y) == target){
setCellOfBoard(result, x, y, true);
return 1 +
getGroupsBoardInternal(board, done, result, target, x - 1, y) +
getGroupsBoardInternal(board, done, result, target, x + 1, y) +
getGroupsBoardInternal(board, done, result, target, x, y - 1) +
getGroupsBoardInternal(board, done, result, target, x, y + 1);
}else{
return 0;
}
}
Stone *normalizeStone(const Stone *stone)
{
Stone *source = cloneStone(stone), *shifted1, *shifted2;
while(true){
shifted1 = shiftLeft(source, 1);
shifted2 = shiftRight(shifted1, 1);
if(! isEqualStone(source, shifted2)){
delete shifted1; delete shifted2;
break;
}
delete source;
delete shifted2;
source = shifted1;
}
while(true){
shifted1 = shiftUp(source, 1);
shifted2 = shiftDown(shifted1, 1);
if(! isEqualStone(source, shifted2)){
delete shifted1; delete shifted2;
break;
}
delete source;
delete shifted2;
source = shifted1;
}
return source;
}
State *getState(Stone *stone, bool flipped, int turn){
State *state = new State();
for(int i = 0; i < STONE_SIZE; i ++)
{
state->zuku[i] = stone->zuku[i];
}
state->flipped = flipped;
state->turn = turn;
return state;
}
void getStatesOfStone(const Stone *source, std::vector<State *> &states)
{
Stone *stone, *stone90, *stone180, *stone270, *flipped_stone, *flipped_stone90, *flipped_stone180, *flipped_stone270;
Stone *stone_n, *stone90_n, *stone180_n, *stone270_n, *flipped_stone_n, *flipped_stone90_n, *flipped_stone180_n, *flipped_stone270_n;
stone = cloneStone(source); stone_n = normalizeStone(stone);
stone90 = rotate90(stone); stone90_n = normalizeStone(stone90);
stone180 = rotate180(stone); stone180_n = normalizeStone(stone180);
stone270 = rotate270(stone); stone270_n = normalizeStone(stone270);
flipped_stone = flip(stone); flipped_stone_n = normalizeStone(flipped_stone);
flipped_stone90 = rotate90(flipped_stone); flipped_stone90_n = normalizeStone(flipped_stone90);
flipped_stone180 = rotate180(flipped_stone); flipped_stone180_n = normalizeStone(flipped_stone180);
flipped_stone270 = rotate270(flipped_stone); flipped_stone270_n = normalizeStone(flipped_stone270);
bool flip =
isEqualStone(stone_n, flipped_stone180_n) ||
isEqualStone(stone_n, flipped_stone_n) ||
isEqualStone(stone_n, flipped_stone90) ||
isEqualStone(stone_n, flipped_stone270); // do not add flipped stones to states
if(isEqualStone(stone_n, stone90_n)){
states.push_back(getState(stone, false, 0));
if(!flip){
states.push_back(getState(flipped_stone, true, 0));
}
}else if(isEqualStone(stone_n, stone180_n)){
states.push_back(getState(stone, false, 0)); states.push_back(getState(stone90, false, 1));
if(!flip){
states.push_back(getState(flipped_stone, true, 0)); states.push_back(getState(flipped_stone90, true, 1));
}
}else{
states.push_back(getState(stone, false, 0)); states.push_back(getState(stone90, false, 1));
states.push_back(getState(stone180, false, 2)); states.push_back(getState(stone270, false, 3));
if(!flip){
states.push_back(getState(flipped_stone, true, 0)); states.push_back(getState(flipped_stone90, true, 1));
states.push_back(getState(flipped_stone180, true, 2)); states.push_back(getState(flipped_stone270, true, 3));
}
}
delete stone; delete stone90; delete stone180; delete stone270;
delete flipped_stone; delete flipped_stone90; delete flipped_stone180; delete flipped_stone270;
delete stone_n; delete stone90_n; delete stone180_n; delete stone270_n;
delete flipped_stone_n; delete flipped_stone90_n; delete flipped_stone180_n; delete flipped_stone270_n;
}
extern void convertStonesToVectorOfStates(Stone *stones, int num, std::vector<std::vector<State *> > &states){
Stone *tmp = stones;
states.resize(num);
for(int i = 0; i < num; i ++){
getStatesOfStone(&*(stones ++), states[i]);
}
stones = tmp;
}
double evalBoard(Board *board)
{
int space;
int maxline = 0,line = 0;
int column[32],maxcolumn = 0;
int cnt,count;
double density[16],aveden[4];
double variance[4],allvariance;
double pergroup;
double evalation;
const double bias_1 = 4.0 ,bias_2 = 20.0;
getGroupsCountBoard(board, 0, &cnt,&count);
space = 1024 - countBitOfBoard(board);
for(int i = 0; i < 32; i++) column[i] = 0;
for(int i = 0; i < 16; i++) density[i] = 0;
for(int i = 0; i < 4; i++) variance[i] = 0;
for(int i = 0; i < 128; i++)
{
if(i % 4 == 0)
{
if(line > maxline) maxline = line;
line = 0;
}
for(int j = 0; j < 8; j++) column[(i%4)*8+j] += (board->block[i] >> (7-j)) % 2;
density[i%4 + (i/32) * 4] += countBit(board->block[i]);
line += countBit(board->block[i]);
}
for(int i = 0; i < 32; i++)
{
if(column[i] > maxcolumn) maxcolumn = column[i];
}
for(int i = 0; i < 4; i++)
{
variance[i] = ((density[i*4]-density[i*4 + 1]) * (density[i*4]-density[i*4 + 1])) + ((density[i*4 + 1]-density[i*4 + 2]) * (density[i*4 + 1]-density[i*4 + 2])) + ((density[i*4 + 2]-density[i*4 + 3]) * (density[i*4 + 2]-density[i*4 + 3]));
aveden[i] = (density[i*4] + density[i*4 + 1] + density[i*4 + 2] + density[i * 4 + 3]) / 4;
variance[i] = variance[i] / (aveden[i] + 1);
}
allvariance = ((variance[0]-variance[1]) * (variance[0]-variance[1])) + ((variance[1]-variance[2]) * (variance[1]-variance[2])) + ((variance[2]-variance[3]) * (variance[2]-variance[3]));
allvariance = allvariance / (1025 - count);
pergroup = (double)cnt / ((double)count + 1) * 1000;
evalation = space - (maxline + maxcolumn) * bias_1 + allvariance * bias_2 - pergroup;
return evalation;
}
int countScore(Answers &ans, Problem &prob)
{
Board *obstacleBoard = new Board;
Board *putBoard = new Board;
*obstacleBoard = prob.board;
for(int i = 0; i < 128; i++) putBoard -> block[i] = 0;
int i;
bool canPut = false;
bool first = true;
for(i = 0; i < ans.answers.size(); i++)
{
if(first)
{
canPut = canPlace(obstacleBoard, putBoard, rotate(&prob.stones[ans.answers[i].stoneNumber], ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y, first);
first = false;
}
else if(!ans.answers[i].flipped)
{
canPut = canPlace(obstacleBoard, putBoard, rotate(&prob.stones[ans.answers[i].stoneNumber], ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
}
else
{
canPut = canPlace(obstacleBoard, putBoard, rotate(flip(&prob.stones[ans.answers[i].stoneNumber]), ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
}
if(!canPut) break;
if(!ans.answers[i].flipped)
{
obstacleBoard = placeStone(obstacleBoard, rotate(&prob.stones[ans.answers[i].stoneNumber], ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
putBoard = placeStone(putBoard, rotate(&prob.stones[ans.answers[i].stoneNumber], ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
}
else
{
obstacleBoard = placeStone(obstacleBoard, rotate(flip(&prob.stones[ans.answers[i].stoneNumber]), ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
putBoard = placeStone(putBoard, rotate(flip(&prob.stones[ans.answers[i].stoneNumber]), ans.answers[i].turn), ans.answers[i].X, ans.answers[i].Y);
}
}
int score = 0;
if(!canPut)
{
for(int y = 0; y < BOARD_SIZE; y++)
{
for(int x = 0; x < BOARD_SIZE; x++)
{
score += (int)!getCellOfBoard(obstacleBoard, x, y);
}
}
return score;
}
else
{
for(int y = 0; y < BOARD_SIZE; y++)
{
for(int x = 0; x < BOARD_SIZE; x++)
{
score += (int)!getCellOfBoard(obstacleBoard, x, y);
}
}
return score;
}
delete obstacleBoard;
delete putBoard;
}
Board *BoardNOT(const Board *board)
{
Board *resultBoard = new Board;
for(int i = 0; i < BOARD_LOOP; i++)
{
resultBoard->block[i] = ~board->block[i];
}
return resultBoard;
}
Board *BoardAND(const Board *Board1, const Board *Board2)
{
Board *resultBoard = new Board;
for(int i = 0; i < BOARD_LOOP; i++)
{
resultBoard->block[i] = Board1->block[i] & Board2->block[i];
}
return resultBoard;
}
Board *BoardOR(const Board *Board1, const Board *Board2)
{
Board *resultBoard = new Board;
for(int i = 0; i < BOARD_LOOP; i++)
{
resultBoard->block[i] = Board1->block[i] | Board2->block[i];
}
return resultBoard;
}
Board *BoardXOR(const Board *Board1, const Board *Board2)
{
Board *resultBoard = new Board;
for (int i = 0; i < BOARD_LOOP; i++)
{
resultBoard->block[i] = Board1->block[i] ^ Board2->block[i];
}
return resultBoard;
}
bool releaseVector(vector<Stone *> stones)
{
for(unsigned int i = 0; i < stones.size(); i++) delete stones.at(i);
vector<Stone *>().swap(stones);
if(stones.capacity() != 0) return false;
else return true;
}
bool releaseVector(vector<Board *> boards)
{
for(unsigned int i = 0; i < boards.size(); i++) delete boards.at(i);
vector<Board *>().swap(boards);
if(boards.capacity() != 0) return false;
else return true;
}
bool releaseVector(vector<State *> states)
{
for(unsigned int i = 0; i < states.size(); i++) delete states.at(i);
vector<State *>().swap(states);
if(states.capacity() != 0) return false;
else return true;
}
bool releaseVector(vector< vector<State *> > stones)
{
for(unsigned int i = 0; i < stones.size(); i++)
{
for(unsigned int j = 0; j < stones.at(i).size(); j++) delete stones.at(i).at(j);
}
vector< vector<State *> >().swap(stones);
if(stones.capacity() != 0) return false;
else return true;
}
bool releaseVector(vector< vector<Stone *> > stones)
{
for(unsigned int i = 0; i < stones.size(); i++)
{
for(unsigned int j = 0; j < stones.at(i).size(); j++) delete stones.at(i).at(j);
}
vector< vector<Stone *> >().swap(stones);
if(stones.capacity() != 0) return false;
else return true;
}