-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgraphics.cpp
289 lines (235 loc) · 7.18 KB
/
graphics.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
/*
* Copyright (c) 2009, 2014 University of Michigan, Ann Arbor.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of Michigan, Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <cmath>
#include <string>
#include "graphics.h"
#include "connect4.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// window width and height
GLdouble width, height;
// mouse x and y
int mouseX, mouseY;
// GLUT window handle
int wd;
// Connect4 Board and Result
Board board;
Result r;
void init(void)
{
// initial window width and height,
// within which we draw. (0,0) is the lower left corner
width = 600.0;
height = 600.0;
mouseX = 0;
mouseY = 0;
r = NoResult;
return;
}
void drawCircle(int x, int y, int radius) {
double theta = M_PI/20.0;
glBegin(GL_TRIANGLE_FAN);
glVertex2i(x, y);
for (double i = 0; i < 2*M_PI+theta; i += theta) {
glVertex2i(x+radius*cos(i), y+radius*sin(i));
}
glEnd();
}
void display(void)
{
// tell OpenGL to use the whole window for drawing
glViewport(0, 0, width, height);
// do an orthographic parallel projection with the coordinate
// system set to first quadrant, limited by screen/window size
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, 0.0, height, -1.f, 1.f);
int side_border = 25;
int bottom_border = 50;
int top_border = 75;
// clear the screen */
glClear(GL_COLOR_BUFFER_BIT);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_QUADS);
glVertex2i(side_border,bottom_border);
glVertex2i(width-side_border,bottom_border);
glVertex2i(width-side_border,height-top_border);
glVertex2i(side_border,height-top_border);
glEnd();
// Begin solution
glColor3f(1.0, 1.0, 1.0);
int increment = 75;
int radius = 25;
for (int row = 1; row <= NUM_ROWS; ++row) {
for (int col = 1; col <= NUM_COLS; ++col) {
PieceType location = board.atLocation(row-1, col-1);
if (location == Empty) {
glColor3f(1.0, 1.0, 1.0);
} else if (location == Player1) {
glColor3f(0.0, 0.0, 0.0);
} else {
glColor3f(1.0, 0.0, 0.0);
}
drawCircle((col*increment), side_border+(row*increment), radius);
}
}
glColor3f(0.0, 0.0, 0.0);
for (int col = 1; col <= NUM_COLS; ++col) {
glRasterPos2i(col*increment-5, 20);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, char('0'+col));
}
if(r == Win){
std::string win_message;
if(board.toMove() == 1) {
win_message = "Red won!";
glColor3f(1.0, 0.0, 0.0);
}
else {
win_message = "Black won!";
glColor3f(0.0, 0.0, 0.0);
}
glRasterPos2i(side_border, height-side_border);
for (int s = 0; s < win_message.length(); ++s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, win_message[s]);
}
} else if (r == Draw) {
std::string draw = "Draw!";
glColor3f(0.5, 0.0, 0.0);
glRasterPos2i(side_border, height-side_border);
for (int s = 0; s < draw.length(); ++s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, draw[s]);
}
} else if (board.toMove() == 1) {
glColor3f(0.0, 0.0, 0.0);
glRasterPos2i(side_border, height-side_border);
std::string message = "Black, enter a move.";
for (int s = 0; s < message.length(); ++s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[s]);
}
} else {
glColor3f(1.0, 0.0, 0.0);
glRasterPos2i(side_border, height-side_border);
std::string message = "Red, enter a move.";
for (int s = 0; s < message.length(); ++s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[s]);
}
}
if (r == IllegalMove) {
std::string draw = "Illegal Move!";
glColor3f(0.5, 0.0, 0.0);
glRasterPos2i(side_border, height-bottom_border);
for (int s = 0; s < draw.length(); ++s) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, draw[s]);
}
}
// End solution
// force drawing to start
glutSwapBuffers();
return;
}
void reshape(int w, int h)
{
glutPostRedisplay();
return;
}
void refresh(void)
{
glutPostRedisplay();
return;
}
void kbd(unsigned char key, int x, int y)
{
// escape
if (key == 27) {
glutDestroyWindow(wd);
exit(0);
}
if (key >= '1' && key <= '7' && r != Win && r != Draw) {
r = board.makeMove(key-1-'0');
}
else if (r != Win && r != Draw) {
r = IllegalMove;
}
glutPostRedisplay();
return;
}
void cursor(int x, int y)
{
glutPostRedisplay();
return;
}
void drag(int x, int y)
{
glutPostRedisplay();
return;
}
void mouse(int button, int state, int x, int y)
{
glutPostRedisplay();
return;
}
int graphicsPlay(int argc, char *argv[])
{
// perform initialization NOT OpenGL/GLUT dependent,
// as we haven't created a GLUT window yet
init();
// initialize GLUT, let it extract command-line
// GLUT options that you may provide
// - NOTE THE '&' BEFORE argc
glutInit(&argc, argv);
// specify the display to be single
// buffered and color as RGBA values
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
// set the initial window size
glutInitWindowSize((int) width, (int) height);
// create the window and store the handle to it
wd = glutCreateWindow("Connect 4");
// --- register callbacks with GLUT ---
// register function that draws in the window
glutDisplayFunc(display);
// register function to handle window resizes
glutReshapeFunc(reshape);
// register the provided refresh()
// function to call when system is idle
glutIdleFunc(refresh);
// register keyboard press event processing function
glutKeyboardFunc(kbd);
// register cursor moved event callback
glutPassiveMotionFunc(cursor);
// register mouse clicked event callback
glutMouseFunc(mouse);
// register mouse dragged event callback
glutMotionFunc(drag);
// Set up initial OpenGL context
// clear color buffer to white
glClearColor(1.0, 1.0, 1.0, 0.0);
// start the GLUT main loop
glutMainLoop();
return 0;
}