-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.cpp
464 lines (416 loc) · 11.6 KB
/
main.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
/*
说明:
本项目代码需要openGL基本运行环境的支持,如果编译过程遇到问题,请联系我(阿杰):
Email:[email protected]
note:
if you are encountering any problem when running the project,feel free to contact me.
my Email is [email protected]
*/
#include "openGL.h"
#include "chess.h"
GLuint locxsize;
GLuint locysize;
GLuint vaoIDs[3];
GLuint vboIDs[6];
// 窗口大小
int xsize = 780;
int ysize = 780;
// 绘制窗口的颜色变量
vec4 yellow = vec4(1.0, 0.89, 0.77, 1.0);
vec4 white = vec4(1.0, 1.0, 1.0, 1.0);
vec4 black = vec4(0.0, 0.0, 0.0, 1.0);
vec4 red = vec4(1.0, 0.0, 0.0, 1.0);
vec4 green = vec4(0.0, 1.0, 0.0, 1.0);
const int ELLIPSE_NUM_POINTS = 100;
int mainWindowSubmenu;
int mainWindowSubmenu1;
int mainWindow;
int mainWindowMenu;
int subWindow;
int subWindowMenu;
int subWindow1;
int subWindowMenu1;
const int MENU_CHOICE_BOOT = 0;
const int MENU_CHOICE_HUMAN = 1;
const int MENU_CHOICE_ROLLBACK = 2;
const int MENU_CHOICE_SAVE = 3;
const int MENU_CHOICE_LOAD = 4;
const int MENU_CHOICE_EXIT = 5;
const int MENU_CHOICE_RANK1 = 6;
const int MENU_CHOICE_RANK2 = 7;
const int MENU_CHOICE_RANK3 = 8;
const int MENU_CHOICE_MUSIC = 9;
bool isOk = false;
bool isMusic = true;
// 游戏和OpenGL初始化
vec4 gridpoints[60 + 255 * ELLIPSE_NUM_POINTS];
vec4 gridcolours[60 + 255 * ELLIPSE_NUM_POINTS];
Gomoku gomoku;
// 获得圆上的点
vec2 getEllipseVertex(vec2 center, double scale, double verticalScale, double angle)
{
vec2 vertex(sin(angle), cos(angle));
vertex *= scale;
vertex += center;
vertex.y *= verticalScale;
return vertex;
}
void init()
{
//设置棋盘背景色
glClearColor(0.80f, 0.52f, 0.25f, 0.0f);
// 初始化棋盘格,包含60个顶点坐标(总共30条线),并且每个顶点一个颜色值3
// 纵向线
int i = 0;
for (int i = 0; i < 15; i++)
{
gridpoints[2 * i] = vec4((-150 + (50.0 * i)), 10.0, 0, 1);
gridpoints[2 * i + 1] = vec4((-150 + (50.0 * i)), 710.0, 0, 1);
}
// 水平线
for (int i = 0; i < 15; i++)
{
gridpoints[30 + 2 * i] = vec4(-150.0, (10 + (50.0 * i)), 0, 1);
gridpoints[30 + 2 * i + 1] = vec4(550.0, (10 + (50.0 * i)), 0, 1);
}
// 将所有线赋成黑色
for (int i = 0; i < 60; i++)
gridcolours[i] = black;
// 载入着色器
GLuint program = InitShader("vshader.glsl", "fshader.glsl");
glUseProgram(program);
locxsize = glGetUniformLocation(program, "xsize");
locysize = glGetUniformLocation(program, "ysize");
GLuint vPosition = glGetAttribLocation(program, "vPosition");
GLuint vColor = glGetAttribLocation(program, "vColor");
glGenVertexArrays(3, &vaoIDs[0]);
// 棋盘格顶点
glBindVertexArray(vaoIDs[0]);
glGenBuffers(2, vboIDs);
// 棋盘格顶点位置
glBindBuffer(GL_ARRAY_BUFFER, vboIDs[0]);
glBufferData(GL_ARRAY_BUFFER, (68 + 225 * ELLIPSE_NUM_POINTS) * sizeof(vec4), gridpoints, GL_DYNAMIC_DRAW);
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vPosition);
// 棋盘格顶点颜色
glBindBuffer(GL_ARRAY_BUFFER, vboIDs[1]);
glBufferData(GL_ARRAY_BUFFER, (68 + 225 * ELLIPSE_NUM_POINTS) * sizeof(vec4), gridcolours, GL_DYNAMIC_DRAW);
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vColor);
}
/** \brief 生成圆的坐标*/
void generateEllipsePoints(bool currentState, int startVertexIndex, int numPoints,
vec2 center, double scale, double verticalScale)
{
double angleIncrement = (2 * M_PI) / numPoints;
double currentAngle = M_PI / 2;
for (int i = startVertexIndex; i < startVertexIndex + numPoints; ++i) {
gridpoints[i] = vec4(getEllipseVertex(center, scale, verticalScale, currentAngle), 0, 1);
if (currentState == 0) {
gridcolours[i] = white;
}
else {
gridcolours[i] = black;
}
currentAngle += angleIncrement;
}
}
// 获得正方形上的点,用来定位最后落棋点
void updateSquarePoints(float x, float y)
{
gridpoints[60] = vec4(x - 25, y - 25, 0, 1);
gridpoints[61] = vec4(x + 25, y - 25, 0, 1);
gridpoints[62] = vec4(x + 25, y - 25, 0, 1);
gridpoints[63] = vec4(x + 25, y + 25, 0, 1);
gridpoints[64] = vec4(x + 25, y + 25, 0, 1);
gridpoints[65] = vec4(x - 25, y + 25, 0, 1);
gridpoints[66] = vec4(x - 25, y + 25, 0, 1);
gridpoints[67] = vec4(x - 25, y - 25, 0, 1);
for (int i = 60; i<68; ++i)
gridcolours[i] = green;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
// 棋盘格顶点位置
glBindBuffer(GL_ARRAY_BUFFER, vboIDs[0]);
glBufferData(GL_ARRAY_BUFFER, (68 + 225 * ELLIPSE_NUM_POINTS) * sizeof(vec4), gridpoints, GL_DYNAMIC_DRAW);
// 棋盘格顶点颜色
glBindBuffer(GL_ARRAY_BUFFER, vboIDs[1]);
glBufferData(GL_ARRAY_BUFFER, (68 + 225 * ELLIPSE_NUM_POINTS) * sizeof(vec4), gridcolours, GL_DYNAMIC_DRAW);
glUniform1i(locxsize, xsize);
glUniform1i(locysize, ysize);
glBindVertexArray(vaoIDs[0]);
// 绘制棋盘格的线
glDrawArrays(GL_LINES, 0, 60);
if (gomoku.piecesCnt>0)
glDrawArrays(GL_LINES, 60, 68);
// 绘制棋子
int startOffset = 68;
for (int i = 0; i<gomoku.piecesCnt; ++i, startOffset += ELLIPSE_NUM_POINTS)
glDrawArrays(GL_TRIANGLE_FAN, startOffset, ELLIPSE_NUM_POINTS);
//glFlush();
glutSwapBuffers();
int x, y;
if (isOk)
{
gomoku.getBestPos(x, y);
cout << "hit:" << gomoku.hit << endl;
gomoku.hit = 0;
cout << "boot:" << x << ' ' << y << endl;
isOk = false;
if (gomoku.placePiece(x, y))
{
gomoku.isGameOver(x, y);
if (gomoku.gameOver)
{
switch (gomoku.gameOver)
{
case 1:
MessageBox(NULL, ("白方赢"), ("操作提示"), MB_OK);
break;
case 2:
MessageBox(NULL, ("黑方赢"), ("操作提示"), MB_OK);
break;
case 3:
MessageBox(NULL, ("和局"), ("操作提示"), MB_OK);
break;
}
}
}
}
}
void mainWindowMouse(int button, int state, int x, int y)
{
if (gomoku.gameOver == 0 && button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
cout << x << ' ' << y << endl;
int a[15];
int i;
for (i = 0; i < 15; ++i)
{
a[i] = 40 + 50 * i;
}
if (x - a[x / 50] >= -25 && x - a[x / 50] < 25)
x = a[x / 50];
else
x = a[x / 50 - 1];
if (y - a[y / 50] >= -25 && y - a[y / 50] < 25)
y = a[y / 50];
else
y = a[y / 50 - 1];
if (!gomoku.placePiece((y - 40) / 50, (x - 40) / 50))
MessageBox(NULL, ("点击位置不合法"), ("操作提示"), MB_YESNO | MB_ICONQUESTION);
else
{
gomoku.isGameOver((y - 40) / 50, (x - 40) / 50);
if (gomoku.gameOver == 0)
{
isOk = true;
}
else
{
switch (gomoku.gameOver)
{
case 1:
MessageBox(NULL, ("白方赢"), ("操作提示"), MB_OK);
break;
case 2:
MessageBox(NULL, ("黑方赢"), ("操作提示"), MB_OK);
break;
case 3:
MessageBox(NULL, ("和局"), ("操作提示"), MB_OK);
break;
}
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// 在窗口被拉伸的时候,控制棋盘格的大小,使之保持固定的比例。
void reshape(GLsizei w, GLsizei h)
{
xsize = w;
ysize = h;
glViewport(0, 0, w, h);
}
//////////////////////////////////////////////////////////////////////////
// 键盘响应时间中的普通按键响应
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 033: // ESC键 和 'q' 键退出游戏
exit(EXIT_SUCCESS);
break;
case 'q':
exit(EXIT_SUCCESS);
break;
case 'r': // 'r' 键重启游戏
//restart();
break;
}
glutPostRedisplay();
}
//////////////////////////////////////////////////////////////////////////
void idle(void)
{
glutPostRedisplay();
}
void music(char ch)
{
MCI_OPEN_PARMS OpenParms;//打开设备
OpenParms.lpstrDeviceType = NULL;// "mpegvideo";
OpenParms.lpstrElementName = "abc.mp3";
OpenParms.wDeviceID = NULL;
mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_ELEMENT, (DWORD)&OpenParms);
MCI_PLAY_PARMS PlayParms;//播放文件
PlayParms.dwFrom = 0;
int i;
char buf[50];
if (ch == 'p')//播放
{
i = mciSendString("play abc.mp3 repeat", buf, strlen(buf), NULL);
if (i)
{
char buf[50];
mciGetErrorString(i, buf, strlen(buf));
string s;
MessageBox(NULL, buf, ("操作提示"), MB_YESNO | MB_ICONQUESTION);
}
}
if (ch == 'a')//暂停
{
i = mciSendString("pause abc.mp3", buf, strlen(buf), NULL);
if (i)
{
mciGetErrorString(i, buf, strlen(buf));
MessageBox(NULL, buf, ("操作提示"), MB_YESNO | MB_ICONQUESTION);
}
}
if (ch == 'r')//继续播放
{
i = mciSendString("resume abc.mp3", buf, strlen(buf), NULL);
if (i)
{
mciGetErrorString(i, buf, strlen(buf));
MessageBox(NULL, buf, ("操作提示"), MB_YESNO | MB_ICONQUESTION);
}
}
if (ch == 's')//停止
{
i = mciSendString("stop abc.mp3", buf, strlen(buf), NULL);
if (i)
{
mciGetErrorString(i, buf, strlen(buf));
MessageBox(NULL, buf, ("操作提示"), MB_YESNO | MB_ICONQUESTION);
}
}
}
//////////////////////////////////////////////////////////////////////////
void mainWindowMenuEvents(int menuChoice)
{
switch (menuChoice) {
case MENU_CHOICE_BOOT:
gomoku.restart();
int x, y;
gomoku.getBestPos(x, y);
if (gomoku.placePiece(x, y))
{
gomoku.isGameOver((y - 40) / 50, (x - 40) / 50);
if (gomoku.gameOver)
{
switch (gomoku.gameOver)
{
case 1:
MessageBox(NULL, ("白方赢"), ("操作提示"), MB_YESNO | MB_ICONQUESTION);
break;
case 2:
MessageBox(NULL, ("黑方赢"), ("操作提示"), MB_YESNO | MB_ICONQUESTION);
break;
case 3:
MessageBox(NULL, ("和局"), ("操作提示"), MB_YESNO | MB_ICONQUESTION);
break;
}
}
}
break;
case MENU_CHOICE_HUMAN:
gomoku.restart();
break;
case MENU_CHOICE_ROLLBACK:
if (gomoku.piecesCnt)
gomoku.removePiece(gomoku.steps[gomoku.steps.size() - 1].x, gomoku.steps[gomoku.steps.size() - 1].y);
if (gomoku.piecesCnt)
gomoku.removePiece(gomoku.steps[gomoku.steps.size() - 1].x, gomoku.steps[gomoku.steps.size() - 1].y);
break;
///更改下棋难易度
case MENU_CHOICE_RANK1:
gomoku.changeDifficulty(1);
break;
case MENU_CHOICE_RANK2:
gomoku.changeDifficulty(5);
break;
case MENU_CHOICE_RANK3:
gomoku.changeDifficulty(7);
break;
case MENU_CHOICE_SAVE:
gomoku.saveSteps();
break;
case MENU_CHOICE_LOAD:
gomoku.loadBoard();
break;
case MENU_CHOICE_MUSIC:
if (isMusic)
music('a');
else
music('r');
isMusic = !isMusic;
break;
case MENU_CHOICE_EXIT:
exit(EXIT_SUCCESS);
break;
}
glutPostRedisplay();
}
void mainWindowSetupMenu()
{
mainWindowSubmenu = glutCreateMenu(mainWindowMenuEvents);
glutAddMenuEntry("boot first", MENU_CHOICE_BOOT);
glutAddMenuEntry("player first", MENU_CHOICE_HUMAN);
mainWindowSubmenu1 = glutCreateMenu(mainWindowMenuEvents);
glutAddMenuEntry("beginner", MENU_CHOICE_RANK1);
glutAddMenuEntry("expect", MENU_CHOICE_RANK2);
glutAddMenuEntry("master", MENU_CHOICE_RANK3);
mainWindowMenu = glutCreateMenu(mainWindowMenuEvents);
glutAddSubMenu("new game", mainWindowSubmenu);
glutAddMenuEntry("rollback", MENU_CHOICE_ROLLBACK);
glutAddSubMenu("difficulty", mainWindowSubmenu1);
glutAddMenuEntry("save board", MENU_CHOICE_SAVE);
glutAddMenuEntry("load board", MENU_CHOICE_LOAD);
glutAddMenuEntry("music", MENU_CHOICE_MUSIC);
glutAddMenuEntry("exit", MENU_CHOICE_EXIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(xsize, ysize);
glutInitWindowPosition(680, 178);
glutCreateWindow("Roger's gomoku");
glewInit();
init();
//默认播放音乐
music('p');
mainWindowSetupMenu();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(mainWindowMouse);
glutIdleFunc(idle);
glutMainLoop();
//关闭音乐
music('s');
return 0;
}