/
gamec.cpp
1531 lines (1449 loc) · 32.7 KB
/
gamec.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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define byte unsigned char
#define Dmy 1
#define Dhis 2
// com.asm
extern "C" Out_Chr();
extern "C" Get_Chr();
extern "C" Ser_Ini();
extern "C" Ser_Rst();
// int9h.asm
extern "C" Key_Ini();
extern "C" Key_Rst();
extern "C" Key_Is_Esc();
extern "C" Get_Key();
// mouse.asm
extern "C" Mouse_Ini();
extern "C" Mouse_Rst();
extern "C" Mouse_Hide();
extern "C" Mouse_Show();
extern "C" Mouse_Clc();
extern "C" Mouse_Clc_Restart();
// timer.asm
extern "C" Timer_Ini();
extern "C" Timer_Rst();
enum chip {None, White, Black, WDamka, BDamka};
enum connect_send_status {CSSTART, C0PING};
enum connect_get_status {CGSTART, GetC, GetC0, GetC0C, GetC0C0, GetC0C0C, CGEXIT};
enum global_game_status {GGSTART, GGNEW, GGCONNECT, GGStartStepWhite, GGStartStepBlack, GGStepWhite, GGStepBlack};
enum get_status {GETSTART, GETS, GETERR, GETC, GETNEXT, GETN, GETD, GETF, GETE, GETLM, GETX, GETXX, GETLEN};
enum command {COMC0, COMCx, COMNG, COMD0, COMD2, COMD3, COMFF, COMEX, COMX};
enum mouse_click_status {MCNone, MCBoard, MCNG, MCD2, MCD3, MCFF};
enum direction {DNone, DLeft, DRight, DUp, DDown};
void graph_ini();
void graph_rst();
void rectangle(int x1, int x2, int y1, int y2, byte color);
void paint_empty_board();
void paint_buttons();
void write_line(byte* line, int size, byte writex, byte writey, byte color);
void debug_ini();
void debug_print(byte* str, int size, byte color);
void debug_print_line(byte* str, int size, byte color);
void debug_print_byte(byte b, byte color);
void mainloop();
void gameloop();
void send_str(byte* str, int size);
void connect();
void timer_ini();
void read_all();
byte random_gesture();
void game();
void connect_send_automat();
void connect_get_automat(byte c);
void rockPaperScissors(byte c);
void sendSS();
void error();
void skip_chat();
void ng_automat();
void get_command();
void get_automat(byte c);
void apply_color(byte x, byte y);
void apply_select(byte x, byte y, byte select);
mouse_click_status get_click();
byte can_step(byte x, byte y, chip color); // color - 梥â 室ï饣®.
byte can_eat(byte x, byte y, chip color);
byte can_move(byte x, byte y, chip color);
byte anyone_can_eat(chip color);
byte damka_can_eat(byte x, byte y, chip color);
void step(chip color);
byte do_step(byte x, byte y, chip color);
byte do_eat(byte x, byte y, chip color);
byte do_move(byte x, byte y, chip color);
byte damka_do_move(byte x, byte y);
void start_step(chip color, chip player);
void start_his_step();
void start_my_step();
void board_init();
byte damka_do_eat(byte x, byte y, chip color);
byte usual_do_eat(byte x, byte y, chip color);
void usual_eat(byte x, byte y);
byte game_over();
void write_hod(byte x, byte y);
void formalize_hod();
void new_game();
void his_step();
void circle(int x1, int y1, int d, byte color);
void point(int x, int y, byte color);
void read_args();
byte cx_send();
byte Page;
byte STR[64];
byte hishod[35];
byte hishod_len;
byte hishod_part;
byte myhod[35];
byte myhod_len;
byte timer18 = 0;
byte timer55 = 0;
connect_send_status css = CSSTART;
connect_get_status cgs = CGSTART;
byte Cx;
byte HisCx;
chip MyColor = None;
chip HisColor = None;
global_game_status ggs = GGCONNECT;
byte NG_is_sent = 0;
byte NG_is_received = 0;
get_status getst = GETSTART;
command comm;
byte templen;
byte isNewCommand = 0;
chip board[8][8];
int click_x;
int click_y;
byte futureDamka;
byte selected_x;
byte selected_y;
direction forbidden_direction;
mouse_click_status mc;
byte drawn_proposed_by_me;
byte drawn_proposed_by_him;
byte argslen;
byte whetherC0;
byte NG_during_game;
void main() {
Key_Ini();
graph_ini();
paint_empty_board();
paint_buttons();
debug_ini();
read_args();
Mouse_Ini();
timer_ini();
Ser_Ini();
mainloop();
Ser_Rst();
Timer_Rst();
Mouse_Rst();
graph_rst();
Key_Rst();
}
void mainloop() {
connect:
MyColor = None;
timer18 = 0;
whetherC0 = 0;
while (MyColor == None) {
Key_Is_Esc();
asm jc end_mainloop
connect();
}
gameloop();
if (ggs == GGCONNECT) goto connect;
end_mainloop:
}
void gameloop() {
byte NG_during_game = 0;
newgame:
debug_print("Ž¦¨¤ ¨¥ ç « ®¢®© ¨£àë", 26, 8);
ggs = GGSTART;
timer18 = 0;
timer55 = 0;
NG_is_sent = 0;
if (NG_during_game) {
NG_is_received = 1;
NG_during_game = 0;
} else {
NG_is_received = 0;
}
getst = GETSTART;
isNewCommand = 0;
drawn_proposed_by_me = 0;
drawn_proposed_by_him = 0;
while (1) {
Key_Is_Esc();
asm jc send_EX
sendSS();
game();
if (ggs == GGNEW) goto newgame;
if (ggs == GGCONNECT) goto end_gameloop;
}
send_EX:
send_str("EX", 2);
debug_print("EX", 2, Dmy);
end_gameloop:
}
void graph_ini() {
// ¥à¥å®¤ ¢ 10h £à ä¨ç¥áª¨© ०¨¬
asm {
mov ah, 00h
mov al, 10h
int 10h
mov ah, 0fh
int 10h
mov Page, bh
}
}
void graph_rst() {
// ‚®§¢à 饨¥ áâ ¤ à⮣® ¢¨¤¥®-०¨¬
asm {
mov ah, 00h
mov al, 3h
int 10h
}
}
void rectangle(int x1, int x2, int y1, int y2, byte color) {
for (int i=y1; i<=y2; i++) {
for (int j=x1; j<=x2; j++) {
point(j, i, color);
}
}
}
#undef byte
void read_args() {
asm mov al, byte ptr cs:[80h]
asm mov argslen, al
if (argslen) argslen--;
}
#define byte unsigned char
void point(int x, int y, byte color) {
asm {
mov ah, 0ch
mov al, color
mov cx, x
mov dx, y
mov bh, Page
int 10h
}
}
void paint_empty_board() {
Mouse_Hide();
byte color = 6;
int x1, x2, y1, y2;
// áç¥à稢 ¥¬ ¯® ¢¥à⨪ «¨
y1 = 0;
y2 = 345;
x1 = 0;
x2 = x1 + 1;
for (int i=0; i<=8; i++) {
rectangle(x1, x2, y1, y2, color);
x1 += 43;
x2 += 43;
}
// áç¥à稢 ¥¬ ¯® £®à¨§®â «¨
y1 = 0;
y2 = y1 + 1;
x1 = 0;
x2 = 345;
for (int j=0; j<=8; j++) {
rectangle(x1, x2, y1, y2, color);
y1 += 43;
y2 += 43;
}
Mouse_Show();
}
void paint_buttons() {
Mouse_Hide();
const byte writex = 45;
const byte writey = 5;
const byte color = 7;
const int size = 2;
byte* line = "NGD2D3FF";
for (int i=0; i<8; i+=2) {
const int y_step = 28;
rectangle(354, 379, 52+y_step*i, 52+y_step*i+2, 8);
rectangle(354, 379, 102+y_step*i-2, 102+y_step*i, 8);
rectangle(354, 354+2, 52+y_step*i, 102+y_step*i, 8);
rectangle(379-2, 379, 52+y_step*i, 102+y_step*i, 8);
write_line(line+i, size, writex, writey+2*i, color);
}
Mouse_Show();
}
void apply_color(byte x, byte y) {
byte color;
switch (board[y][x]) {
case White: color = 8; break;
case WDamka: color = 15; break;
case Black: color = 1; break;
case BDamka: color = 3; break;
default: color = 0;
}
if (MyColor == White) {
y = 7-y;
} else {
x = 7-x;
}
Mouse_Hide();
circle(8 + x*43, 8 + y*43, 29, color);
Mouse_Show();
}
void circle(int x1, int y1, int d, byte color) {
int r = d/2;
for (int y=0; y<d; y++) {
for (int x=0; x<d; x++) {
if ((x-r)*(x-r)+(y-r)*(y-r)<r*r) {
point(x1+x, y1+y, color);
}
}
}
}
void apply_select(byte x, byte y, byte select) {
byte color = 0;
if (select) {
color = 14;
selected_x = x;
selected_y = y;
}
if (MyColor == White) {
y = 7-y;
} else {
x = 7-x;
}
int x1 = 43 - 5 + x*43;
int x2 = 43 - 2 + x*43;
int y1 = 3 + y*43;
int y2 = 6 + y*43;
Mouse_Hide();
rectangle(x1, x2, y1, y2, color);
Mouse_Show();
}
void debug_ini() {
rectangle(386, 389, 0, 349, 8);
}
void debug_print_line(byte* line, int size, byte color) {
// Œ ªá¨¬ «ì ï ¤«¨ áâப¨ 29 ᨬ¢®«®¢
const byte left = 50;
const byte right = 79;
const byte up = 0;
const byte down = 24;
const byte writex = 50;
const byte writey = 24;
Mouse_Hide();
asm {
pusha
// பàã⪠®¤ã áâப㠢¢¥àå ¯¥à¥¤ ¯¥ç âìî ®ç¥à¥¤®©
mov ah, 6
mov al, 1
mov bx, 0000h
mov ch, up
mov cl, left
mov dh, down
mov dl, right
int 10h
popa
}
write_line(line, size, writex, writey, color);
Mouse_Show();
}
void write_line(byte* line, int size, byte writex, byte writey, byte color) {
asm {
pusha
mov ah, 13h
mov al, 0
mov bh, Page
mov bl, color
mov cx, size
mov dl, writex
mov dh, writey
push cs
pop es
mov bp, line
int 10h
popa
}
}
void debug_print_byte(byte b, byte color) {
byte c = b + 48;
debug_print(&c, 1, color);
}
void debug_print(byte* str, int size, byte color) {
// 29 - ¬ ªá¨¬ «ì®¥ ª«¨ç¥á⢮ ᨬ¢®«®¢, ¯¥ç â ¥¬®¥ ®¤®© áâப¥
while (size > 0) {
int part = size <= 29 ? size : 29;
debug_print_line(str, part, color);
str += part;
size -= part;
}
}
void send_str(byte* str, int size) {
byte* str_end = str + size;
byte b;
while (str < str_end) {
b = *str;
asm mov al, b
send_via_com:
Out_Chr();
asm jc send_via_com
str++;
asm mov cx, 7400
l:
asm loop l
}
}
void timer_ini() {
byte* timer18_ptr = &timer18;
byte* timer55_ptr = &timer55;
asm {
push ax; push bx
mov ax, timer18_ptr
mov bx, timer55_ptr
}
Timer_Ini();
asm {
push bx; push ax
}
}
byte random_gesture() {
byte r;
asm {
mov ah, 0
int 1Ah
mov r, dl
}
byte random3 = r % 3 + 1;
return random3;
}
void connect() {
// ®«ã票¥
byte c;
Get_Chr();
asm jc no_char
asm mov c, al
if (argslen) debug_print(&c, 1, 8);
timer55 = 0;
connect_get_automat(c);
no_char:
// Žâ¯à ¢ª
connect_send_automat();
if (timer55 > 18 && cgs != CGSTART) {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
timer55 = 0;
}
}
byte cx_send() {
if (whetherC0) {
STR[0] = 'C';
STR[1] = Cx + 48;
/* send_str("C0", 2);
debug_print("C0", 2, Dmy);*/
send_str(STR, 2);
debug_print(STR, 2, Dmy);
// …᫨ ç¥à¥§ 18 ¥ ¡ã¤¥â ®â¢¥â®£® Cx, ¨¤¥¬ ç «®.
timer18 = 0;
css = C0PING;
return 1;
}
css = C0PING;
return 0;
}
void connect_send_automat() {
switch (css) {
case CSSTART: {
if (timer18 > 18) {
//debug_print('T', 1, 4);
timer18 = 0;
css = C0PING;
debug_print("“áâ ®¢ª ᮥ¤¨¥¨ï", 20, 8);
send_str("C0", 2);
debug_print("C0", 2, Dmy);
whetherC0 = 1;
}
break;
}
case C0PING: {
if (timer18 > 18) {
//debug_print('T', 1, 4);
timer18 = 0;
cgs = CGSTART;
send_str("C0", 2);
debug_print("C0", 2, Dmy);
whetherC0 = 1;
}
break;
}
default: {}
}
}
void connect_get_automat(byte c) {
switch (cgs) {
case CGSTART: {
if (c == 'C') {
cgs = GetC;
} else {
css = CSSTART;
whetherC0 = 0;
}
break;
}
case GetC: {
if (c == '0') {
cgs = GetC0;
debug_print("C0", 2, Dhis);
Cx = random_gesture();
if (!cx_send()) {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
} else {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
break;
}
case GetC0: {
if (c == 'C') {
cgs = GetC0C;
} else {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
break;
}
case GetC0C: {
if (c == '0') {
cgs = GetC0C0;
debug_print("C0", 2, Dhis);
} else {
if (c == '1' || c == '2' || c == '3') {
rockPaperScissors(c);
} else {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
}
break;
}
case GetC0C0: {
if (c == 'C') {
cgs = GetC0C0C;
} else {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
break;
}
case GetC0C0C: {
if (c == '0') {
cgs = GetC0;
debug_print("C0", 2, Dhis);
} else {
if (c == '1' || c == '2' || c == '3') {
rockPaperScissors(c);
} else {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
}
}
break;
}
default: {}
}
}
void rockPaperScissors(byte c) {
cgs = CGEXIT;
STR[0] = 'C';
STR[1] = c;
HisCx = c - 48;
debug_print(STR, 2, Dhis);
if (Cx == HisCx) {
cgs = CGSTART;
css = CSSTART;
whetherC0 = 0;
MyColor = None;
} else {
byte cnb = Cx * 10 + HisCx;
// ¥à¢ ï æ¨äà - ï. “ ¯®¡¥¤¨â¥«ï ç¥àë©.
if (cnb == 12 || cnb == 23 || cnb == 31) {
MyColor = Black;
} else {
MyColor = White;
}
}
}
void sendSS() {
if (timer18 >= 18) {
if (argslen >= 2) debug_print("SS", 2, Dmy);
send_str("SS", 2);
timer18 = 0;
}
if (timer55 >= 55) {
debug_print("®â¥àï á¢ï§ì á ¯à®â¨¢¨ª®¬", 28, 4);
ggs = GGCONNECT;
}
}
void error() {
debug_print("D0", 2, Dmy);
send_str("D0", 2);
ggs = GGNEW;
}
void skip_chat() {
byte c = 0;
while (c != '$') {
Get_Chr();
asm jc no_char
asm mov c, al
no_char:
}
}
void get_command() {
isNewCommand = 0;
while (1) {
byte c;
Get_Chr();
asm jc no_char
asm mov c, al
if (argslen == 2 || (argslen == 1 && c != 'S')) debug_print(&c, 1, 8);
timer55 = 0;
get_automat(c);
if (getst == GETSTART || getst == GETERR) {
isNewCommand = 1;
goto no_char;
}
}
no_char:
}
void get_automat(byte c) {
if (getst == GETNEXT) getst = GETSTART;
switch (getst) {
case GETSTART: {
switch (c) {
case 'S': getst = GETS; break;
case 'C': getst = GETC; break;
case 'N': getst = GETN; break;
case 'D': getst = GETD; break;
case 'F': getst = GETF; break;
case 'E': getst = GETE; break;
case 'L':
case 'M': getst = GETLM;break;
case 'X': getst = GETX; break;
default: getst = GETERR;
}
break;
}
case GETS: {
if (c == 'S') {
if (argslen >= 2) debug_print("SS", 2, Dhis);
getst = GETNEXT;
} else {
getst = GETERR;
}
break;
}
case GETC: {
if (c == '0') {
debug_print("C0", 2, Dhis);
comm = COMC0;
getst = GETSTART;
} else if (c >= '1' && c <= '3') {
STR[0] = 'C';
STR[1] = c;
debug_print(STR, 2, Dhis);
comm = COMCx;
Cx = c - 49;
getst = GETSTART;
} else {
getst = GETERR;
}
break;
}
case GETN: {
if (c == 'G') {
debug_print("NG", 2, Dhis);
comm = COMNG;
getst = GETSTART;
} else {
getst = GETERR;
}
break;
}
case GETD: {
switch (c) {
case '0': {
debug_print("D0", 2, Dhis);
comm = COMD0;
getst = GETSTART;
break;
}
case '2': {
debug_print("D2", 2, Dhis);
comm = COMD2;
getst = GETSTART;
break;
}
case '3': {
debug_print("D3", 2, Dhis);
comm = COMD3;
getst = GETSTART;
break;
}
default: {
if (c >= '4' && c <= '9') {
STR[0] = 'D';
STR[1] = c;
debug_print(STR, 2, Dhis);
getst = GETNEXT;
} else {
getst = GETERR;
}
}
}
break;
}
case GETF: {
if (c == 'F') {
debug_print("FF", 2, Dhis);
comm = COMFF;
getst = GETSTART;
} else {
getst = GETERR;
}
break;
}
case GETE: {
if (c == 'X') {
debug_print("EX", 2, Dhis);
comm = COMEX;
getst = GETSTART;
} else {
getst = GETERR;
}
break;
}
case GETLM: {
if (c == '$') {
debug_print("ML$", 3, Dhis);
getst = GETNEXT;
}
break;
}
case GETX: {
if (c == '0' || c == '1') {
hishod_len = c - 48;
getst = GETXX;
} else {
getst = GETERR;
}
break;
}
case GETXX: {
if (c >= '0' && c <= '9') {
hishod_len = hishod_len*10 + (c - 48);
templen = 0;
getst = GETLEN;
} else {
getst = GETERR;
}
break;
}
case GETLEN: {
hishod[templen+3] = c;
templen++;
if (templen >= hishod_len*2) {
hishod[0] = 'X';
hishod[1] = hishod_len / 10 + 48;
hishod[2] = hishod_len % 10 + 48;
debug_print(hishod, 3+hishod_len*2, Dhis);
comm = COMX;
getst = GETSTART;
}
break;
}
default: {
getst = GETERR;
}
}
}
mouse_click_status get_click() {
int x, y;
int y_step = 28*2;
Mouse_Clc();
asm {
jnc no_clc;
mov x, ax
mov y, bx;
}
goto clc;
no_clc:
return MCNone;
clc:
if (x < 345 && y < 345) {
x = x / 43;
y = y / 43;
if (MyColor == White) {
click_x = x;
click_y = 7-y;
} else {
click_x = 7-x;
click_y = y;
}
return MCBoard;
}
if (x > 356 && x < 377) {
if (y > 54 && y < 100) return MCNG;
if (y > 54+y_step && y < 100+y_step) return MCD2;
if (y > 54+y_step*2 && y < 100+y_step*2) return MCD3;
if (y > 54+y_step*3 && y < 100+y_step*3) return MCFF;
}
return MCNone;
}
byte can_step(byte x, byte y, chip color) {
if (can_eat(x, y, color)) return 1;
if (can_move(x, y, color)) {
if (!anyone_can_eat(color)) {
return 1;
}
}
return 0;
}
byte can_eat(byte x, byte y, chip color) {
byte cell = board[y][x];
if (color == White) {
if (cell == White) {
if (y<6 && board[y+2][x] == None && (board[y+1][x] == Black || board[y+1][x] == BDamka)) return 1;
if (x>1 && board[y][x-2] == None && (board[y][x-1] == Black || board[y][x-1] == BDamka)) return 1;
if (x<6 && board[y][x+2] == None && (board[y][x+1] == Black || board[y][x+1] == BDamka)) return 1;
} else if (cell == WDamka) {
return damka_can_eat(x, y, color);
}
} else {
if (cell == Black) {
if (y>1 && board[y-2][x] == None && (board[y-1][x] == White || board[y-1][x] == WDamka)) return 1;
if (x>1 && board[y][x-2] == None && (board[y][x-1] == White || board[y][x-1] == WDamka)) return 1;
if (x<6 && board[y][x+2] == None && (board[y][x+1] == White || board[y][x+1] == WDamka)) return 1;
} else if (cell == BDamka) {
return damka_can_eat(x, y, color);
}
}
return 0;
}
byte damka_can_eat(byte x, byte y, chip color) {
chip usual, damka; // –¢¥â ¯à®â¨¢¨ª
if (color == White) {
usual = Black;
damka = BDamka;
} else {
usual = White;
damka = WDamka;
}
byte xi, yi;
if (forbidden_direction != DUp) {
yi = y + 1;
while (yi < 7) {
byte cell = board[yi][x];
if ((cell == usual || cell == damka) && board[yi+1][x] == None) return 1;
if (cell != None) break;
yi++;
}
}
if (forbidden_direction != DDown) {
yi = y - 1;
while (yi > 0) {
byte cell = board[yi][x];
if ((cell == usual || cell == damka) && board[yi-1][x] == None) return 1;
if (cell != None) break;
yi--;
}
}
if (forbidden_direction != DRight) {
xi = x + 1;
while (xi < 7) {
byte cell = board[y][xi];
if ((cell == usual || cell == damka) && board[y][xi+1] == None) return 1;
if (cell != None) break;
xi++;
}
}
if (forbidden_direction != DLeft) {
xi = x - 1;
while (xi > 0) {
byte cell = board[y][xi];
if ((cell == usual || cell == damka) && board[y][xi-1] == None) return 1;
if (cell != None) break;
xi--;
}
}
return 0;
}
byte anyone_can_eat(chip color) {
for (byte y=0; y<8; y++)
for (byte x=0; x<8; x++)
if (can_eat(x, y, color))
return 1;
return 0;
}
byte can_move(byte x, byte y, chip color) {
if (color == White) {
if (board[y][x] == White || board[y][x] == WDamka) {
if (y!=7 && board[y+1][x] == None) return 1;
if (x!=0 && board[y][x-1] == None) return 1;
if (x!=7 && board[y][x+1] == None) return 1;
return 0;
}
if (board[y][x] == WDamka) {
if (y!=0 && board[y-1][x] == None) return 1;
}
return 0;
} else {
if (board[y][x] == Black || board[y][x] == BDamka) {
if (y!=0 && board[y-1][x] == None) return 1;
if (x!=0 && board[y][x-1] == None) return 1;
if (x!=7 && board[y][x+1] == None) return 1;
return 0;
}
if (board[y][x] == BDamka) {
if (y!=7 && board[y+1][x] == None) return 1;
}
return 0;
}
}
byte do_step(byte x, byte y, chip color) {
if (can_eat(selected_x, selected_y, color)) {
if (do_eat(x, y, color))
return 2; // 2 - 室 § ª®ç¨«áï à㡪®©.
return 0;
}
return do_move(x, y, color);
}
byte do_eat(byte x, byte y, chip color) {
if (board[selected_y][selected_x] == White || board[selected_y][selected_x] == Black) {
return usual_do_eat(x, y, color);
}
return damka_do_eat(x, y, color);
}
byte damka_do_eat(byte x, byte y, chip color) {
chip usual, damka; // –¢¥â ¯à®â¨¢¨ª
if (color == White) {
usual = Black;
damka = BDamka;
} else {
usual = White;
damka = WDamka;
}
byte xi, yi;
byte enemy_x, enemy_y;
byte enemy_count;
if (y > selected_y && x == selected_x && forbidden_direction != DUp) {
yi = selected_y + 1;
enemy_count = 0;
while (y > yi) {