-
Notifications
You must be signed in to change notification settings - Fork 0
/
doublectrl.c
616 lines (558 loc) · 17.7 KB
/
doublectrl.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
#include <windows.h>
#include <math.h>
#define BOARD_CELL_NUM 14
#define FIVE_MARK_POINT_RADIUS 4
#define CHESS_PIECE_RADIUS 13
#define BLACK_FLAG 1
#define WHITE_FLAG 2
#define NULL_FLAG 0
#define WIN_CONDITION 5
POINT cursorPos = {7, 7}; // 初始光标位置在棋盘中央
typedef struct
{
POINT pos;
int flag;
} ChessMove;
ChessMove moveHistory[(BOARD_CELL_NUM + 1) * (BOARD_CELL_NUM + 1)];
int moveCount = 0;
typedef enum Enum_Direction
{
Direction_Top = 0,
Direction_RightTop = 1,
Direction_Right = 2,
Direction_RightBottom = 3,
Direction_Bottom = 4,
Direction_LeftBottom = 5,
Direction_Left = 6,
Direction_LeftTop = 7
} Win_Direction;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("五子棋"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
610,
610,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// 绘制黑色实心点
HRESULT _DrawBlackSolidPoint(HDC hdc, int radius, POINT postion)
{
SelectObject(hdc, GetStockObject(BLACK_BRUSH));
Ellipse(hdc, postion.x - radius, postion.y - radius, postion.x + radius, postion.y + radius);
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
return S_OK;
}
// 绘制白色空心点
HRESULT _DrawWhiteHollowPoint(HDC hdc, int radius, POINT postion)
{
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
Ellipse(hdc, postion.x - radius, postion.y - radius, postion.x + radius, postion.y + radius);
return S_OK;
}
// 获取一小格宽度和高度
HRESULT _GetCellWidthAndHeight(POINT ptLeftTop, int cxClient, int cyClient, int *cxCell, int *cyCell)
{
*cxCell = (cxClient - ptLeftTop.x * 2) / BOARD_CELL_NUM;
*cyCell = (cyClient - ptLeftTop.y * 2) / BOARD_CELL_NUM;
return S_OK;
}
// 将实际坐标转化为逻辑坐标,这里需要进行实际点到棋盘点的转化
HRESULT _ExChangeLogicalPosition(POINT actualPostion, POINT ptLeftTop, int cxClient, int cyClient, POINT *logicalPostion)
{
// 获得一小格的宽度和高度
int cxCell = 0, cyCell = 0;
_GetCellWidthAndHeight(ptLeftTop, cxClient, cyClient, &cxCell, &cyCell);
// 检查点击有效性
if (actualPostion.x < ptLeftTop.x || actualPostion.x > ptLeftTop.x + BOARD_CELL_NUM * cxCell ||
actualPostion.y < ptLeftTop.y || actualPostion.y > ptLeftTop.y + BOARD_CELL_NUM * cyCell)
{
MessageBox(NULL, TEXT("请点击棋盘内下棋!"), TEXT("提示"), MB_OK);
return S_FALSE;
}
// 获取相邻四个点
int xCount = 0, yCount = 0;
POINT sidePoints[4] = {0};
for (int x = ptLeftTop.x; x <= ptLeftTop.x + BOARD_CELL_NUM * cxCell; x += cxCell, xCount++)
{
if (actualPostion.x >= x && actualPostion.x <= x + cxCell)
{
sidePoints[0].x = x;
sidePoints[2].x = x;
sidePoints[1].x = x + cxCell;
sidePoints[3].x = x + cxCell;
break;
}
}
for (int y = ptLeftTop.y; y <= ptLeftTop.y + BOARD_CELL_NUM * cyCell; y += cyCell, yCount++)
{
if (actualPostion.y >= y && actualPostion.y <= y + cyCell)
{
sidePoints[0].y = y;
sidePoints[1].y = y;
sidePoints[2].y = y + cyCell;
sidePoints[3].y = y + cyCell;
break;
}
}
// 计算当前点到四个点到当前点距离
double lengthCount[4] = {0};
for (int item = 0; item < 4; ++item)
{
lengthCount[item] = pow(abs(sidePoints[item].x - actualPostion.x), 2) + pow(abs(sidePoints[item].y - actualPostion.y), 2);
}
// 获取四个距离值中最短的一个
int shortestIndex = 0;
for (int item = 0; item < 4; ++item)
{
if (lengthCount[item] < lengthCount[shortestIndex])
{
shortestIndex = item;
}
}
// 计算逻辑坐标,其中下标为0的点为基准点
if (1 == shortestIndex)
{
xCount += 1;
}
else if (2 == shortestIndex)
{
yCount += 1;
}
else if (3 == shortestIndex)
{
xCount += 1;
yCount += 1;
}
logicalPostion->x = xCount;
logicalPostion->y = yCount;
return S_OK;
}
// 将逻辑坐标转化为实际坐标
HRESULT _ExchangeActualPositon(POINT logicalPos, int cxCell, int cyCell, POINT ptLeftTop, POINT *actualPos)
{
actualPos->x = ptLeftTop.x + logicalPos.x * cxCell;
actualPos->y = ptLeftTop.y + logicalPos.y * cyCell;
return S_OK;
}
// 绘制棋盘
HRESULT DrawChessBoard(HDC hdc, POINT ptLeftTop, int cxClient, int cyClient)
{
// 获得一小格的宽度和高度
int cxCell = 0, cyCell = 0;
_GetCellWidthAndHeight(ptLeftTop, cxClient, cyClient, &cxCell, &cyCell);
// 绘制竖线
for (int col = 0; col < BOARD_CELL_NUM + 1; ++col)
{
MoveToEx(hdc, ptLeftTop.x + col * cxCell, ptLeftTop.y, NULL);
LineTo(hdc, ptLeftTop.x + col * cxCell, ptLeftTop.y + BOARD_CELL_NUM * cyCell);
}
// 绘制灰色的横线
HPEN hPen, hOldPen;
hPen = CreatePen(PS_SOLID, 1, RGB(190, 190, 190));
hOldPen = SelectObject(hdc, hPen);
for (int row = 0; row < 7; ++row)
{
MoveToEx(hdc, ptLeftTop.x, ptLeftTop.y + cyCell + row * 2 * cyCell, NULL);
LineTo(hdc, ptLeftTop.x + BOARD_CELL_NUM * cxCell, ptLeftTop.y + cyCell + row * 2 * cyCell);
}
SelectObject(hdc, hOldPen);
// 绘制黑色的横线
for (int row = 0; row < 8; ++row)
{
MoveToEx(hdc, ptLeftTop.x, ptLeftTop.y + row * 2 * cyCell, NULL);
LineTo(hdc, ptLeftTop.x + BOARD_CELL_NUM * cxCell, ptLeftTop.y + row * 2 * cyCell);
}
// 绘制五个黑色实心点
_DrawBlackSolidPoint(hdc, FIVE_MARK_POINT_RADIUS, (POINT){ptLeftTop.x + 3 * cxCell, ptLeftTop.y + 3 * cyCell});
_DrawBlackSolidPoint(hdc, FIVE_MARK_POINT_RADIUS, (POINT){ptLeftTop.x + 11 * cxCell, ptLeftTop.y + 3 * cyCell});
_DrawBlackSolidPoint(hdc, FIVE_MARK_POINT_RADIUS, (POINT){ptLeftTop.x + 7 * cxCell, ptLeftTop.y + 7 * cyCell});
_DrawBlackSolidPoint(hdc, FIVE_MARK_POINT_RADIUS, (POINT){ptLeftTop.x + 3 * cxCell, ptLeftTop.y + 11 * cyCell});
_DrawBlackSolidPoint(hdc, FIVE_MARK_POINT_RADIUS, (POINT){ptLeftTop.x + 11 * cxCell, ptLeftTop.y + 11 * cyCell});
return S_OK;
}
// 定义棋盘的左上角点
POINT ptBoardTop = {10, 10};
int cxClient = 0, cyClient = 0;
// 逻辑棋盘
int chessBoard[BOARD_CELL_NUM + 1][BOARD_CELL_NUM + 1] = {0};
BOOL bIsBlackTurn = TRUE;
HRESULT UndoLastMove(HWND hwnd)
{
if (moveCount == 0)
{
MessageBox(hwnd, TEXT("没有可以悔棋的步骤了!"), TEXT("提示"), MB_OK);
return S_FALSE;
}
moveCount--;
ChessMove lastMove = moveHistory[moveCount];
chessBoard[lastMove.pos.y][lastMove.pos.x] = NULL_FLAG;
InvalidateRect(hwnd, NULL, TRUE);
return S_OK;
}
// 清空棋盘
HRESULT ClearChessBoard()
{
for (int row = 0; row < BOARD_CELL_NUM + 1; ++row)
{
for (int col = 0; col < BOARD_CELL_NUM + 1; ++col)
{
chessBoard[row][col] = NULL_FLAG;
}
}
bIsBlackTurn = TRUE;
moveCount = 0; // 清空步骤记录
return S_OK;
}
// 获取逻辑坐标点的标志
HRESULT GetCellFlag(POINT logicalPos, int *cellFlag)
{
*cellFlag = chessBoard[logicalPos.y][logicalPos.x];
return S_OK;
}
// 设置逻辑坐标点的标志
HRESULT SetCellFlag(POINT logicalPos, int cellFlag)
{
chessBoard[logicalPos.y][logicalPos.x] = cellFlag;
return S_OK;
}
// 在指定位置绘制棋子
HRESULT DrawChessPiece(HDC hdc, POINT logicalPos, int chessFlag)
{
// 获得一小格的宽度和高度
int cxCell = 0, cyCell = 0;
_GetCellWidthAndHeight(ptBoardTop, cxClient, cyClient, &cxCell, &cyCell);
// 获取实际绘制点
POINT actualPos = {0};
_ExchangeActualPositon(logicalPos, cxCell, cyCell, ptBoardTop, &actualPos);
// 绘制棋子
switch (chessFlag)
{
case BLACK_FLAG:
_DrawBlackSolidPoint(hdc, CHESS_PIECE_RADIUS, actualPos);
break;
case WHITE_FLAG:
_DrawWhiteHollowPoint(hdc, CHESS_PIECE_RADIUS, actualPos);
break;
default:
break;
}
return S_OK;
}
// 是否存在某方向连子
BOOL IsExistWinFlagInSomeDirection(Win_Direction winDirection, POINT logicalPos, int winChessFlag, int winChessPieceNum)
{
POINT chessPos = logicalPos;
switch (winDirection)
{
case Direction_Top:
chessPos.y -= winChessPieceNum;
break;
case Direction_RightTop:
chessPos.y -= winChessPieceNum;
chessPos.x += winChessPieceNum;
break;
case Direction_Right:
chessPos.x += winChessPieceNum;
break;
case Direction_RightBottom:
chessPos.y += winChessPieceNum;
chessPos.x += winChessPieceNum;
break;
case Direction_Bottom:
chessPos.y += winChessPieceNum;
break;
case Direction_LeftBottom:
chessPos.y += winChessPieceNum;
chessPos.x -= winChessPieceNum;
break;
case Direction_Left:
chessPos.x -= winChessPieceNum;
break;
case Direction_LeftTop:
chessPos.y -= winChessPieceNum;
chessPos.x -= winChessPieceNum;
break;
}
// 检查坐标合法性
if (chessPos.x < 0 || chessPos.y < 0 || chessPos.x > BOARD_CELL_NUM || chessPos.y > BOARD_CELL_NUM)
{
return FALSE;
}
// 获取坐标值
int cellFlag = 0;
GetCellFlag(chessPos, &cellFlag);
if (winChessFlag != cellFlag)
{
return FALSE;
}
return TRUE;
}
// 判断是否存在胜利者
// HRESULT IsSomeoneWin(POINT logicalPos, int chessFlag)
// {
// // 遍历某点的所有方向,查看是否存在5连子
// int maxWinPieceNum = WIN_CONDITION - 1;
// for (int direction = Direction_Top; direction <= Direction_LeftTop; ++direction) {
// int winPieceNum = 0;
// for (int count = 1; count <= maxWinPieceNum; ++count) {
// if (!IsExistWinFlagInSomeDirection(direction, logicalPos, chessFlag, count)) {
// break;
// }
// winPieceNum++;
// }
// for (int count = 1; count <= maxWinPieceNum; ++count) {
// if (!IsExistWinFlagInSomeDirection((Win_Direction)((int)Direction_Bottom - direction), logicalPos, chessFlag, count)) {
// break;
// }
// winPieceNum++;
// }
// if (winPieceNum >= maxWinPieceNum) {
// return S_OK;
// }
// }
// return S_FALSE;
// }
#define BOARD_CELL_NUM 15
#define BLACK_FLAG 1
#define WHITE_FLAG 2
#define NULL_FLAG 0
// 方向数组,用于遍历8个方向
int directions[8][2] = {
{0, 1}, {1, 0}, {1, 1}, {1, -1}, {0, -1}, {-1, 0}, {-1, -1}, {-1, 1}};
BOOL IsSomeoneWin(POINT pos, int currentChessFlag)
{
int count;
int x, y;
// 遍历8个方向
for (int d = 0; d < 4; d++)
{
count = 1;
// 向正方向延伸
for (int i = 1; i < 5; i++)
{
x = pos.x + directions[d][0] * i;
y = pos.y + directions[d][1] * i;
if (x < 0 || x >= BOARD_CELL_NUM + 1 || y < 0 || y >= BOARD_CELL_NUM + 1)
{
break;
}
if (chessBoard[y][x] == currentChessFlag)
{
count++;
}
else
{
break;
}
}
// 向反方向延伸
for (int i = 1; i < 5; i++)
{
x = pos.x - directions[d][0] * i;
y = pos.y - directions[d][1] * i;
if (x < 0 || x >= BOARD_CELL_NUM + 1 || y < 0 || y >= BOARD_CELL_NUM + 1)
{
break;
}
if (chessBoard[y][x] == currentChessFlag)
{
count++;
}
else
{
break;
}
}
// 判断是否有5个连续的棋子
if (count >= 5)
{
return TRUE;
}
}
return FALSE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
POINT logicalPos = {0};
switch (message)
{
case WM_CREATE:
ClearChessBoard();
CreateWindow(TEXT("button"), TEXT("悔棋"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
520, 10, 70, 30, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL);
cursorPos.x = BOARD_CELL_NUM / 2;
cursorPos.y = BOARD_CELL_NUM / 2;
return 0;
case WM_COMMAND:
if (LOWORD(wParam) == 1)
{
UndoLastMove(hwnd);
}
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
if (S_OK != _ExChangeLogicalPosition((POINT){LOWORD(lParam), HIWORD(lParam)}, ptBoardTop, cxClient, cyClient, &logicalPos))
{
return 0;
}
int cellFlag = 0;
GetCellFlag(logicalPos, &cellFlag);
if (NULL_FLAG != cellFlag)
{
MessageBox(NULL, TEXT("这个位置已经有棋子了,请换个地方下棋!"), TEXT("提示"), MB_OK);
return 0;
}
// 获取棋子
int currentChessFlag = bIsBlackTurn ? BLACK_FLAG : WHITE_FLAG;
// 记录当前步
moveHistory[moveCount].pos = logicalPos;
moveHistory[moveCount].flag = currentChessFlag;
moveCount++;
// 绘制棋子
DrawChessPiece(hdc, logicalPos, currentChessFlag);
// 设置标志
SetCellFlag(logicalPos, currentChessFlag);
// 判断胜利
if (TRUE == IsSomeoneWin(logicalPos, currentChessFlag))
{
if (IDYES == MessageBox(hwnd, bIsBlackTurn ? TEXT("黑方获胜!是否重新开始?") : TEXT("白方获胜!是否重新开始?"), TEXT("提示"), MB_YESNO))
{
ClearChessBoard();
InvalidateRect(hwnd, NULL, TRUE);
}
else
{
DestroyWindow(hwnd);
}
}
bIsBlackTurn = !bIsBlackTurn;
ReleaseDC(hwnd, hdc);
return 0;
// 键盘控制
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT:
if (cursorPos.x > 0)
cursorPos.x--;
break;
case VK_RIGHT:
if (cursorPos.x < BOARD_CELL_NUM)
cursorPos.x++;
break;
case VK_UP:
if (cursorPos.y > 0)
cursorPos.y--;
break;
case VK_DOWN:
if (cursorPos.y < BOARD_CELL_NUM)
cursorPos.y++;
break;
case VK_SPACE:
case VK_RETURN:
hdc = GetDC(hwnd);
int cellFlag = 0;
GetCellFlag(cursorPos, &cellFlag);
if (NULL_FLAG != cellFlag)
{
MessageBox(NULL, TEXT("这个位置已经有棋子了,请换个地方下棋!"), TEXT("提示"), MB_OK);
ReleaseDC(hwnd, hdc);
return 0;
}
// 获取棋子
int currentChessFlag = bIsBlackTurn ? BLACK_FLAG : WHITE_FLAG;
// 记录当前步
moveHistory[moveCount].pos = cursorPos;
moveHistory[moveCount].flag = currentChessFlag;
moveCount++;
// 绘制棋子
DrawChessPiece(hdc, cursorPos, currentChessFlag);
// 设置标志
SetCellFlag(cursorPos, currentChessFlag);
// 判断胜利
if (TRUE == IsSomeoneWin(cursorPos, currentChessFlag))
{
if (IDYES == MessageBox(hwnd, bIsBlackTurn ? TEXT("黑方获胜!是否重新开始?") : TEXT("白方获胜!是否重新开始?"), TEXT("提示"), MB_YESNO))
{
ClearChessBoard();
InvalidateRect(hwnd, NULL, TRUE);
}
else
{
DestroyWindow(hwnd);
}
}
bIsBlackTurn = !bIsBlackTurn;
ReleaseDC(hwnd, hdc);
break;
}
InvalidateRect(hwnd, NULL, TRUE); // 重绘棋盘以显示光标
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
// 绘制光标
POINT actualPos;
int cxCell, cyCell;
_GetCellWidthAndHeight(ptBoardTop, cxClient, cyClient, &cxCell, &cyCell);
_ExchangeActualPositon(cursorPos, cxCell, cyCell, ptBoardTop, &actualPos);
SetBkColor(hdc, RGB(255, 255, 255));
Rectangle(hdc, actualPos.x - cxCell / 2, actualPos.y - cyCell / 2, actualPos.x + cxCell / 2, actualPos.y + cyCell / 2);
DrawChessBoard(hdc, ptBoardTop, cxClient, cyClient);
for (int row = 0; row < BOARD_CELL_NUM + 1; ++row)
{
for (int col = 0; col < BOARD_CELL_NUM + 1; ++col)
{
if (chessBoard[row][col] != NULL_FLAG)
{
DrawChessPiece(hdc, (POINT){col, row}, chessBoard[row][col]);
}
}
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}