-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsgf2gif.cpp
1595 lines (1422 loc) · 35.1 KB
/
sgf2gif.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
// MIT License
//
// Copyright (c) 2016-2019 toy80
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// 注意: 以上版权声明只适用于本文件(sgf2gif.cc), 不适用于 3rdpart 目录下的文件.
#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <Shlwapi.h>
#include <CommCtrl.h>
#include <vector>
#include <string>
#include <assert.h>
#include <gdiplus.h>
#include <stdio.h>
#include <stdlib.h>
#include "3rdpart/CxImage/ximage.h"
#include "3rdpart/CxImage/ximagif.h"
#include "3rdpart/simplexnoise1234.h"
#include "3rdpart/Quantize.h"
#include "res/resource.h"
// #define S2F_RENJU_ONLY
// #define S2F_ENG // 使用英文标题
#ifdef S2F_ENG
# define STR_WHITE L"W"
# define STR_BLACK L"B"
# define STR_VS L" "
# define STR_UNKOWN L"Unkown"
# define STR_WIN L"+"
# define STR_WIN_BY_RESIGN L"+R"
#
#else
# define STR_WHITE L"白"
# define STR_BLACK L"黑"
# define STR_VS L" "
# define STR_UNKOWN L"未知"
# define STR_WIN L"胜"
# define STR_WIN_BY_RESIGN L"中盘胜"
#endif
using namespace Gdiplus;
using namespace std;
const int MARGIN = 4; // 边距, 防止内容紧贴着图片边界
const int TITLE_HEIGHT = 20;
const int LEFT_SPACE_WIDTH = 0;
const int MAX_BOARD_SIZE = 27;
const int MAX_MOVE = MAX_BOARD_SIZE * MAX_BOARD_SIZE * 4; // ugly, but works
const int BOARD_LEFT = LEFT_SPACE_WIDTH + MARGIN;
const int BOARD_TOP = TITLE_HEIGHT + MARGIN;
// 选项对话框里的内容, 最开始的时候只需记简单的信息, 现在要记得东西多了, 已经有点不适用.
struct Options
{
int delay; // 延迟, 即播放速度
int numbers; // 最后的多少手棋显示手数
bool splitPeriodically; // 是否分割
int splitCount; // 分割点的个数
int splitPoints[20]; // 分割点
int cw; // cell width, 棋子尺寸
// 是否真的要分割图片
bool RealSplit()
{
return splitPeriodically || splitCount > 0;
}
// 延迟转为字符串
string GetDelayString()
{
char buf[32];
sprintf(buf, "%d", delay);
return buf;
}
void SetDelayString(const string & s)
{
delay = 50;
delay = atoi(s.c_str());
if(delay <0)
{
delay = 0;
}
}
string GetNumbersString()
{
char buf[32];
sprintf(buf, "%d", numbers);
return buf;
}
void SetNumbersString(const string & s)
{
numbers = 1;
numbers = atoi(s.c_str());
}
string GetSplitString()
{
if(splitPeriodically)
{
char buf[32];
sprintf(buf, "*%d", splitPoints[0]);
return buf;
}
else
{
string ret;
for(int i=0; i<splitCount; i++)
{
char buf[32];
sprintf(buf, "%d ", splitPoints[i]);
ret+= buf;
}
if(ret.length() > 0)
{
// ret.pop_back(); pop_back unavailable before c++11 standard
ret.resize(ret.length() - 1);
}
return ret;
}
}
void SetSplitString(const string & s)
{
if(s.empty())
{
splitPeriodically = false;
splitCount = 0;
return;
}
if(s[0] == '*')
{
splitPeriodically = true;
splitCount = 1;
splitPoints[0] = 50;
if(s.length() > 1)
{
splitPoints[0] = atoi(&s[1]);
if(splitPoints[0] < 5)
{
splitPoints[0] = 5;
}
}
}
else
{
splitPeriodically = false;
splitCount = 0;
string tmp;
int n = 0;
for(int i=0; i<s.length() && splitCount<20;)
{
tmp.clear();
while(i<s.length() && (s[i] <= 32 || s[i] ==',' || s[i] == '|'))
{
i++;
}
while(i<s.length() && s[i] > 32 && s[i] != ',' && s[i] !='|' )
{
tmp.push_back(s[i]);
i++;
}
int n1 = atoi(tmp.c_str());
if(n1 > n + 5)
{
n = n1;
splitPoints[splitCount] = n;
splitCount++;
}
}
}
}
string GetCWString()
{
char buf[32];
sprintf(buf, "%d", cw);
return buf;
}
void SetCWString(const string & s)
{
cw = 23;
cw = atoi(s.c_str());
if(cw < 15)
{
cw = 15;
}
else if(cw > 50)
{
cw = 50;
}
}
} g_options;
int CW() // cell width (pixels)
{
return g_options.cw;
}
int BW(int BS) // board width (pixels)
{
return BOARD_LEFT + CW() * BS + MARGIN + 16;
}
int BH(int BS) // board height (pixels)
{
return BOARD_TOP + CW() * BS + MARGIN + 16;
}
enum StoneColor
{
None = 0,
Black = 1,
White = 2,
};
// 棋盘上的交叉点
struct Cross
{
char x;
char y;
};
struct Move : public Cross
{
bool addition; // 是否座子
StoneColor color;
Move()
{
color = None;
addition = false;
x = 0;
y = 0;
}
};
struct Game
{
// 棋谱类型, 参见SFG官网的定义, GM[x]. 我们只支持五子棋和围棋
enum GameType
{
gameUnkown = 0,
gameGo = 1,
gameRenju = 4
}gameType;
int ca; // encoding
int boardSize;
StoneColor winner;
bool winByResign; // 中盘胜, +R
wstring gameTitle;
wstring komi; // 贴目, 用处不大
wstring result; // 对弈结果, 文本
wstring whiteName;
wstring whiteRank;
wstring blackName;
wstring blackRank;
int moveCount; // 当前的步数
Move moves[MAX_MOVE];
Game()
{
ca = 0;
winner = None;
winByResign = false;
moveCount = 0;
boardSize = 19;
#ifdef S2F_RENJU_ONLY
gameType = gameRenju;
#else
gameType = gameGo;
#endif
}
wstring GetTitle()
{
wstring ret;
if (!gameTitle.empty()) {
return gameTitle;
}
if(!whiteName.empty() || !blackName.empty())
{
ret += whiteName.empty() ? STR_WHITE L": " STR_UNKOWN : STR_WHITE L": " + whiteName;
ret += whiteRank.empty() ? L"" : L" " + whiteRank + L"";
ret += STR_VS;
ret += blackName.empty() ? STR_BLACK L": " STR_UNKOWN : STR_BLACK L": " + blackName;;
ret += blackRank.empty() ? L"" : L" " + blackRank + L"";
//ret += L" ";
}
if(winByResign)
{
if(winner == White)
{
ret+= STR_WHITE STR_WIN_BY_RESIGN;
}
else if(winner == Black)
{
ret+= STR_BLACK STR_WIN_BY_RESIGN;
}
}
else
{
if(winner == White)
{
ret+= STR_WHITE STR_WIN;
}
else if(winner == Black)
{
ret+= STR_BLACK STR_WIN;
}
if(!result.empty())
{
ret+= result;
}
}
//if(!komi.empty())
//{
// ret+= " 贴目: " + komi;
//}
return ret;
}
int GetRealMoveCount()
{
int n;
for(n = 0; n < moveCount && moves[n].addition; n++)
{}
return moveCount - n;
}
wstring ReadStringValue(const string & src, int & i);
};
string RemoveExtension(const string & src)
{
if(src.length() > 4 && src[src.length() - 4] == '.')
{
return src.substr(0, src.length() - 4);
}
else
{
return src;
}
}
string ReadFileIntoString(const string & srcPath)
{
string ret;
FILE * file = fopen(srcPath.c_str(), "r");
if(!file)
{
return ret;
}
while(!feof(file))
{
char c = fgetc(file);
ret.push_back(c);
}
fclose(file);
return ret;
}
// 解释落子坐标, SGF里用的是字母
bool ReadMove(const string & src, int & i, Move & m)
{
while(i < src.size() && src[i] <= 32)
{
i++;
}
if(i >= src.size() || src[i] != '[')
{
return false;
}
i++;
if (src[i] == ']') {
m.x = -1;
m.y = -1;
i++;
return true;
}
if(i < src.size() && src[i] >= 'a' && src[i] <= 'z')
{
m.x = src[i] - 'a';
i++;
if(i < src.size() && src[i] >= 'a' && src[i] <= 'z')
{
m.y = src[i] - 'a';
while(i < src.size() && src[i] != ']')
{
i++;
}
i++;
return true;
}
}
return false;
}
// 跳过未知的值
bool SkipValue(const string & src, int & i)
{
while(i < src.size() && src[i] <= 32)
{
i++;
}
if(i >= src.size() || src[i] != '[')
{
return false;
}
i++;
while(i < src.size() && src[i] != ']')
{
i++;
}
if (src[i] == ']') {
i++;
return true;
}
return false;
}
wstring mbs_to_ws(UINT cp, const char * p)
{
wstring dst;
int i = MultiByteToWideChar(cp, 0, p, -1, NULL, 0);
dst.resize(i + 1);
MultiByteToWideChar(cp, 0, p, -1, (wchar_t*)dst.data(), i);
return dst;
}
wstring mbs_to_ws(UINT cp, const string& str)
{
return mbs_to_ws(cp, str.c_str());
}
// 读字符串, 参见SGF官方定义
wstring Game::ReadStringValue(const string & src, int & i)
{
string ret;
while(i < src.size() && src[i] <= 32)
{
i++;
}
if(i < src.size() && src[i] == '[')
{
i++;
while(i < src.size() && src[i] != ']')
{
if(src[i] == '\\')
{
i++;
if(i < src.size())
{
ret.push_back(src[i]);
}
}
else
{
ret.push_back(src[i]);
/*if(src[i] < 0 && i + 1 < src.size())
{
i++;
ret.push_back(src[i]);
}*/
}
i++;
}
i++;
}
return mbs_to_ws(this->ca, ret);
}
// 解释SGF格式.
// (为啥不用第三方库? 因为我不会用.)
bool LoadSimpleSGF(Game & game, const string & srcPath)
{
bool bReadSize = false;
// 暂时把座子也算在Move里
string src = ReadFileIntoString(srcPath);
if(src.empty())
{
return false;
}
int i = 0;
// 跳过 (;
while(i < src.size() && ( src[i] <= 32 || src[i] == '(' || src[i] == ';'))
{
i++;
}
// 依次读标志, 直到遇到右括号
while(i < src.size() && src[i] != ')')
{
#ifdef _DEBUG
const char * tmp = &src[i];
#endif
while(i < src.size() && (src[i] <= 32 || src[i] == ';' || src[i] == '('))
{
i++;
}
if (src[i] == ')')
{
break;
}
string propName;
while(i < src.size() && src[i] >= 'A' && src[i] <= 'Z')
{
propName.push_back(src[i++]);
}
if(propName == "")
{
return false;
}
#ifdef _DEBUG
printf("debug: read prop: %s @%d\n", propName.c_str(), tmp - &src[0]);
#endif
if(propName == "GM")
{
// 棋种, 棋谱类型
wstring s = game.ReadStringValue(src, i);
int t = s.empty() ? 1 : _wtoi(s.c_str());
if(t == 4)
{
game.gameType = Game::gameRenju;
if(!bReadSize)
{
game.boardSize = 15;
}
printf("Game type is GM[%d] Renju.\n", t);
}
else
{
#ifdef S2F_RENJU_ONLY
printf("Unsupported game type : %d, treat as Renju.\n", t);
game.gameType = Game::gameRenju;
if(!bReadSize)
{
game.boardSize = 15;
}
#else
if(t != 1)
{
printf("Unsupported game type : %d, treat as Weiqi.\n", t);
}
else
{
printf("Game type is GM[%d] Weiqi.\n", t);
}
game.gameType = Game::gameGo;
if(!bReadSize)
{
game.boardSize = 19;
}
#endif
}
}
else if(propName == "AB" || propName == "AW" || propName == "B" || propName == "W")
{
// 座子/落子坐标
Move m;
m.color = propName == "AB" || propName == "B" ? Black : White;
m.addition = propName == "AB" || propName == "AW" ? true : false;
while(ReadMove(src, i, m))
{
game.moves[game.moveCount++] = m;
}
}
else if(propName == "SZ")
{
// 棋盘大小. 围棋一般是19/13/9, 五子棋15. 但也有例外
bReadSize = true;
wstring s = game.ReadStringValue(src, i);
if(!s.empty())
{
game.boardSize = _wtoi(s.c_str());
}
if(game.boardSize < 3 || game.boardSize > MAX_BOARD_SIZE)
{
game.boardSize = game.gameType == Game::gameRenju ? 15 : 19;
}
}
else if(propName == "PB")
{
// 黑方棋手
game.blackName = game.ReadStringValue(src, i);
}
else if(propName == "PW")
{
// 白方棋手
game.whiteName = game.ReadStringValue(src, i);
}
else if(propName == "BR")
{
// 黑方段位
game.blackRank = game.ReadStringValue(src, i);
}
else if(propName == "WR")
{
// 白方段位
game.whiteRank = game.ReadStringValue(src, i);
}
else if(propName == "KM")
{
// 贴目
game.komi = game.ReadStringValue(src, i);
}
else if(propName == "RE")
{
// 对局结果
// [B+R] 黑中盘胜
// [W+R] 白中盘胜
// [B+0.5] 黑胜0.5目
// [W+100] 白胜100目
// [其他] 其他结果, 例如"打挂", "无胜负"等
wstring re = game.ReadStringValue(src, i);
if(re.size() >= 2 && re[1] == '+')
{
if(re[0] == 'B' || re[0] == 'b')
{
game.winner = Black;
}
else if(re[0] == 'W' || re[0] == 'w')
{
game.winner = White;
}
if(re.size() >= 3 )
{
if(re[2] == 'R' || re[2] == 'r')
{
game.winByResign = true;
}
game.result = re.substr(2, -1);
}
else
{
game.result = re;
}
}
else
{
game.result = re;
}
}
else if(propName == "CA")
{
// 按官网的定义, CA属性内容是RFC1345里定义的编码页, 但RFC1345全文没有""UTF"相关的内容.
// 估计实际的软件并不完全遵守这个规则, 我们手头又没有足够的资料, 有点抓瞎.
// 目前我们采用模糊匹配的方法来猜测, 如果检测不出就默认为操作系统的编码页.
wstring re = game.ReadStringValue(src, i);
if (StrStrIW(re.c_str(), L"utf") != NULL && StrStrIW(re.c_str(), L"8") != NULL) {
// UTF-8
game.ca = CP_UTF8;
} else if (StrStrIW(re.c_str(), L"gb") != NULL || StrStrIW(re.c_str(), L"936") != NULL) {
// GBK
game.ca = 936;
} else if ((StrStrIW(re.c_str(), L"big") != NULL && StrStrIW(re.c_str(), L"5") != NULL) || StrStrIW(re.c_str(), L"950") != NULL) {
// BIG5
game.ca = 950;
} else if ((StrStrIW(re.c_str(), L"shift") != NULL && StrStrIW(re.c_str(), L"jis") != NULL) || StrStrIW(re.c_str(), L"932") != NULL) {
// SHIFT_JIS
game.ca = 932;
} else if ((StrStrIW(re.c_str(), L"euc") != NULL && StrStrIW(re.c_str(), L"jp") != NULL) || StrStrIW(re.c_str(), L"51932") != NULL) {
// EUC_JP
game.ca = 51932;
} else if ((StrStrIW(re.c_str(), L"euc") != NULL && StrStrIW(re.c_str(), L"kr") != NULL) || StrStrIW(re.c_str(), L"51949") != NULL) {
// EUC_KR
game.ca = 51949;
} else {
printf("unkown CA prop, treat as locale MBS\n");
game.ca = CP_ACP;
}
}
else if(propName == "GN")
{
// 棋谱名称?
game.gameTitle = game.ReadStringValue(src, i);
}
else if(propName == "C")
{
// 注解, 注释, 备注
game.ReadStringValue(src, i);
printf("ignore comment at move %d\n", game.moveCount);
}
else
{
// 其他属性直接忽略, 例如三角符号, A,B,C,D等分支标识等, 这些东西太费事了.
printf("Ignore property : %s\n", propName.c_str());
while(SkipValue(src, i))
{
// do nothing
}
}
}
return true;
}
// 种子填充算法, 数气, 此函数不是特别慢, 没必要优化
bool HasLiberty(int BS, StoneColor board[MAX_BOARD_SIZE][MAX_BOARD_SIZE], bool flags[MAX_BOARD_SIZE][MAX_BOARD_SIZE], int x, int y)
{
flags[x][y] = true;
if(x <(BS-1))
{
if( board[x][y] == board[x+1][y] && !flags[x+1][y])
{
if(HasLiberty(BS, board, flags, x + 1, y))
{
return true;
}
}
else if(board[x+1][y] == None)
{
return true;
}
}
if(x >0)
{
if( board[x][y] == board[x-1][y] && !flags[x-1][y])
{
if( HasLiberty(BS, board, flags, x - 1, y))
{
return true;
}
}
else if(board[x-1][y] == None)
{
return true;
}
}
if(y <(BS-1))
{
if( board[x][y] == board[x][y+1] && !flags[x][y+1])
{
if( HasLiberty(BS, board, flags, x, y+1))
{
return true;
}
}
else if(board[x][y+1] == None)
{
return true;
}
}
if(y > 0)
{
if( board[x][y] == board[x][y-1] && !flags[x][y-1])
{
if( HasLiberty(BS, board, flags, x, y-1))
{
return true;
}
}
else if(board[x][y-1] == None)
{
return true;
}
}
return false;
}
// 检测是否需要提子
void CheckTakeStone(int BS, StoneColor board[MAX_BOARD_SIZE][MAX_BOARD_SIZE], int x, int y)
{
bool flags[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
memset(flags, 0, sizeof(flags));
StoneColor c = board[x][y];
if(!HasLiberty(BS, board, flags, x, y))
{
for(int i=0; i<BS; i++)
{
for(int j=0; j<BS; j++)
{
if(flags[i][j])
{
assert(board[i][j] == c); // 原本的颜色一定和检测点的相同, 因为是同一块棋
board[i][j] = None; // 提子
}
}
}
}
}
// 五子棋落子函数, 不需要提子
bool PutStoneRenju(int BS, StoneColor board[MAX_BOARD_SIZE][MAX_BOARD_SIZE], const Move & m)
{
if(m.color == None)
{
return false;
}
if(m.x < 0 || m.y < 0|| m.x >= BS || m.y >= BS)
{
printf("Coord is out of board, treat as pass.\n");
return false;
}
board[m.x][m.y] = m.color;
return true;
}
// 围棋落子函数, 需要检测提子
bool PutStoneWeiqi(int BS, StoneColor board[MAX_BOARD_SIZE][MAX_BOARD_SIZE], const Move & m)
{
if(m.color == None)
{
return false;
}
if(m.x < 0 || m.y < 0|| m.x >= BS || m.y >= BS)
{
printf("Coord is out of board, treat as pass.\n");
return false;
}
board[m.x][m.y] = m.color;
// 四个方向的棋都需要检测是否气尽
if(m.x < (BS-1) && m.color != board[m.x+1][m.y])
{
CheckTakeStone(BS, board, m.x + 1, m.y);
}
if(m.x > 0 && m.color != board[m.x-1][m.y])
{
CheckTakeStone(BS, board, m.x - 1, m.y);
}
if(m.y < (BS-1) && m.color != board[m.x][m.y+1])
{
CheckTakeStone(BS,board, m.x, m.y+1);
}
if(m.y > 0 && m.color != board[m.x][m.y-1])
{
CheckTakeStone(BS,board, m.x, m.y-1);
}
// 按目前的规则似乎不需要检测自提
return true;
}
bool PutStone(Game::GameType gt, int BS, StoneColor board[MAX_BOARD_SIZE][MAX_BOARD_SIZE], const Move & m)
{
if(gt == Game::gameGo)
{
return PutStoneWeiqi(BS, board, m);
}
else
{
return PutStoneRenju(BS, board, m);
}
}
static HFONT CreateNativeFont(const string & _fontName, int _fontSize, bool _bold)
{
if(_fontSize < 1)
{
_fontSize = 1;
}
LOGFONT lf;
memset(&lf, 0, sizeof(lf));
lf.lfHeight = -_fontSize;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
if(_bold)
{
lf.lfWeight = FW_BOLD;
}
else
{
lf.lfWeight = FW_NORMAL;
}
//lf.lfItalic = _font.italic ? TRUE : FALSE;
//lf.lfUnderline = _font.underline ? TRUE : FALSE;
//lf.lfStrikeOut = _font.strikethrough ? TRUE : FALSE;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH;
if(_fontName.length() > 32)
{
# ifdef UNICODE
wcscpy(lf.lfFaceName, L"SYSTEM");
# else
strcpy(lf.lfFaceName, "SYSTEM");
# endif
}
else
{
# ifdef UNICODE
wcscpy(lf.lfFaceName, _fontName.c_str());
# else
strcpy(lf.lfFaceName, _fontName.c_str());
# endif
}
return ::CreateFontIndirect(&lf);
}
bool IsStar(int bs, int x, int y)
{
switch(bs)
{
case 9:
return (x == 2 || x == 6) && (y == 2 || y == 6);
case 13:
return ((x == 3 || x == 9) && (y == 3 || y == 9)) || (x == 6 && y == 6);
case 19:
return (x == 3 || x == 9 || x == 15) && (y == 3 || y == 9 || y == 15);
default:
return false;
}
}
// 画格子, 有棋子画棋子, 没有棋子画空交叉点.
// 注意图片是不透明的, 原先有棋子的地方被空交叉点覆盖就是提子的效果
void DrawCell(HDC hDC, int BS, StoneColor color, int i, int j, int x0, int y0, int num = 0)
{
Graphics * g = Graphics::FromHDC(hDC);
Status status = g->SetSmoothingMode( Gdiplus::SmoothingModeAntiAlias );
g->SetPageUnit( Gdiplus::UnitPixel );
if(color == White || color == Black)
{
// 画棋子
if(color == White)
{
::SelectObject(hDC, ::GetStockObject(WHITE_BRUSH));
::SetTextColor(hDC, RGB(0, 0, 0));
SolidBrush brush(Color::White);
g->FillEllipse(&brush, x0+1 , y0+1 , CW()-2, CW()-2);
}