-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgemgame.cpp
More file actions
207 lines (182 loc) · 4.73 KB
/
gemgame.cpp
File metadata and controls
207 lines (182 loc) · 4.73 KB
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
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
// Needed on MsWindows
#include <windows.h>
#endif // Win32 platform
#include <GLUT/glut.h>
#include <stdio.h>
#include <iostream>
#include <time.h>
int gemarray[10][10]; //gem type at each location
// generate a 'random' array
void makeArray()
{
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
gemarray[i][j] = rand()%3;
}
}
// Based on array of gem values, draws gems on screen
void drawShapes()
{
double x,y; //coordinates to start drawing from
int type;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for(int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
//Lower left corner
x = -1+(j)*0.2;
y = 0.8-(i)*0.2;
type = gemarray[i][j];
switch(type)
{
case 0:
glBegin(GL_TRIANGLE_FAN); //triangle fan - pentagon
glColor3d(1.0, 0.0, 1.0); //purple
glVertex2d(x+0.1, y+0.1);
glColor3d(0.0, 0.0, 0.2);
glVertex2d(x+0.1, y+0.19);
glVertex2d(x+0.02,y+0.13);
glVertex2d(x+0.05,y+0.01);
glVertex2d(x+0.15,y+0.01);
glVertex2d(x+0.18, y+0.13);
glVertex2d(x+0.1, y+0.19);
glEnd();
break;
case 1:
glBegin(GL_TRIANGLE_FAN); //triangle fan - triangle
glColor3d(0.0, 1.0, 0.0); //green
glVertex2d(x+0.1, y+0.1);
glColor3d(0.0, 0.0, 0.2);
glVertex2d(x+0.1, y+0.19);
glVertex2d(x+0.01,y+0.01);
glVertex2d(x+0.19,y+0.01);
glVertex2d(x+0.1, y+0.19);
glEnd();
break;
case 2:
glBegin(GL_TRIANGLE_FAN); //triangle fan - diamond
glColor3d(1.0, 0.0, 0.0); //red
glVertex2d(x+0.1, y+0.1);
glColor3d(0.0, 0.0, 0.2);
glVertex2d(x+0.1, y+0.19);
glVertex2d(x+0.01,y+0.1);
glVertex2d(x+0.1,y+0.01);
glVertex2d(x+0.19, y+0.1);
glVertex2d(x+0.1, y+0.19);
glEnd();
break;
}
}
}
glutSwapBuffers();
}
// recursively finds all connected gems of 'type'
// changes all the gems in the group to type 'changeto'
int changeNeighbors(int type, int i, int j, int changeto)
{
if (i > 9 || i < 0 || j > 9 || j < 0 || type != gemarray[i][j])
return 0;
else
{
gemarray[i][j] = changeto;
return 1 + changeNeighbors(type, i, j+1, changeto) +
changeNeighbors(type, i+1, j, changeto) + changeNeighbors(type, i-1, j, changeto)
+ changeNeighbors(type, i, j-1, changeto);
}
}
// moves all the gems 'down' to fill empty spaces marked with -1
// replaces all spaces above gems to -2
void moveDown()
{
int *dropto, *dropfrom, *top, *bottom = NULL;
for (int j = 0; j <=9; j++) //left to right
{
top = &gemarray[0][j];
bottom = &gemarray[9][j];
dropto = dropfrom = top;
for (dropto = top; dropto < bottom+1; dropto+=10)
{ // move downwards until find empty space
if (*dropto == -1)
{
for(dropfrom = dropto; dropfrom > top-1; dropfrom-= 10)
{ // look upwards from dropto until find a gem
if(*dropfrom != -1)
break;
}
int *to = dropto;
int *from = dropfrom;
while (from > top-1) // move everything down from dropfrom to dropto
{
*to = *from;
to-=10;
from-=10;
} // to is now at the topmost gem
while (to > top-1) // changes empty spaces above gem to -2
{
*to = -2;
to-= 10;
}
}
}
}
}
// On click detects connecting gems, removes them, shifts everything else down
void onClick(int button, int state, int x, int y)
{
if(state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
{
// find location in array
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
int j = floor((double)x/w*10);
int i = floor((double)y/h*10);
int type = gemarray[i][j];
if (type == -1) // do nothing if clicked on empty space
return;
// change all connecting gems of same type to -1
int count = changeNeighbors(type, i, j, -1);
if (count < 4) // if less than 4 were changed, change everything back
changeNeighbors(-1, i, j, type);
else
moveDown();
drawShapes();
}
}
// recursively finds all connected gems of 'type'
// changes all the gems in the group to type 'changeto'
void onDisplay()
{
makeArray();
drawShapes();
}
// resets gems upon pressing 'r' key
void onKeyboard(unsigned char key, int x, int y) //parameter
{
if (key == 'r')
{
makeArray();
glutPostRedisplay(); //refresh the screen
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
glutInitDisplayMode(
GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //RGBA if you want an alpha channel for transparency
glutCreateWindow("GEM GAME");
// Register event handlers here
glutKeyboardFunc(onKeyboard);
glutDisplayFunc(onDisplay);
glutMouseFunc(onClick);
glutMainLoop();
return 0;
}