-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelloworld.cpp
executable file
·1165 lines (1038 loc) · 36.6 KB
/
helloworld.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
/*
##### # # # # ##### Pocket GnuGo
# # ## # # # # # #### for
# # # # # # # # # Pocket PC
# #### # # # # # # #### # # and Windows CE
# # # # # # # # # # # ---------
# # # ## # # # # # # Version
##### # # ##### ##### #### 2.6.2
(c) Ivan Davtchev ([email protected]), Alexander K. Seewald ([email protected])
Based on Pocket GNU Go 1.2.0 by Ivan Datchev and 2.6.0 by Alex Seewald
Includes code from:
GNUGO - the game of Go (Wei-Chi) Version 2.6
Copyright (C) Free Software Foundation, Inc.
written by Man Li,Daniel Bump, David Denholm, Gunnar Farneback, Nils Lohner,
Jerome Dumonteil, Tommy Thorn, Nicklas Ekstrand, Inge Wallin, Thomas Traber,
Douglas Ridgway, Teun Burgers, Tanguy Urvoy and Thien-Thi Nguyen.
*** IMPORTANT: ***
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation - version 2.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Please report any bug/fix, modification, suggestion to
*/
#include "stdafx.h"
#include "Helloworld.h"
#include <commctrl.h>
#include <aygshell.h>
#include <sipapi.h>
#include "liberty.h"
#include "interface.h"
#include "ttsgf.h"
#include "sgfana.h"
#define MAX_LOADSTRING 100
#define STATUS_LEN 45
// board/stones related
int STONE_SIZE=10;
int CELL_SIZE=12;
// should be uppercase
int half_stone=5;
int half_cell=6;
int cell_plus=13;
int BOARD_SIZE=19;
// fuseki vars
int corner_done=0;
int fuseki_ended=0;
int firstmove = 1;
// board's screen boundaries
#if _WIN32_WCE >= 300
#define TOP_EDGE 12
#define LEFT_EDGE 12
#define BOTTOM_EDGE 220
#define RIGHT_EDGE 228
#else
#define TOP_EDGE 12+MENU_HEIGHT
#define LEFT_EDGE 12
#define BOTTOM_EDGE 220+MENU_HEIGHT
#define RIGHT_EDGE 228
#endif
// game states
#define PLAYING 1
#define END_GAME 0
#define SCORING 2
// game types
#define HUMAN_HUMAN 0
#define HUMAN_CPU 1
#define HUMAN_IRDA 2
#define NODES BOARD_SIZE*BOARD_SIZE
#define ENDLIST 1000
#define QSIZE 150
#define MAXTRY 400
#define line(x) (abs(x - 9))
#define CPU_CAPTURED (cpu_color== WHITE ? black_captured : white_captured)
#define HUMAN_CAPTURED (human_color== WHITE ? black_captured : white_captured)
#if _WIN32_WCE >= 300
#define SHGetSubMenu(hWndMB,ID_MENU) (HMENU)SendMessage((hWndMB), SHCMBM_GETSUBMENU, (WPARAM)0, (LPARAM)ID_MENU);
#endif
// Global Variables:
HINSTANCE hInst; // The current instance
HWND hwndCB; // The command bar handle
HWND hWnd; // The main and only window
HDC hdc; // Current screen "device"
// Debug Mode Flags & Vars!
TCHAR szDebugText[30] = TEXT("");
//#define DEBUG001 1
int pass = 0; /* two passes and its over */
int play=PLAYING;
SGFNodeP curnode = sgf_root;
float memory = MEMORY;
int DEPTH=12;
int BACKFILL_DEPTH=7;
int FOURLIB_DEPTH=4;
int KO_DEPTH=6;
int bThinking = FALSE; // boolean flag, is GNU go thinking
HANDLE hThinking; // event handle for the think thread
HANDLE hThinkThread; // handle to thinking thread itself
int gameType = HUMAN_CPU; // by default it is human vs PPC
TCHAR szCurrentText[STATUS_LEN] = TEXT(""); // the current state info on the screen
int human_color = BLACK;
int cpu_color = WHITE;
int lastI, lastJ, lastX, lastY; // coords of last stone placed
TCHAR szScore[] = TEXT("Score:");
TCHAR szCPUCaptured[] = TEXT("P/PC captured ");
TCHAR szHumanCaptured[] = TEXT("you captured ");
TCHAR szWhiteCaptured[] = TEXT("W captured ");
TCHAR szBlackCaptured[] = TEXT("B captured ");
TCHAR szPass[] = TEXT("pass");
TCHAR szUndo[] = TEXT("UNDO");
TCHAR szFormatA[] = TEXT("%s");
TCHAR szFormatB[] = TEXT("%d");
TCHAR szFormatNorm[] = TEXT("(%C-%d) %s%d; %s%d");
TCHAR szFormatPass[] = TEXT("(%s) %s%d, %s%d");
TCHAR szFormatScore[] = TEXT("%s %s%.1f, %s%.1f (k=%.1f,H=%d)");
TCHAR szScoring[] = TEXT("Everything ok? If yes: Edit>Pass");
TCHAR szWelcome[] = TEXT("Welcome to Pocket GNU Go!");
TCHAR szGnuGoThinks[4][40] = { TEXT("GNU Go thinks..."), TEXT("GNU Go contemplates your move..."), TEXT("GNU Go evaluates its position..."), TEXT("GNU Go is deep in thought...") };
TCHAR szCongrats[] = TEXT("Congrats!");
TCHAR szIWin[] = TEXT("I won!");
TCHAR szTie[] = TEXT("It's a tie!");
TCHAR szBlack[] = TEXT("B:");
TCHAR szWhite[] = TEXT("W:");
HGDIOBJ oldBrush, bbrush, wbrush, gbrush, cbrush, deadWhiteBrush, deadBlackBrush; // various brushes used for drawing
HPEN oldPen, redPen; // pens for drawing
COLORREF rgbBoard = 0x0066CCFF; // color for board
COLORREF rgbCrimson = 0x00010195; // color for last piece so far
COLORREF rgbDeadWhite = 0x00c0c0c0;
COLORREF rgbDeadBlack = 0x00808080;
HMENU myMenu; // for graying out items etc
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass (HINSTANCE, LPTSTR);
BOOL InitInstance (HINSTANCE, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK NewBox (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ExplainBox (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK HelpBox (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK OptionsBox (HWND, UINT, WPARAM, LPARAM);
HWND CreateRpCommandBar(HWND);
static int fval(int newlib, int minlib);
// transform from screen coordinates to board position
// 0,0 is valid origin in both
BOOL ScreenToBoard (int x, // screen x
int y, // screen y
int *i, // board x
int *j) // board y
{
x-=LEFT_EDGE; x+=half_cell;
y-=TOP_EDGE; y+=half_cell;
if (x < 0 || y < 0) return FALSE;
int rx, ry;
rx = x / CELL_SIZE;
ry = y / CELL_SIZE;
if (rx<0||rx>=BOARD_SIZE||ry<0||ry>=BOARD_SIZE) {
return FALSE;
} else {
*i=rx; *j=ry;
return TRUE;
}
}
BOOL ValidMove(int i, int j) { // check the validity of a move
if (!legal(i,j,get_tomove()))
return FALSE;
return TRUE;
}
void ComputerMove(void) { // generate computer move
if (play == PLAYING) {
wsprintf(szCurrentText, szFormatA, szGnuGoThinks[Random() % 4]);
InvalidateRect(hWnd, NULL, TRUE); // refresh
SendMessage(hWnd, WM_PAINT, NULL, NULL);
if (get_move(&lastI, &lastJ,cpu_color)!=0) // no pass
{
pass = 0; // clear passes
curnode = sgfAddPlay(curnode, cpu_color, lastI, lastJ);
// generate status string
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatNorm, (CHAR)(lastI+'A'), BOARD_SIZE-lastJ,
szCPUCaptured, CPU_CAPTURED, szHumanCaptured, HUMAN_CAPTURED);
} else {
wsprintf(szCurrentText, szFormatPass, szPass,
szBlackCaptured, black_captured, szWhiteCaptured, white_captured);
}
switch_tomove();
} else {
pass++;
curnode = sgfAddPlay(curnode, cpu_color, BOARD_SIZE, BOARD_SIZE);
play = (pass>1)? END_GAME : PLAYING;
lastI = lastJ = lastX = lastY = -1;
// generate status string
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatPass, szPass,
szCPUCaptured, CPU_CAPTURED, szHumanCaptured, HUMAN_CAPTURED);
} else {
wsprintf(szCurrentText, szFormatPass, szPass,
szBlackCaptured, black_captured, szWhiteCaptured, white_captured);
}
switch_tomove();
}
if (play == END_GAME) {
// temporarily disable Pass on Edit menu
EnableMenuItem(myMenu, IDM_EDIT_PASS, MF_GRAYED | MF_BYCOMMAND);
// disable Undo until New game
EnableMenuItem(myMenu, IDM_EDIT_UNDO, MF_GRAYED | MF_BYCOMMAND);
DialogBox(hInst, (LPCTSTR)IDD_EXPLAIN, hWnd, (DLGPROC)ExplainBox);
} else {
// restore the "Pass" item on the Edit menu
EnableMenuItem(myMenu, IDM_EDIT_PASS, MF_ENABLED | MF_BYCOMMAND);
}
InvalidateRect(hWnd, NULL, TRUE); // refresh
}
}
void SetupGo(float komi) { // set up variables etc at the beginning
// Initialize Go variables
init_board();
init_ginfo();
init_gopt();
set_boardsize(BOARD_SIZE);
set_computer_player(cpu_color);
set_komi(komi);
// init hash memory
float nodes;
if (!movehash) {
nodes = (float)( (memory * 1024 * 1024) / (sizeof(Hashnode) + sizeof(Read_result) * 1.4));
movehash = hashtable_new((int) (1.5 * nodes), (int)(nodes), (int)(nodes * 1.4) );
}
hash_init();
/* clear some caches */
clear_wind_cache();
clear_safe_move_cache();
sgfCreateHeaderNode(get_komi());
curnode = sgf_root;
corner_done=0;
fuseki_ended=0;
firstmove = 1;
play = PLAYING;
pass = 0;
// clear the status string
wsprintf(szCurrentText, szFormatA, szWelcome);
lastI=lastJ=lastX=lastY = -1;
}
void CountFinalScore(void) {
int black,white;
int CPUWins;
float komi;
float black_score, white_score;
komi=get_komi();
pushgo();
evaluate_territory(&white,&black);
black_score=black+white_captured-komi;
white_score=white+black_captured+0.0f;
popgo();
if (human_color==BLACK) {
CPUWins=(white_score>black_score);
} else {
CPUWins=(black_score>white_score);
}
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatScore, (CPUWins ? szIWin : (white_score==black_score ? szTie : szCongrats)),
szBlack, black_score, szWhite, white_score, komi, get_handicap());
} else {
wsprintf(szCurrentText, szFormatScore, szScore,
szBlack, black_score, szWhite, white_score, komi, get_handicap());
}
// disable (gray) the Undo command, although it should already be disabled..
EnableMenuItem(myMenu, IDM_EDIT_UNDO, MF_GRAYED | MF_BYCOMMAND);
}
DWORD WINAPI ThinkThread (PVOID pvoid) {
// entry point to the thread that does the thinking
// bThinking is a global boolean flag that it (re)sets
while (TRUE) {
// wait for thinking signal
WaitForSingleObject (hThinking, INFINITE); // resets automatically
bThinking = TRUE;
// and now generate move
if (gameType == HUMAN_CPU) { // if against PC
ComputerMove();
} else { // assume against human
InvalidateRect(hWnd, NULL, TRUE); // refresh
SendMessage(hWnd, WM_PAINT, NULL, NULL);
int temp = human_color;
human_color = cpu_color;
cpu_color = temp;
}
// signal end of thinking
bThinking = FALSE;
}
return TRUE;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
HACCEL hAccelTable;
DWORD ThreadID;
void *p;
// use this memory for hashtable: maximum available MB - 3
// sorry for the hack, but GlobalMemoryStatus does not seem to work
// on my device
memory = 64; // towards the Compaq iPaq w/ 64MB ;-)
while ((p=malloc((int)memory * 1024 * 1024)) == NULL)
memory--;
free(p);
memory/=2;
SetupGo(5.5f); // go setup
sgf_write_game_info(get_boardsize(), get_handicap(),get_komi(), get_seed(), "WinCE");
// Initialize brushes etc
bbrush = GetStockObject(BLACK_BRUSH);
wbrush = GetStockObject(WHITE_BRUSH);
gbrush = GetStockObject(LTGRAY_BRUSH);
cbrush = CreateSolidBrush(rgbBoard);
deadWhiteBrush = CreateSolidBrush(rgbDeadWhite);
deadBlackBrush = CreateSolidBrush(rgbDeadBlack);
redPen = CreatePen(PS_SOLID, 2, rgbCrimson);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_HELLOWORLD);
#ifdef DEBUG001
MessageBox(NULL, L"WinMain!", L"Debug", MB_OK);
#endif
// init event handle for thinking thread
hThinking = CreateEvent (NULL, FALSE, FALSE, NULL);
// nameless event, automatic reset, init state = non-signaled
#ifdef DEBUG001
if (hThinking)
MessageBox(NULL, L"CreateEvent OK!", L"Debug", MB_OK);
else
MessageBox(NULL, L"CreateEvent FAIL!", L"Debug", MB_OK);
#endif
// create non-suspended thinking thread here
hThinkThread = CreateThread(NULL, 0, ThinkThread, NULL, 0, &ThreadID);
#ifdef DEBUG001
if (hThinkThread)
MessageBox(NULL, L"CreateThread OK!", L"Debug", MB_OK);
else
MessageBox(NULL, L"CreateThread FAIL!", L"Debug", MB_OK);
#endif
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
if (bThinking && (msg.message == WM_LBUTTONDOWN)) {
// user clicked on board while thinking, ignore!
} else {
DispatchMessage(&msg);
}
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// It is important to call this function so that the application
// will get 'well formed' small icons associated with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_HELLOWORLD));
wc.hCursor = 0;
// wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.hbrBackground = NULL;
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hWnd = NULL;
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The window class name
hInst = hInstance; // Store instance handle in our global variable
// Initialize global strings
LoadString(hInstance, IDC_HELLOWORLD, szWindowClass, MAX_LOADSTRING);
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
// If it is already running, then focus on the window
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
SetForegroundWindow ((HWND) (((DWORD)hWnd) | 0x01));
return 0;
}
MyRegisterClass(hInstance, szWindowClass);
RECT rect;
GetClientRect(hWnd, &rect);
hWnd = CreateWindow(szWindowClass, szTitle, WS_CLIPCHILDREN | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
#if _WIN32_WCE >= 300
//When the main window is created using CW_USEDEFAULT the height of the menubar (if one
// is created is not taken into account). So we resize the window after creating it
// if a menubar is present
{
RECT rc;
GetWindowRect(hWnd, &rc);
rc.bottom -= MENU_HEIGHT;
if (hwndCB)
MoveWindow(hWnd, rc.left, rc.top, rc.right, rc.bottom, FALSE);
}
#endif
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
RECT rt;
int movenumber, next;
// ===========================================
// MESSAGE PROCESSING LOOP
// ===========================================
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EDIT_UNDO:
movenumber = get_movenumber();
movenumber--; // always two steps back, so human plays again
if ((movenumber > 0)&&(play==PLAYING)) {
black_captured = white_captured = 0;
curnode = sgf_root;
next = sgfPlayTree(&curnode, &movenumber);
movenumber--;
set_tomove(next);
set_movenumber(movenumber);
// update status text
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatPass, szUndo,
szCPUCaptured, CPU_CAPTURED, szHumanCaptured, HUMAN_CAPTURED);
} else {
wsprintf(szCurrentText, szFormatPass, szPass,
szBlackCaptured, black_captured, szWhiteCaptured, white_captured);
}
lastX = lastY = lastI = lastJ = -1;
InvalidateRect(hWnd, NULL, TRUE); // refresh
}
break;
case IDM_HELP_HELP:
// pop up help box
DialogBox(hInst, (LPCTSTR)IDD_HELP, hWnd, (DLGPROC)HelpBox);
break;
case IDM_EDIT_OPTIONS:
// pop up help box
DialogBox(hInst, (LPCTSTR)IDD_OPTIONS, hWnd, (DLGPROC)OptionsBox);
break;
case IDM_EDIT_PASS:
if (play == SCORING) {
play = END_GAME;
// disable Pass on Edit menu
EnableMenuItem(myMenu, IDM_EDIT_PASS, MF_GRAYED | MF_BYCOMMAND);
// and count the score!!!
CountFinalScore();
InvalidateRect(hWnd, NULL, TRUE); // refresh
}
if (play == PLAYING) {
pass++;
curnode = sgfAddPlay(curnode, human_color, BOARD_SIZE, BOARD_SIZE);
play = (pass>1)? END_GAME : PLAYING;
lastI = lastJ = -1;
// generate status text
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatPass, szPass,
szCPUCaptured, CPU_CAPTURED, szHumanCaptured, HUMAN_CAPTURED);
} else {
wsprintf(szCurrentText, szFormatPass, szPass,
szBlackCaptured, black_captured, szWhiteCaptured, white_captured);
}
if (gameType == HUMAN_CPU) {
// temporarily disable Pass on Edit menu
EnableMenuItem(myMenu, IDM_EDIT_PASS, MF_GRAYED | MF_BYCOMMAND);
}
if (play == END_GAME) {
// disable Undo for scoring
EnableMenuItem(myMenu, IDM_EDIT_UNDO, MF_GRAYED | MF_BYCOMMAND);
DialogBox(hInst, (LPCTSTR)IDD_EXPLAIN, hWnd, (DLGPROC)ExplainBox);
} else {
//ComputerMove(); // unused in m-threaded version
SetEvent(hThinking); // signal to thinking thread
}
}
break;
case IDM_HELP_ABOUT:
// pop up dialog box
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_FILE_NEW:
// pop up dialog box
DialogBox(hInst, (LPCTSTR)IDD_NEWGAME, hWnd, (DLGPROC)NewBox);
if ((play==PLAYING)&&(is_computer_player(get_tomove()))) {
//ComputerMove(); // unused in m-threaded version
SetEvent(hThinking); // signal to thinking thread
}
break;
case IDM_FILE_EXIT:
// exit command selected
SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
case IDOK:
SendMessage(hWnd, WM_ACTIVATE, MAKEWPARAM(WA_INACTIVE, 0), (LPARAM)hWnd);
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
// Pen control
case WM_LBUTTONDOWN:
lastX = LOWORD(lParam);
lastY = HIWORD(lParam);
if (ScreenToBoard(lastX, lastY, &lastI, &lastJ)) {
if ((play == PLAYING) && ValidMove(lastI, lastJ)) {
put_move(lastI,lastJ,human_color);
inc_movenumber();
pass = 0; // clear passes
curnode = sgfAddPlay(curnode, human_color, lastI, lastJ);
// generate status text
// update status text
if (gameType == HUMAN_CPU) {
wsprintf(szCurrentText, szFormatNorm, (CHAR)(lastI+'A'), BOARD_SIZE-lastJ,
szCPUCaptured, CPU_CAPTURED, szHumanCaptured, HUMAN_CAPTURED);
} else {
wsprintf(szCurrentText, szFormatNorm, (CHAR)(lastI+'A'), BOARD_SIZE-lastJ,
szBlackCaptured, black_captured, szWhiteCaptured, white_captured);
}
switch_tomove();
//ComputerMove(); // unused if multithreaded
SetEvent(hThinking); // signal to thinking thread
}
if (play == SCORING) {
// toggle status of strings...
change_dragon_status(lastI,lastJ,dragon[lastI][lastJ].status == DEAD ? ALIVE : DEAD);
InvalidateRect(hWnd, NULL, TRUE);
}
} else {
// indicate no successful move; to be used by UNDO
lastX = lastY = lastI = lastJ = -1;
}
break;
case WM_CREATE:
hwndCB = CreateRpCommandBar(hWnd);
#if _WIN32_WCE >= 300
myMenu = (HMENU)SHGetSubMenu(hwndCB, IDM_EDIT);
#else
myMenu = GetSubMenu((HMENU)CommandBar_GetMenu(hwndCB, 0), 1);
#endif
break;
case WM_PAINT:
int i,j,distBorder, res;
HDC hdc2; HBITMAP hbm; HGDIOBJ oldObj;
hdc = BeginPaint(hWnd, &ps); // redraw flashes - maybe two hdcs & switch?
hdc2 = hdc; hdc = CreateCompatibleDC(hdc2);
GetClientRect(hWnd, &rt); // gets the rect size from the device
hbm=CreateCompatibleBitmap(hdc2,rt.right,rt.bottom);
oldObj = SelectObject(hdc,hbm);
// clear screen
oldBrush = SelectObject(hdc, wbrush);
Rectangle(hdc,0,0,rt.right-1,rt.bottom-1);
#if _WIN32_WCE < 300
rt.top+=MENU_HEIGHT;
rt.bottom = 255+MENU_HEIGHT; // set the bottom border by hand
#else
rt.bottom = 255;
#endif
// write status text
oldBrush = SelectObject(hdc, bbrush);
DrawText(hdc, szCurrentText, _tcslen(szCurrentText), &rt,
DT_BOTTOM | DT_CENTER);
// draw lines on the Go board
// maybe draw only lines (faster)?
// nice bitmap (scaled) as go board, same for stones..
// MUCH LATER!!
oldBrush = SelectObject(hdc, cbrush);
for (i = LEFT_EDGE; i < RIGHT_EDGE; i += CELL_SIZE) {
for (j = TOP_EDGE; j < BOTTOM_EDGE; j += CELL_SIZE) {
Rectangle(hdc, i, j, i+cell_plus, j+cell_plus);
}
}
// draw handicap dots
oldBrush = SelectObject(hdc, bbrush);
distBorder = ( BOARD_SIZE>9 ? 3 : 2);
// draw four border points
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*distBorder-2,TOP_EDGE+CELL_SIZE*distBorder-2,
LEFT_EDGE+CELL_SIZE*distBorder+3,TOP_EDGE+CELL_SIZE*distBorder+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,TOP_EDGE+CELL_SIZE*distBorder-2,
LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3,TOP_EDGE+CELL_SIZE*distBorder+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*distBorder-2,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,
LEFT_EDGE+CELL_SIZE*distBorder+3,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,
LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3);
// draw middle points for big board
if (BOARD_SIZE==19) {
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*9-2,TOP_EDGE+CELL_SIZE*distBorder-2,
LEFT_EDGE+CELL_SIZE*9+3,TOP_EDGE+CELL_SIZE*distBorder+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*9-2,TOP_EDGE+CELL_SIZE*9-2,
LEFT_EDGE+CELL_SIZE*9+3,TOP_EDGE+CELL_SIZE*9+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*9-2,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,
LEFT_EDGE+CELL_SIZE*9+3,TOP_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*distBorder-2,TOP_EDGE+CELL_SIZE*9-2,
LEFT_EDGE+CELL_SIZE*distBorder+3,TOP_EDGE+CELL_SIZE*9+3);
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)-2,TOP_EDGE+CELL_SIZE*9-2,
LEFT_EDGE+CELL_SIZE*(BOARD_SIZE-distBorder-1)+3,TOP_EDGE+CELL_SIZE*9+3);
}
// draw Go stones
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (p[i][j] != EMPTY) {
// if board position not empty
// select right color for brush
if (p[i][j] == BLACK) {
if ((play==SCORING)&&(dragon[i][j].status==DEAD))
oldBrush = SelectObject(hdc, deadBlackBrush);
else
oldBrush = SelectObject(hdc, bbrush);
} else {
if ((play==SCORING)&&(dragon[i][j].status==DEAD))
oldBrush = SelectObject(hdc, deadWhiteBrush);
else
oldBrush = SelectObject(hdc, wbrush);
}
// and draw the stone
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*i-half_stone, TOP_EDGE+CELL_SIZE*j-half_stone,
LEFT_EDGE+CELL_SIZE*i+half_stone, TOP_EDGE+CELL_SIZE*j+half_stone);
}
}
}
// draw last stone differently, so human player can see it better
if ((play == PLAYING) && (lastI >= 0)) {
// set up colors
oldPen = (HPEN)SelectObject(hdc, redPen);
if (p[lastI][lastJ] == BLACK) {
oldBrush = SelectObject(hdc, bbrush);
} else {
oldBrush = SelectObject(hdc, wbrush);
}
// draw stone
Ellipse(hdc, LEFT_EDGE+CELL_SIZE*lastI-half_stone, TOP_EDGE+CELL_SIZE*lastJ-half_stone,
LEFT_EDGE+CELL_SIZE*lastI+half_stone, TOP_EDGE+CELL_SIZE*lastJ+half_stone);
// restore black pen
SelectObject(hdc, oldPen);
}
GetClientRect(hWnd, &rt); // gets the rect size from the device, again
res=BitBlt(hdc2,0,0,rt.right,rt.bottom,hdc,0,0,SRCCOPY);
res=DeleteDC(hdc);
res=DeleteObject(hbm);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
free(movehash);
CommandBar_Destroy(hwndCB);
PostQuitMessage(0);
break;
case WM_SETTINGCHANGE:
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#if _WIN32_WCE >= 300
HWND CreateRpCommandBar(HWND hwnd)
{
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hwnd;
mbi.nToolBarId = IDM_MENU;
mbi.hInstRes = hInst;
mbi.nBmpId = 0;
mbi.cBmpImages = 0;
if (!SHCreateMenuBar(&mbi))
return NULL;
return mbi.hwndMB;
}
#else
HWND CreateRpCommandBar(HWND hwnd)
{
HWND hwndCB;
hwndCB = CommandBar_Create(hInst, hwnd, 1);
CommandBar_InsertMenubar (hwndCB, hInst, IDM_MENU, 0);
return hwndCB;
}
#endif
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
#if _WIN32_WCE >= 300
SHINITDLGINFO shidi;
#endif
switch (message)
{
case WM_INITDIALOG:
#if _WIN32_WCE >= 300
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
#endif
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// Mesage handler for the Help box.
LRESULT CALLBACK HelpBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
#if _WIN32_WCE >= 300
SHINITDLGINFO shidi;
#endif
switch (message)
{
case WM_INITDIALOG:
#if _WIN32_WCE >= 300
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
#endif
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// Mesage handler for the Explain box about scoring at the end of the game
LRESULT CALLBACK ExplainBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
#if _WIN32_WCE >= 300
SHINITDLGINFO shidi;
#endif
switch (message)
{
case WM_INITDIALOG:
#if _WIN32_WCE >= 300
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
#endif
// start SCORING mode
play = SCORING;
make_worms();
make_dragons();
make_moyo(get_computer_player());
// restore the "Pass" item on the Edit menu
EnableMenuItem(myMenu, IDM_EDIT_PASS, MF_ENABLED | MF_BYCOMMAND);
// change state message
wsprintf(szCurrentText, szFormatA, szScoring);
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK) {
InvalidateRect(hWnd, NULL, TRUE); // last refresh
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
// Message handler for the New Game box.
LRESULT CALLBACK NewBox(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
int i;
#if _WIN32_WCE >= 300
SHINITDLGINFO shidi;
#endif
TCHAR szNumber[10]; // For the items on the list
HWND hwndHandListCtrl; // Window handle to the handicap list box
HWND hwndSizeListCtrl; // Window handle to the board size list box
HWND hwndBlackButton; // Window handle to the black radio button box
HWND hwndAgainstCPU; // Window handle to the game type check box
int iCurrItem = -1; // Current item
int handicap, bs; // Handicap and board size
float komi_ = 5.5f;
switch (message)
{
case WM_INITDIALOG:
#if _WIN32_WCE >= 300
// Create a Done button and size it.
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = /*SHIDIF_DONEBUTTON |*/ SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
#endif
// -----------------------------
hwndHandListCtrl = GetDlgItem (hDlg, IDC_HANDICAP);
hwndSizeListCtrl = GetDlgItem (hDlg, IDC_BOARD_SIZE);
hwndBlackButton = GetDlgItem (hDlg, IDC_BLACK);
hwndAgainstCPU = GetDlgItem (hDlg, IDC_AGAINSTCPU);
SendMessage (hwndBlackButton, BM_SETCHECK, BST_CHECKED, 0); // check button
SendMessage (hwndAgainstCPU, BM_SETCHECK, BST_CHECKED, 0); // check button
for (i = 0; i < 10; i++) {
// place list items
wsprintf(szNumber, szFormatB, i);
// Add the number to the list control.
iCurrItem = SendMessage (hwndHandListCtrl, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR) szNumber);
// Set a 32-bit value, (LPARAM) i, that is associated
// with the newly added item in the list control.
SendMessage (hwndHandListCtrl, LB_SETITEMDATA,
(WPARAM) iCurrItem, (LPARAM) i);
}
// Select the first number in the list control.
SendMessage (hwndHandListCtrl, LB_SETCURSEL, 0, 0);
// Add list items & associate board sizes
wsprintf(szNumber,TEXT(" 9x9"));
iCurrItem = SendMessage (hwndSizeListCtrl, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR) szNumber);
SendMessage (hwndSizeListCtrl, LB_SETITEMDATA,
(WPARAM) iCurrItem, (LPARAM) 9);
wsprintf(szNumber,TEXT("13x13"));
iCurrItem = SendMessage (hwndSizeListCtrl, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR) szNumber);
SendMessage (hwndSizeListCtrl, LB_SETITEMDATA,
(WPARAM) iCurrItem, (LPARAM) 13);
wsprintf(szNumber,TEXT("19x19"));;
iCurrItem = SendMessage (hwndSizeListCtrl, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR) szNumber);
SendMessage (hwndSizeListCtrl, LB_SETITEMDATA,
(WPARAM) iCurrItem, (LPARAM) 19);
// Select the first number in the list control.
SendMessage (hwndSizeListCtrl, LB_SETCURSEL, 2, 0);
// -----------------------------
#if _WIN32_WCE >= 300
SHInitDialog(&shidi);
#endif
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK) {
// see what is selected and make new game
hwndHandListCtrl = GetDlgItem (hDlg, IDC_HANDICAP);
hwndSizeListCtrl = GetDlgItem (hDlg, IDC_BOARD_SIZE);
hwndBlackButton = GetDlgItem (hDlg, IDC_BLACK);
hwndAgainstCPU = GetDlgItem (hDlg, IDC_AGAINSTCPU);
// ask buttons for their state
if (BST_CHECKED == SendMessage(hwndAgainstCPU, BM_GETCHECK, 0, 0)) {
gameType = HUMAN_CPU;
} else {