https://blog.csdn.net/lie_u/article/details/107950505
#include <graphics.h>
#include <conio.h>
#include<stdio.h>
#include <time.h>
#define NUM_B 225 //棋子数量
#define HL 15 //上下
#define X(x) 162+x50
#define Y(y) 30+y50
#define BXY 15 //横轴/纵轴数量
void board(void);
int get_x(short xx);
int get_y(short yy);
int left_right(int(*arr)[BXY], short x, short y); //左右
int up_down(int(*arr)[BXY], short x, short y); //上下
int lu_rd(int(*arr)[BXY], short x, short y); //左上右下
int ld_ru(int(*arr)[BXY], short x, short y); //左下右上
void win_b(void);
void win_w(void);
void fall_w(void); //落白子
void fall_b(void); //落黑子
int main()
{
int undo_x = 0, undo_y = 0;
int again = 0; //用于胜利后开启下一局的开关
int cut = 0; //用于胜利后棋盘不可下棋的开关
int FLAG[BXY][BXY]; //棋盘,储存棋子,0代表空,1代表白子,2代表黑子
int x, y; //坐标
int i, j, wb; //i,j储存鼠标上一个状态,wb控制下一局开局的棋子颜色(这个感觉有没有都一样,,,写了就不删了)
i = j = wb = 0;
int x1[2] = { -2,-2 }, y1[2] = { -2,-2 }; //储存鼠标前后状态,用于消除十字准星
board(); //创建窗口
fall_b();
/* 储存所有棋子坐标,并且把棋子状态设为0 */
for (y = 0; y < 15; y++)
for (x = 0; x < 15; x++)
FLAG[x][y] = 0;
MOUSEMSG m; // 定义鼠标消息
while (true) //游戏无限循环
{
m = GetMouseMsg(); //获取鼠标信息
switch (m.uMsg)
{
case WM_MOUSEMOVE: //鼠标移动
if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1) //十字准星
{
// 创建一个矩形区域
HRGN rgn = CreateRectRgn(162, 30, 862, 730);
// 将该矩形区域设置为裁剪区
setcliprgn(rgn);
// 不再使用 rgn,清理 rgn 占用的系统资源
DeleteObject(rgn);
if (i == 0)
{
x1[i] = get_x(m.x);
y1[i] = get_y(m.y);
i = 1;
j = 0;
}
else if (i == 1)
{
x1[i] = get_x(m.x);
y1[i] = get_y(m.y);
i = 0;
j = 1;
}
setfillcolor(LIGHTBLUE);
solidrectangle(X(get_x(m.x)) - 15, Y(get_y(m.y)) - 3, X(get_x(m.x)) + 15, Y(get_y(m.y)) + 3);
solidrectangle(X(get_x(m.x)) - 3, Y(get_y(m.y)) - 15, X(get_x(m.x)) + 3, Y(get_y(m.y)) + 15);
}
break;
case WM_LBUTTONDOWN: //鼠标左键点击
if (cut == 0 && (get_x(m.x) > -1 && get_x(m.x) < 15) && (get_y(m.y) > -1 && get_y(m.y) < 15) && FLAG[get_x(m.x)][get_y(m.y)] < 1)
{
// 取消之前设置的裁剪区
setcliprgn(NULL);
if (wb == 0)
{
setfillcolor(BLACK);
solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
undo_x = get_x(m.x);
undo_y = get_y(m.y);
fall_w();
wb = 1;
FLAG[get_x(m.x)][get_y(m.y)] = 2;
if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
{
win_b();
again = 1;
cut = 1;
}
}
else if (wb == 1)
{
setfillcolor(WHITE);
solidcircle(X(get_x(m.x)), Y(get_y(m.y)), 20);
undo_x = get_x(m.x);
undo_y = get_y(m.y);
fall_b();
wb = 0;
FLAG[get_x(m.x)][get_y(m.y)] = 1;
if (left_right(FLAG, m.x, m.y) > 0 || up_down(FLAG, m.x, m.y) > 0 || lu_rd(FLAG, m.x, m.y) > 0 || ld_ru(FLAG, m.x, m.y) > 0)
{
win_w();
again = 1;
cut = 1;
}
}
}
break;
case WM_RBUTTONUP: //鼠标右键点击
if (again == 1)
{
for (y = 0; y < 15; y++)
for (x = 0; x < 15; x++)
FLAG[x][y] = 0;
// 取消之前设置的裁剪区
setcliprgn(NULL);
board();
undo_x = undo_y = 0;
again = 0;
cut = 0;
if (wb == 1)
{
wb = 0;
fall_b();
}
else
{
wb = 1;
fall_w();
}
}
else
{
if (FLAG[undo_x][undo_y] == 1)
{
// 取消之前设置的裁剪区
setcliprgn(NULL);
setfillcolor(RGB(209, 186, 116)); //填充背景颜色
solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
/*9个带黑点的落子位置*/
if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(undo_x), Y(undo_y), 5);
}
fall_w();
FLAG[undo_x][undo_y] = 0;
wb = 1;
}
else if (FLAG[undo_x][undo_y] == 2)
{
// 取消之前设置的裁剪区
setcliprgn(NULL);
setfillcolor(RGB(209, 186, 116)); //填充背景颜色
solidrectangle(X(undo_x) - 20, Y(undo_y) - 20, X(undo_x) + 20, Y(undo_y) + 20);
line(X(undo_x) - 20, Y(undo_y), X(undo_x) + 20, Y(undo_y));
line(X(undo_x), Y(undo_y) - 20, X(undo_x), Y(undo_y) + 20);
/*9个带黑点的落子位置*/
if (undo_x != 15 && undo_y != 15 && (undo_x + 1) % 4 == 0 && (undo_y + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(undo_x), Y(undo_y), 5);
}
fall_b();
FLAG[undo_x][undo_y] = 0;
wb = 0;
}
}
break;
}
if (((get_x(m.x)) != x1[j] || (get_y(m.y) != y1[j])) && FLAG[x1[j]][y1[j]] < 1)
{
setfillcolor(RGB(209, 186, 116)); //填充背景颜色
solidrectangle(X(x1[j]) - 15, Y(y1[j]) - 15, X(x1[j]) + 15, Y(y1[j]) + 15);
line(X(x1[j]) - 15, Y(y1[j]), X(x1[j]) + 15, Y(y1[j]));
line(X(x1[j]), Y(y1[j]) - 15, X(x1[j]), Y(y1[j]) + 15);
/*9个带黑点的落子位置*/
if (x1[j] != 15 && y1[j] != 15 && (x1[j] + 1) % 4 == 0 && (y1[j] + 1) % 4 == 0)
{
setfillcolor(BLACK);
fillcircle(X(x1[j]), Y(y1[j]), 5);
}
}
}
// 关闭图形窗口
closegraph();
_getch();
return 0;
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
}
void board()
{
initgraph(1024, 760); //窗口大小
setbkcolor(RGB(190, 231, 233)); //窗口背景
cleardevice();
setlinecolor(BLACK); //线条颜色
setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盘边框线条样式
setfillcolor(RGB(209, 186, 116)); //棋盘背景颜色
fillrectangle(132, 0, 892, 760);
fillrectangle(162, 30, 862, 730); //棋盘大小,棋盘规格15X15
setlinestyle(PS_SOLID | PS_ENDCAP_SQUARE, 2); //棋盘线条样式
for (int i = 80; i <= 730; i += 50) //棋盘横线
line(162, i, 862, i);
for (int i = 212; i <= 862; i += 50) //棋盘竖线
line(i, 30, i, 730);
setfillcolor(BLACK); //圆点填充颜色为黑色
for (int i = 3; i <= 12; i += 4) //绘制9个圆点
{
fillcircle(X(i), Y(3), 5);
fillcircle(X(i), Y(7), 5);
fillcircle(X(i), Y(11), 5);
}
setfillcolor(RGB(190, 237, 199)); //棋盘背景颜色
fillrectangle(20, 310, 115, 425);
settextcolor(BLACK);
settextstyle(25, 0, _T(“Consolas”));
wchar_t s[] = L"请落子";
outtextxy(30, 315, s);
fillrectangle(5, 460, 125, 495);
settextstyle(15, 0, _T("Consolas"));
wchar_t s2[] = L"点击鼠标右键悔棋";
outtextxy(10, 470, s2);
settextstyle(25, 0, _T("Consolas"));
wchar_t s1[] = L"yp_lien";
settextcolor(RGB(214, 213, 183));
outtextxy(930, 730, s1);
1
2
3
4
5
6
7
8
9
}