-
Notifications
You must be signed in to change notification settings - Fork 54
/
main.c
5841 lines (5502 loc) · 170 KB
/
main.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
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 _CRT_SECURE_NO_WARNINGS /* for vs */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "yixin.h"
#include "resource.h" /* not needed if you are not provided with resource.h, Yixin.rc and icon.ico */
typedef long long I64;
#define MAX_SIZE 22
#define CAUTION_NUM 4 //0..CAUTION_NUM
#define MIN_SPLIT_DEPTH 5
#define MAX_SPLIT_DEPTH 20
#define MAX_TOOLBAR_ITEM 32
#define MAX_TOOLBAR_COMMAND_LEN 2048
#define MAX_HOTKEY_ITEM 32
#define MAX_HOTKEY_COMMAND_LEN 2048
int yixin_strnicmp(const char *string1, const char *string2, size_t count)
{
#ifdef _WIN32
return _strnicmp(string1, string2, count);
#else
return strncasecmp(string1, string2, count);
#endif
}
int respawn = 0;
int showdatabase = 1;
int usedatabase = 1;
int zobristflag = 1;
I64 zobrist[MAX_SIZE][MAX_SIZE][3];
int boardsizeh = 15, boardsizew = 15;
int rboardsizeh = 15, rboardsizew = 15;
int inforule = 0;
int specialrule = 0;
int infopondering = 0;
int infovcthread = 0;
int timeoutturn = 10000;
int timeoutmatch = 2000000;
int maxdepth = 100;
int maxnode = 1000000000;
int maxthreadnum = 1; //1..maxthreadnum
int maxhashsize = 21;
int increment = 0;
int computerside = 0; /* 0 none 1 black 2 while 3 black&white */
int cautionfactor = 1;
int threadnum = 1;
int hashsize = 19;
int threadsplitdepth = 7;
int nbestsym = 0;
int board[MAX_SIZE][MAX_SIZE];
int boardnumber[MAX_SIZE][MAX_SIZE];
int movepath[MAX_SIZE*MAX_SIZE];
int forbid[MAX_SIZE][MAX_SIZE];
int boardblock[MAX_SIZE][MAX_SIZE];
int boardbest[MAX_SIZE][MAX_SIZE];
int boardlose[MAX_SIZE][MAX_SIZE];
int boardpos[MAX_SIZE][MAX_SIZE];
int boardtag[MAX_SIZE][MAX_SIZE];
int blockautoreset = 0;
int blockpathautoreset = 0;
int hashautoclear = 0;
int piecenum = 0;
char isthinking = 0, isgameover = 0, isneedrestart = 0, isneedomit = 0;
char bestline[MAX_SIZE*MAX_SIZE*5+1] = "";
int bestval;
int move5N;
int levelchoice = 0;
int commandmodel = 0;
int shownumber = 1;
int showlog = 1;
int showanalysis = 1;
int showclock = 1;
int showforbidden = 1;
int showtoolbarboth = 1;
int showsmallfont = 0;
int showwarning = 1;
int checktimeout = 1;
int toolbarpos = 1;
int language = 0; /* 0: English 1: Other languages */
int rlanguage = 0;
char **clanguage = NULL; /* Custom language */
int movx[8] = { 0, 0, 1, -1, 1, 1, -1, -1}; /* note that the order is related to winning checking function(s)*/
int movy[8] = { 1, -1, 0, 0, 1, -1, 1, -1};
/* engine */
GIOChannel *iochannelin, *iochannelout, *iochannelerr;
/* windowmain */
GtkWidget *windowmain;
GtkWidget *tableboard;
GtkWidget *imageboard[MAX_SIZE][MAX_SIZE];
GtkWidget *labelboard[2][MAX_SIZE];
GtkWidget *vboxwindowmain;
GdkPixbuf *pixbufboard[9][14];
GdkPixbuf *pixbufboardnumber[9][14][MAX_SIZE*MAX_SIZE+1][2];
GdkPixbuf *pixbufboardchar[9][14][128][2];
int imgtypeboard[MAX_SIZE][MAX_SIZE];
char piecepicname[80] = "piece.bmp";
/* log */
GtkWidget *textlog;
GtkTextBuffer *buffertextlog, *buffertextcommand;
GtkWidget *scrolledtextlog, *scrolledtextcommand;
GtkWidget *toolbar;
double hdpiscale = 1.0;
int toolbarnum = 6;
GtkWidget *windowclock;
GtkWidget *clocklabel[4];
GtkWidget *playerlabel[2];
int timercomputerturn;
int timercomputermatch;
int timerhumanturn;
int timerhumanmatch;
int timercomputerincrement;
int timerhumanincrement;
int timerstart;
int timerstatus = 0;
int timeoutflag = 0;
int recorddebuglog = 0;
FILE *debuglog;
int toolbarlng[MAX_TOOLBAR_ITEM] =
{
48,
46,
47,
49,
45,
44
};
char *toolbaricon[MAX_TOOLBAR_ITEM] =
{
GTK_STOCK_GOTO_FIRST,
GTK_STOCK_GO_BACK,
GTK_STOCK_GO_FORWARD,
GTK_STOCK_GOTO_LAST,
GTK_STOCK_STOP,
GTK_STOCK_EXECUTE
};
char toolbarcommand[MAX_TOOLBAR_ITEM][MAX_TOOLBAR_COMMAND_LEN] =
{
"undo all\n",
"undo one\n",
"redo one\n",
"redo all\n",
"thinking stop\n",
"thinking start\n"
};
int hotkeynum = 6;
int hotkeykey[MAX_HOTKEY_ITEM] =
{
13,
14,
15,
16,
27,
11
};
char hotkeycommand[MAX_HOTKEY_ITEM][MAX_HOTKEY_COMMAND_LEN] =
{
"undo all\n",
"redo all\n",
"undo one\n",
"redo one\n",
"thinking stop\n",
"thinking start\n"
};
char *hotkeynamelis[] = {
"",
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"Ctrl + Up", "Ctrl + Down", "Ctrl + Left", "Ctrl + Right",
"Ctrl + 1", "Ctrl + 2", "Ctrl + 3", "Ctrl + 4", "Ctrl + 5", "Ctrl + 6", "Ctrl + 7", "Ctrl + 8", "Ctrl + 9", "Ctrl + 0",
"Escape", NULL
};
int hotkeykeylis[][2] =
{
{ 0, 0 }, //0
{ 0, GDK_F1 }, //1
{ 0, GDK_F2 }, //2
{ 0, GDK_F3 }, //3
{ 0, GDK_F4 }, //4
{ 0, GDK_F5 }, //5
{ 0, GDK_F6 }, //6
{ 0, GDK_F7 }, //7
{ 0, GDK_F8 }, //8
{ 0, GDK_F9 }, //9
{ 0, GDK_F10 }, //10
{ 0, GDK_F11 }, //11
{ 0, GDK_F12 }, //12
{ GDK_CONTROL_MASK, GDK_Up }, //13
{ GDK_CONTROL_MASK, GDK_Down }, //14
{ GDK_CONTROL_MASK, GDK_Left }, //15
{ GDK_CONTROL_MASK, GDK_Right }, //16
{ GDK_CONTROL_MASK, GDK_1 }, //17
{ GDK_CONTROL_MASK, GDK_2 }, //18
{ GDK_CONTROL_MASK, GDK_3 }, //19
{ GDK_CONTROL_MASK, GDK_4 }, //20
{ GDK_CONTROL_MASK, GDK_5 }, //21
{ GDK_CONTROL_MASK, GDK_6 }, //22
{ GDK_CONTROL_MASK, GDK_7 }, //23
{ GDK_CONTROL_MASK, GDK_8 }, //24
{ GDK_CONTROL_MASK, GDK_9 }, //25
{ GDK_CONTROL_MASK, GDK_0 }, //26
{ 0, GDK_Escape } //27
};
char * _T(char *s)
{
return g_locale_to_utf8(s, -1, 0, 0, 0);
}
void print_log(char *text)
{
GtkTextIter start, end;
GtkTextMark *endmark;
static int init = 0;
static int flag = 0, fspace = 0;
int len;
int i;
if(buffertextlog == NULL)
{
printf("%s", text);
return;
}
if (commandmodel == 0)
{
if (strncmp(text, "OK", 2) == 0) text += 2;
if (strncmp(text, "MESSAGE", 7) == 0) text += 7 + 1;
if (strncmp(text, "DETAIL", 6) == 0) text += 6 + 1;
if (strncmp(text, "DEBUG", 5) == 0) text += 5 + 1;
if (strncmp(text, "ERROR", 5) == 0) text += 5;
if (strncmp(text, "UNKNOWN", 7) == 0) text += 7;
//if(strncmp(text, "FORBID", 6) == 0) text += 6;
if (strncmp(text, "YIXIN", 5) == 0 || strncmp(text, "DEEP YIXIN", 10) == 0)
{
if (flag == 0) flag = 1; else return;
}
for (i = 0; text[i]; i++)
{
if (!isspace(text[i])) break;
}
if (text[i] == 0)
{
fspace++;
}
else
{
fspace = 0;
}
if (fspace >= 2) return;
}
len = gtk_text_buffer_get_line_count(GTK_TEXT_BUFFER(buffertextlog));
if(len > 400)
{
gtk_text_buffer_get_iter_at_line(GTK_TEXT_BUFFER(buffertextlog), &start, 0);
gtk_text_buffer_get_iter_at_line(GTK_TEXT_BUFFER(buffertextlog), &end, len-400);
gtk_text_buffer_delete(GTK_TEXT_BUFFER(buffertextlog), &start, &end);
}
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffertextlog), &start, &end);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffertextlog), &end, text, strlen(text));
gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(buffertextlog), &end);
gtk_text_iter_set_line_offset(&end, 0);
if(init == 0)
{
init = 1;
gtk_text_buffer_create_mark(GTK_TEXT_BUFFER(buffertextlog), "scroll", &end, TRUE);
}
endmark = gtk_text_buffer_get_mark(GTK_TEXT_BUFFER(buffertextlog), "scroll");
gtk_text_buffer_move_mark(GTK_TEXT_BUFFER(buffertextlog), endmark, &end);
gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(textlog), endmark);
}
int printf_log(char *fmt, ...)
{
int cnt;
char buffer[8192];
char text[8192], *p = text;
int i;
va_list va;
va_start(va,fmt);
cnt = vsprintf(buffer, fmt, va);
if(language)
{
for(i=0; buffer[i]; )
{
if(strncmp(buffer+i, "BESTLINE", 8) == 0)
{
strcpy(p, clanguage[0]);
p += strlen(clanguage[0]);
i += 8;
continue;
}
if(strncmp(buffer+i, "EVALUATION", 10) == 0)
{
strcpy(p, clanguage[1]);
p += strlen(clanguage[1]);
i += 10;
continue;
}
if(strncmp(buffer+i, "SPEED", 5) == 0)
{
strcpy(p, clanguage[2]);
p += strlen(clanguage[2]);
i += 5;
continue;
}
if(strncmp(buffer+i, "TIME", 4) == 0)
{
strcpy(p, clanguage[3]);
p += strlen(clanguage[3]);
i += 4;
continue;
}
if(strncmp(buffer+i, "DEPTH", 5) == 0)
{
strcpy(p, clanguage[4]);
p += strlen(clanguage[4]);
i += 5;
continue;
}
if(strncmp(buffer+i, "BLOCK", 5) == 0)
{
strcpy(p, clanguage[5]);
p += strlen(clanguage[5]);
i += 5;
continue;
}
if(strncmp(buffer+i, "NODE", 4) == 0)
{
strcpy(p, clanguage[6]);
p += strlen(clanguage[6]);
i += 4;
continue;
}
if(strncmp(buffer+i, "VAL", 3) == 0)
{
strcpy(p, clanguage[7]);
p += strlen(clanguage[7]);
i += 3;
continue;
}
if(strncmp(buffer+i, "MS", 2) == 0)
{
strcpy(p, clanguage[8]);
p += strlen(clanguage[8]);
i += 2;
continue;
}
if(strncmp(buffer+i, "RULE", 4) == 0)
{
strcpy(p, clanguage[9]);
p += strlen(clanguage[9]);
i += 4;
continue;
}
*p = buffer[i];
p ++;
i ++;
}
*p = '\0';
}
else
{
strcpy(text, buffer);
}
print_log(_T(text));
va_end(va);
return cnt;
}
void print_command(char *text)
{
GtkTextIter start, end;
if(buffertextcommand == NULL)
{
printf("%s", text);
return;
}
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buffertextcommand), &start, &end);
gtk_text_buffer_insert(GTK_TEXT_BUFFER(buffertextcommand), &end, text, strlen(text));
}
int printf_command(char *fmt, ...)
{
int cnt;
char buffer[1024];
va_list va;
va_start(va,fmt);
cnt = vsprintf(buffer, fmt, va);
print_command(_T(buffer));
va_end(va);
return cnt;
}
void show_welcome()
{
printf_log("Yixin Board "VERSION"\n");
if(language)
{
printf_command(clanguage[10]);
}
else
{
printf_command("To get help, type help and press Enter here");
}
}
void show_thanklist()
{
printf_log(language==0?"Thanks to all who helped development of Yixin:":
clanguage[11]);
printf_log("\n");
printf_log(" 彼方\n");
printf_log(" XR\n");
printf_log(" 舒自均\n");
printf_log(" Tianyi Hao\n");
printf_log(" Hao Wu\n");
printf_log(" 雨中飞燕\n");
printf_log(" Tuyen Do\n");
printf_log(" 肥国乃乃\n");
printf_log(" Saturn|Titan\n");
printf_log(" 元\n");
printf_log(" Alexander Bogatirev\n");
printf_log(" Epifanov Dmitry\n");
printf_log(" TZ\n");
printf_log(" 濤声依旧\n");
printf_log(" 嘿嘿\n");
printf_log(" 张锡森\n");
printf_log(" ax_pokl\n");
printf_log(" Ola Strom");
printf_log("\n");
}
GdkPixbuf *draw_overlay(GdkPixbuf *pb, int w, int h, gchar *text, char *color, int type)
{
GdkPixmap *pm;
GdkGC *gc;
GtkWidget *scratch;
PangoLayout *layout;
gchar *markup;
GdkPixbuf *ret;
gchar format[100];
pm = gdk_pixmap_new(windowmain->window, w, h, -1);
gdk_drawable_set_colormap(pm, gdk_colormap_get_system());
gc = gdk_gc_new(pm);
gdk_draw_pixbuf(pm, gc, pb, 0, 0, 0, 0, w, h, GDK_RGB_DITHER_NONE, 0, 0);
scratch = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_realize(scratch);
layout = gtk_widget_create_pango_layout(scratch, NULL);
gtk_widget_destroy(scratch);
if (type == 0)
sprintf(format, "<b><span foreground='%s' size='%d'>%%s</span></b>", color, 11000);
else
sprintf(format, "<b><span foreground='%s' size='%d'>%%s</span></b>", color, 9000);
markup = g_strdup_printf(format, text);
pango_layout_set_markup(layout, markup, -1);
g_free(markup);
if (type == 0)
gdk_draw_layout(pm, gc, w / 2 - (int)(strlen(text) * 4 * hdpiscale), h / 2 - (int)(10* hdpiscale), layout);
else
gdk_draw_layout(pm, gc, w / 2 - (int)(strlen(text) * 4 * hdpiscale), h / 2 - (int)(8 * hdpiscale), layout);
g_object_unref(layout);
ret = gdk_pixbuf_get_from_drawable(NULL, pm, NULL, 0, 0, 0, 0, w, h);
g_object_unref(pm);
g_object_unref(gc);
return ret;
}
void send_command(char *command)
{
gsize size;
g_io_channel_write_chars(iochannelin, command, -1, &size, NULL);
g_io_channel_flush(iochannelin, NULL);
//printf_log(command); //for debug
if (debuglog != NULL)
{
fprintf(debuglog, "SEND_COMMAND [%s,%s,%s,%s]: %s\n",
gtk_label_get_text(clocklabel[0]),
gtk_label_get_text(clocklabel[1]),
gtk_label_get_text(clocklabel[2]),
gtk_label_get_text(clocklabel[3]),
command);
fflush(debuglog);
}
}
int swap2done = 0;
int refreshboardflag = 0; //it is set to 1 when making the 5th move under soosorv rule, otherwise, 0
int refreshboardflag2 = 0; //it is set to 1 when refreshboardflag has been set to 1
void refresh_board()
{
int i, j;
for (i = 0; i < boardsizeh; i++)
{
for (j = 0; j < boardsizew; j++)
{
if (board[i][j] == 0)
{
int f = 0;
if (inforule == 2 && (computerside & 1) == 0 && piecenum % 2 == 0 && forbid[i][j] && isgameover == 0 && isthinking == 0 && showforbidden) f = 2;
if (f == 0)
{
if (boardblock[i][j]) f = 10;
else if (showanalysis)
{
if (boardlose[i][j]) f = 7;
else if (boardbest[i][j]) f = 8;
else if (boardpos[i][j] == 1) f = 9;
else if (boardpos[i][j] == 2) f = 11;
}
if (f == 0 && usedatabase && showdatabase)
{
if (boardtag[i][j])
f = 12 + piecenum % 2;
}
}
if (f <= 11 || boardtag[i][j] <= 0)
{
if (imgtypeboard[i][j] <= 8)
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[imgtypeboard[i][j]][max(0, f)]);
else
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[0][max(1, f)]);
}
else
{
int x, y;
GdkPixbuf *p = NULL;
char n[10];
if (imgtypeboard[i][j] <= 8)
{
y = imgtypeboard[i][j];
x = 0;
}
else
{
y = 0;
x = 1;
}
if (boardtag[i][j] < 128)
{
if (pixbufboardchar[y][x][boardtag[i][j]][piecenum % 2] == NULL)
{
sprintf(n, "%c", boardtag[i][j] == 'w' ? 'W' : (boardtag[i][j] == 'l' ? 'L' : boardtag[i][j]));
pixbufboardchar[y][x][boardtag[i][j]][piecenum % 2] = draw_overlay(pixbufboard[y][x], gdk_pixbuf_get_width(pixbufboard[y][x]), gdk_pixbuf_get_height(pixbufboard[y][x]), n, piecenum % 2 ? "#FFFFFF" : "#000000", showsmallfont);
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardchar[y][x][boardtag[i][j]][piecenum % 2]);
}
else
{
if (boardtag[i][j] == 'w')
sprintf(n, "W");
else if (boardtag[i][j] == 'l')
sprintf(n, "L");
else if (boardtag[i][j] / 256 == 0)
sprintf(n, "%c", boardtag[i][j]);
else
{
sprintf(n, "%c%c", boardtag[i][j] / 256, boardtag[i][j] % 256);
}
p = draw_overlay(pixbufboard[y][x], gdk_pixbuf_get_width(pixbufboard[y][x]), gdk_pixbuf_get_height(pixbufboard[y][x]), n, piecenum % 2 ? "#FFFFFF" : "#000000", showsmallfont);
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), p);
g_object_unref(G_OBJECT(p));
}
}
}
else
{
int f = 0, _f = 0;
int x, y;
int bn = boardnumber[i][j];
int bz = 3 + board[i][j] - 1;
if(movepath[piecenum-1]/boardsizew == i && movepath[piecenum-1]%boardsizew == j) f = 2;
if (refreshboardflag == 1)
{
if (specialrule == 3)
{
int k;
for (k = 4; k < piecenum - 1; k++)
{
if (movepath[k] / boardsizew == i && movepath[k] % boardsizew == j)
{
_f = 2;
bz -= k % 2;
bn = 5;
break;
}
}
if (f)
{
bz -= (piecenum - 1) % 2;
bn = 5;
}
}
}
if(shownumber)
{
y = imgtypeboard[i][j]%9;
x = bz;
if(pixbufboardnumber[y][x][bn][(f || _f)?1:0] == NULL)
{
char n[10];
sprintf(n, "%d", bn);
if(f || _f)
{
pixbufboardnumber[y][x][bn][1] = draw_overlay(pixbufboard[y][x], gdk_pixbuf_get_width(pixbufboard[y][x]), gdk_pixbuf_get_height(pixbufboard[y][x]), n, "#FF0000", showsmallfont);
}
else
{
if(boardnumber[i][j] % 2 == 1)
pixbufboardnumber[y][x][bn][0] = draw_overlay(pixbufboard[y][x], gdk_pixbuf_get_width(pixbufboard[y][x]), gdk_pixbuf_get_height(pixbufboard[y][x]), n, "#FFFFFF", showsmallfont);
else
pixbufboardnumber[y][x][bn][0] = draw_overlay(pixbufboard[y][x], gdk_pixbuf_get_width(pixbufboard[y][x]), gdk_pixbuf_get_height(pixbufboard[y][x]), n, "#000000", showsmallfont);
}
}
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboardnumber[y][x][bn][(f || _f)?1:0]);
}
else
{
y = imgtypeboard[i][j]%9;
x = f+_f+bz;
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[y][x]);
}
}
}
}
//for debug
/*
for(i=0; i<boardsizeh; i++)
{
for(j=0; j<boardsizew; j++)
{
if(i <= 8 && j <= 13)
{
gtk_image_set_from_pixbuf(GTK_IMAGE(imageboard[i][j]), pixbufboard[i][j]);
}
}
}
*/
}
int is_legal_move(int y, int x)
{
return y>=0 && x>=0 && y<boardsizeh && x<boardsizew && board[y][x] == 0;
}
void make_move(int y, int x)
{
int i, j, k;
board[y][x] = piecenum%2+1;
boardnumber[y][x] = piecenum+1;
if(movepath[piecenum] != y*boardsizew+x)
{
movepath[piecenum] = y*boardsizew+x;
for(i=piecenum+1; i<MAX_SIZE*MAX_SIZE; i++) movepath[i] = -1;
}
piecenum ++;
if(piecenum == boardsizeh*boardsizew) isgameover = 1;
memset(bestline, 0, sizeof(bestline));
memset(boardbest, 0, sizeof(boardbest));
memset(boardlose, 0, sizeof(boardlose));
memset(boardpos, 0, sizeof(boardpos));
refresh_board();
for(i=0; i<8; i+=2)
{
int ny, nx;
k = 1;
ny = y;
nx = x;
for(j=1; j<6; j++)
{
ny += movy[i];
nx += movx[i];
if(nx<0 || ny<0 || nx>=boardsizew || ny>=boardsizeh) break;
if(board[ny][nx] != board[y][x]) break;
k ++;
}
ny = y;
nx = x;
for(j=1; j<6; j++)
{
ny -= movy[i];
nx -= movx[i];
if(nx<0 || ny<0 || nx>=boardsizew || ny>=boardsizeh) break;
if(board[ny][nx] != board[y][x]) break;
k ++;
}
if(k==5 || (k>5 && inforule != 1))
{
isgameover = 1;
break;
}
}
}
void show_database()
{
int i;
char command[80];
if (usedatabase)
{
sprintf(command, "yxquerydatabaseall\n");
send_command(command);
for (i = 0; i < piecenum; i++)
{
sprintf(command, "%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
}
}
void show_forbid()
{
int i;
char command[80];
if((computerside&1) || (piecenum%2))
{
memset(forbid, 0, sizeof(forbid));
return;
}
sprintf(command, "start %d %d\n", boardsizew, boardsizeh);
send_command(command);
sprintf(command, "yxboard\n");
send_command(command);
for(i=0; i<piecenum; i++)
{
sprintf(command, "%d,%d,%d\n", movepath[i]/boardsizew,
movepath[i]%boardsizew, piecenum%2==i%2 ? 1 : 2);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
sprintf(command, "yxshowforbid\n");
send_command(command);
}
void show_dialog_undo_warning_query(GtkWidget *window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, language==0?"The operation will stop the calculation. Do you want to continue?":_T(clanguage[12]));
gtk_window_set_title(GTK_WINDOW(dialog), "Yixin");
result = gtk_dialog_run(GTK_DIALOG(dialog));
switch(result)
{
case GTK_RESPONSE_YES:
change_piece(window, (gpointer)1);
break;
case GTK_RESPONSE_NO:
break;
}
gtk_widget_destroy(dialog);
}
void show_dialog_swap_query2(GtkWidget *window)
{
GtkWidget *dialog;
gint result;
char msg[80];
char command[80];
int i;
sprintf(msg, "%s", language == 0 ? "Choose one option" : _T(clanguage[107]));
dialog = gtk_dialog_new_with_buttons(msg, GTK_WINDOW(windowmain), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
language == 0 ? "Stay with white" : _T(clanguage[108]), 1,
language == 0 ? "Swap" : _T(clanguage[109]), 2,
language == 0 ? "Add 2 more pieces" : _T(clanguage[110]), 3,
NULL);
gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
result = gtk_dialog_run(GTK_DIALOG(dialog));
switch (result)
{
case 1:
swap2done = 1;
break;
case 2:
//swap
timerhumanincrement += increment;
if (computerside == 2)
{
change_side_menu(1, NULL);
change_side_menu(-2, NULL);
}
else
{
change_side_menu(-1, NULL);
change_side_menu(2, NULL);
}
if (language) printf_log(clanguage[14]); else printf_log("Swap");
printf_log("\n");
isthinking = 1;
clock_timer_change_status(1);
isneedrestart = 0;
sprintf(command, "INFO time_left %d\n", timeoutmatch - timercomputermatch + timercomputerincrement);
send_command(command);
if (hashautoclear) send_command("yxhashclear\n");
sprintf(command, "start %d %d\n", boardsizew, boardsizeh);
send_command(command);
sprintf(command, "board\n");
send_command(command);
for (i = 0; i<piecenum; i++)
{
sprintf(command, "%d,%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew, piecenum % 2 == i % 2 ? 1 : 2);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
swap2done = 1;
break;
case 3:
//should do nothing
break;
}
gtk_widget_destroy(dialog);
}
void show_dialog_swap_query(GtkWidget *window)
{
GtkWidget *dialog;
gint result;
char msg[80];
if (specialrule == 3 && piecenum == 4)
sprintf(msg, "%s (N=%d)", language == 0 ? "Swap?" : _T(clanguage[13]), move5N);
else
sprintf(msg, "%s", language == 0 ? "Swap?" : _T(clanguage[13]));
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, msg);
gtk_window_set_title(GTK_WINDOW(dialog), "Yixin");
result = gtk_dialog_run(GTK_DIALOG(dialog));
switch(result)
{
case GTK_RESPONSE_YES:
timerhumanincrement += increment;
if (specialrule == 4)
{
int i;
char command[80];
isthinking = 1;
clock_timer_change_status(1);
isneedrestart = 0;
sprintf(command, "INFO time_left %d\n", timeoutmatch - timercomputermatch + timercomputerincrement);
send_command(command);
if (hashautoclear) send_command("yxhashclear\n");
sprintf(command, "start %d %d\n", boardsizew, boardsizeh);
send_command(command);
sprintf(command, "board\n");
send_command(command);
for (i = 0; i<piecenum; i++)
{
sprintf(command, "%d,%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew, piecenum % 2 == i % 2 ? 1 : 2);
send_command(command);
}
sprintf(command, "done\n");
send_command(command);
}
else if (specialrule == 3)
{
isneedrestart = 1;
if (computerside == 2)
{
change_side_menu(1, NULL);
change_side_menu(-2, NULL);
}
else
{
change_side_menu(-1, NULL);
change_side_menu(2, NULL);
}
if (piecenum == 3)
{
int i;
char command[80];
send_command("yxsoosorvstep3\n");
for (i = 0; i < piecenum; i++)
{
sprintf(command, "%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew);
send_command(command);
}
send_command("done\n");
}
else //==4
{
int i;
char command[80];
sprintf(command, "yxsoosorvstep5 %d\n", move5N);
send_command(command);
for (i = 0; i < piecenum; i++)
{
sprintf(command, "%d,%d\n", movepath[i] / boardsizew,
movepath[i] % boardsizew);
send_command(command);
}
send_command("done\n");
}
}
else if(specialrule == 2)
{
isneedrestart = 1;
make_move(4, 5);
if(computerside == 2)
{
change_side_menu(1, NULL);
change_side_menu(-2, NULL);
}
else
{
change_side_menu(-1, NULL);
change_side_menu(2, NULL);
}
}
if(language) printf_log(clanguage[14]); else printf_log("Swap");
printf_log("\n");
break;
case GTK_RESPONSE_NO:
if (specialrule == 4)
{
if (computerside == 2)
{
change_side_menu(1, NULL);
change_side_menu(-2, NULL);
}
else
{
change_side_menu(-1, NULL);
change_side_menu(2, NULL);
}
}
printf_log("\n");
break;
}
gtk_widget_destroy(dialog);
}
void show_dialog_swap_info(GtkWidget *window)
{
GtkWidget *dialog;
gint result;
dialog = gtk_message_dialog_new(GTK_WINDOW(window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_OK, language==0?"Swap":_T(clanguage[15]));
gtk_window_set_title(GTK_WINDOW(dialog), "Yixin");
result = gtk_dialog_run(GTK_DIALOG(dialog));
isneedrestart = 1;
if(computerside == 2)
{
change_side_menu(1, NULL);
change_side_menu(-2, NULL);
}
else
{
change_side_menu(-1, NULL);
change_side_menu(2, NULL);
}
gtk_widget_destroy(dialog);
}
void show_dialog_timeout(GtkWidget *window)