-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoGo(witheye_maker).cpp
458 lines (456 loc) · 11.1 KB
/
NoGo(witheye_maker).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
#include <easyx.h>
#include <graphics.h>
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int board[10][10] = { 0 };//0表无子,1表黑子,2表白子
int check_board[10][10] = { 0 };//是否要归零还不确定
int edge = 9;
int MOVE[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int cnt = 0;
int flag_side = 0;
int MININT = -2147483648;
int row = 0, col = 0, best_row, best_col, best_value = MININT, value = 0;
void print_board()
{
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
//下面行读取图像文件
//loadimage(&black, _T("hei.jpg"), 54, 54, FALSE);
//loadimage(&white, _T("bai.jpg"), 54, 54, FALSE);
//loadimage(&cundang, _T("cundang.jpg"), 141, 50, FALSE);
//下面行显示图像文件
//putimage(600, 50, &black);
//putimage(600, 380, &white);
//putimage(600, 490, &cundang);
//下面代码网格
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 540, 540);
setfillcolor(RGB(255, 239, 213));
fillrectangle(60, 60, 540, 540);
setcolor(RGB(0, 0, 0));
line(60, 120, 540, 120);
line(60, 180, 540, 180);
line(60, 240, 540, 240);
line(60, 300, 540, 300);
line(60, 360, 540, 360);
line(60, 420, 540, 420);
line(60, 480, 540, 480);
line(120, 60, 120, 540);
line(180, 60, 180, 540);
line(240, 60, 240, 540);
line(300, 60, 300, 540);
line(360, 60, 360, 540);
line(420, 60, 420, 540);
line(480, 60, 480, 540);
setcolor(RGB(0, 0, 0));//画黑棋选项
rectangle(600, 120, 660, 180);
setfillcolor(RGB(0, 0, 0));
fillrectangle(600, 120, 660, 180);
setcolor(BLACK);//画白棋选项
rectangle(600, 240, 660, 300);
setfillcolor(WHITE);
fillrectangle(600, 240, 660, 300);
outtextxy(632, 423, L"RETURN");
setcolor(RGB(0, 0, 0));
rectangle(600, 420, 720, 440);
outtextxy(643, 483, L"SAVE");
setcolor(RGB(0, 0, 0));
rectangle(600, 480, 720, 500);
return;
}
void print_menu()
{
initgraph(800, 600); //初始化图形系统
setbkcolor(RGB(255, 239, 213)); //设置图形窗口的背景颜色
cleardevice(); //清屏幕
setcolor(RGB(0, 0, 0));
rectangle(60, 60, 300, 120);
rectangle(60, 180, 300, 240);
rectangle(60, 300, 300, 360);
rectangle(60, 420, 300, 480);
settextstyle(30, 0, _T("Consolas"));
outtextxy(145, 193, _T("BEGIN"));
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;
if (x >= 60 && x <= 300 && y >= 180 && y <= 240)
{
break;
}
}
}
return;
}
void print_game_over_AI(int flag_side)
{
if (flag_side == 2) {//AI执白,AI输
outtextxy(600, 360, L"BLACK SIDE WINS");
}
else if (flag_side == 1) {//AI执黑,AI输
outtextxy(600, 360, L"WHITE SIDE WINS");
}
return;
}
void print_game_over_human(int human_flag_side)
{
if (human_flag_side == 1) {//AI执白,AI赢
outtextxy(600, 360, L"WHITE SIDE WINS");
}
else if (human_flag_side == 2) {//AI执黑,AI赢
outtextxy(600, 360, L"BLACK SIDE WINS");
}
return;
}
int evaluation(int i, int j)
{
srand((unsigned int)time(0));
return (rand() % 9);
}
int check(int r, int c)//返回1表示在棋盘内
{
if (r >= 0 && r < 9 && c >= 0 && c < 9)
return 1;
else
return 0;
}
bool judge(int r, int c)//返回1表示能下(与上面保持一致)
{
check_board[r][c] = 1;
int flag = 0;
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] == 0)
flag = 1;
if (board[r1][c1] == board[r][c] && !check_board[r1][c1]) {
if (judge(r1, c1))
flag = 1;
}
}
}
return flag;
}
bool judgement(int r, int c, int flag_side)//返回1表示可下
{
if (board[r][c] == 1 || board[r][c] == 2)
return 0;
board[r][c] = flag_side;
memset(check_board, 0, sizeof(check_board));
//check_board[r][c] = 1;
if (judge(r, c) == 0) {
board[r][c] = 0;
return 0;
}
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] && !check_board[r1][c1]) {
if (!judge(r1, c1)) {
board[r][c] = 0;
return 0;
}
}
}
}
board[r][c] = 0;
return 1;
}
int nega_max(int depth, int flag_side)//flag_side==1表示AI为黑方,flagSide==2表示AI为白方
{
if (depth == 0)
return evaluation(row, col);
for (row = 0; row < edge; row++) {
for (col = 0; col < edge; col++) {
if (board[row][col] == 0) {
if (judgement(row, col, flag_side) == 0) {
continue;
}
else {
value = -nega_max(depth - 1, flag_side);
}
if (value > best_value) {
best_value = value;
best_row = row;
best_col = col;
}
//以下用来清空初始数据
col = 0; row = 0;
}
else
continue;
}
}
return best_value;
}
int judge_eye(int r,int c,int flag_side)//返回1表示优先下此处
{
int Qi = 4;
if (check(r, c)) {
for (int i = 0; i < 4; i++) {
int r1 = r + MOVE[i][0];
int c1 = c + MOVE[i][1];
if (check(r1, c1)) {
if (board[r1][c1] != 0) {
Qi--;
}
}
else {
Qi--;
}
}
}
if (Qi == 1)
return 1;
else
return 0;
}
int eye_maker(int r, int c, int flag_side)
{
return 0;
}
void choose_side()
{
while (1) {//选择对弈方
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton)
{
int x = msg.x;
int y = msg.y;
//flag_side = choose_side(x, y);
if (x >= 600 && x <= 660 && y >= 120 && y <= 180)//600, 120, 660, 180
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 240, 660, 300);
flag_side = 2;//AI执白
return;
}
else if (x >= 600 && x <= 660 && y >= 240 && y <= 300)//600, 240, 660, 300
{
setfillcolor(RGB(255, 239, 213));
fillrectangle(600, 120, 660, 180);
flag_side = 1;//AI执黑
return;
}
else if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
print_menu();
}
}
FlushMouseMsgBuffer();
}//选择对弈方
}
void stop()
{
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton) {
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
break;
}
}
}
return;
}
void first_move_AI(int flag_side)
{
if (flag_side == 1)//人选白棋时AI的第一手棋
{
int x0 = 60; int y0 = 60;
int initial_x = 7, initial_y = 1;
board[initial_y][initial_x] = flag_side;
setfillcolor(BLACK);
fillcircle(x0 + initial_x * 60, y0 + initial_y * 60, 25);
cnt++;
}//人选白棋时AI的第一手棋
return;
}
//以下图形界面相关
//x横,y竖
IMAGE white;
IMAGE black;
IMAGE cundang;
int main()
{
while (1) {
print_menu();
print_board();
//下面代码鼠标功能
//int cnt=0;//用来统计总共走了多少步
choose_side();
first_move_AI(flag_side);
//结束之后无法再次begin,可以通过将落子,AI判断那一块写成函数解决
//未落子情况下无法return
int flag_game_over = 0;
while (1)
{
MOUSEMSG msg = GetMouseMsg();
if (msg.mkLButton && flag_side == 2)//人执黑,AI执白
{
int finish_loop = 0;//用来让游戏结束
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
break;
}
//以下可改写为一个函数
for (int j = 0; j < edge; j++) {
for (int i = 0; i < edge; i++) {
int x0 = 60, y0 = 60;
if (((x - (x0 + i * 60)) * (x - (x0 + i * 60)) + (y - (y0 + j * 60)) * (y - (y0 + j * 60)) <= 30 * 30) && (board[j][i] == 0)) {
if (judgement(j, i, 1)) {
board[j][i] = 1;
setfillcolor(BLACK);
fillcircle(x0 + i * 60, y0 + j * 60, 25);
cnt++;
//人走后,马上进AI判断
vector<int> available_list; //合法位置表
int suspension = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (judgement(i, j, flag_side)) {
if (!judge_eye(i, j, flag_side)) {
available_list.push_back(i * 9 + j);
}
else {
suspension = 1;
board[i][j] = flag_side;
setfillcolor(WHITE);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
cnt++;
break;
}
}
}
if (suspension)
break;
}
if (suspension == 0) {
if (available_list.size() == 0) {//若AI没位置下,判AI输
print_game_over_AI(flag_side);
flag_game_over = 1;
}
else {
int result = available_list[rand() % available_list.size()];
best_row = result / 9; best_col = result % 9;
board[best_row][best_col] = flag_side;
setfillcolor(WHITE);
fillcircle(x0 + best_col * 60, y0 + best_row * 60, 25);
cnt++;
}
}
suspension = 0;
}
else {
print_game_over_human(1);
flag_game_over = 1;
}
finish_loop = 1;
best_row = 0; best_col = 0;
break;
}
}
if (finish_loop)
break;
}
if (flag_game_over)
break;
}
else if (msg.mkLButton && flag_side == 1)//人执白,AI执黑
{
int flag_game_over = 0;//用来让游戏结束
int x = msg.x;
int y = msg.y;
if (x >= 600 && x <= 720 && y >= 420 && y <= 440) {
break;
}
//以下可改写为一个函数
for (int j = 0; j < edge; j++) {
int finish_loop = 0;
for (int i = 0; i < edge; i++) {
int x0 = 60, y0 = 60;
if (((x - (x0 + i * 60)) * (x - (x0 + i * 60)) + (y - (y0 + j * 60)) * (y - (y0 + j * 60)) <= 30 * 30) && board[j][i] == 0) {
if (judgement(j, i, 2)) {
board[j][i] = 2;
setfillcolor(WHITE);
fillcircle(x0 + i * 60, y0 + j * 60, 25);
cnt++;
vector<int> available_list; //合法位置表
int suspension = 0;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (judgement(i, j, flag_side)) {
if (!judge_eye(i, j, flag_side)) {
available_list.push_back(i * 9 + j);
}
else {
suspension = 1;
board[i][j] = flag_side;
setfillcolor(BLACK);
fillcircle(x0 + j * 60, y0 + i * 60, 25);
cnt++;
break;
}
}
}
if (suspension)
break;
}
if (suspension == 0) {
if (available_list.size() == 0) {
print_game_over_AI(flag_side);
flag_game_over = 1;
}
else {
int result = available_list[rand() % available_list.size()];
best_row = result / 9; best_col = result % 9;
board[best_row][best_col] = flag_side;
setfillcolor(BLACK);
fillcircle(x0 + best_col * 60, y0 + best_row * 60, 25);
cnt++;
}
}
suspension = 0;
}
else {
print_game_over_human(2);
flag_game_over = 1;
}
finish_loop = 1;
best_row = 0; best_col = 0;
break;
}
}
if (finish_loop)
break;
}
if (flag_game_over)
break;
}
FlushMouseMsgBuffer();
}
if (flag_game_over) {
stop();
}
for (int i = 0; i < edge; i++) {
for (int j = 0; j < edge; j++) {
board[i][j] = 0;
}
}
cnt = 0; flag_side = 0; flag_game_over = 0;
row = 0, col = 0, best_row, best_col, best_value = MININT, value = 0;
}
return 0;
}