SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <GL/gl.h> | |
| 2 | #include <GL/glut.h> | |
| 3 | #include <iostream> | |
| 4 | #include<cstdlib> | |
| 5 | #include <time.h> | |
| 6 | ||
| 7 | using namespace std; | |
| 8 | ||
| 9 | #define len 8.0 | |
| 10 | int a[int(len)][int(len)] = {{0,0,0,0,9,9,0,0},
| |
| 11 | - | {0,0,0,0,0,0,0,0},
|
| 11 | + | {0,0,0,0,0,0,0,0},
|
| 12 | - | {0,0,0,0,0,0,0,0},
|
| 12 | + | {0,0,0,0,0,0,0,0},
|
| 13 | - | {0,0,9,0,0,0,0,0},
|
| 13 | + | {0,0,9,0,0,0,0,0},
|
| 14 | - | {0,9,0,0,0,0,9,9},
|
| 14 | + | {0,9,0,0,0,0,9,9},
|
| 15 | - | {0,0,9,0,0,0,9,0},
|
| 15 | + | {0,0,9,0,0,0,9,0},
|
| 16 | - | {0,0,0,0,0,0,0,9},
|
| 16 | + | {0,0,0,0,0,0,0,9},
|
| 17 | - | {0,0,0,0,0,0,0,9}};
|
| 17 | + | {0,0,0,0,0,0,0,9}};
|
| 18 | char b[int(len)][int(len)]; | |
| 19 | int minesnum = 10; | |
| 20 | float step = 1 / len, delta = 0.005; | |
| 21 | ||
| 22 | void dispPress(int x, int y) {
| |
| 23 | ||
| 24 | glColor3f(0.95,0.95,0.95); | |
| 25 | glBegin(GL_TRIANGLES); | |
| 26 | glVertex3f(y*step+delta,(len-x)*step-delta,0); | |
| 27 | glVertex3f((y+1)*step-delta,(len-x)*step-delta,0); | |
| 28 | glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0); | |
| 29 | glEnd(); | |
| 30 | ||
| 31 | glColor3f(0.9,0.9,0.9); | |
| 32 | glBegin(GL_TRIANGLES); | |
| 33 | glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0); | |
| 34 | glVertex3f(y*step+delta,(len-x-1)*step+delta,0); | |
| 35 | glVertex3f(y*step+delta,(len-x)*step-delta,0); | |
| 36 | glEnd(); | |
| 37 | ||
| 38 | glFlush(); | |
| 39 | } | |
| 40 | ||
| 41 | void dispBlank(int x, int y) {
| |
| 42 | glColor3f(0.75,0.75,0.75); | |
| 43 | glBegin(GL_POLYGON); | |
| 44 | glVertex3f(y*step+delta,(len-x)*step-delta,0); | |
| 45 | glVertex3f((y+1)*step-delta,(len-x)*step-delta,0); | |
| 46 | glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0); | |
| 47 | glVertex3f(y*step+delta,(len-x-1)*step+delta,0); | |
| 48 | glEnd(); | |
| 49 | } | |
| 50 | ||
| 51 | void dispLeft(int x, int y) {
| |
| 52 | ||
| 53 | int sum = 0; | |
| 54 | ||
| 55 | if(x-1 >= 0) | |
| 56 | sum += a[x-1][y]; | |
| 57 | ||
| 58 | if(x+1 < len) | |
| 59 | sum += a[x+1][y]; | |
| 60 | ||
| 61 | if(y-1 >= 0) | |
| 62 | sum += a[x][y-1]; | |
| 63 | ||
| 64 | if(y+1 < len) | |
| 65 | sum += a[x][y+1]; | |
| 66 | ||
| 67 | if(x-1 >= 0 && y+1 < len) | |
| 68 | sum += a[x-1][y+1]; | |
| 69 | ||
| 70 | if(x+1 < len && y+1 < len) | |
| 71 | sum += a[x+1][y+1]; | |
| 72 | ||
| 73 | if(x-1 >= 0 && y-1 >= 0) | |
| 74 | sum += a[x-1][y-1]; | |
| 75 | ||
| 76 | if(x+1 < len && y-1 >= 0) | |
| 77 | sum += a[x+1][y-1]; | |
| 78 | ||
| 79 | int num = sum / 9; | |
| 80 | b[x][y] = 48 + num; | |
| 81 | ||
| 82 | if(num==9) {
| |
| 83 | glColor3f(0,0,0.5); | |
| 84 | glBegin(GL_POLYGON); | |
| 85 | glVertex3f(y*step,(len-x)*step,0); | |
| 86 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 87 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 88 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 89 | glEnd(); | |
| 90 | } | |
| 91 | ||
| 92 | switch(num) {
| |
| 93 | case 0: | |
| 94 | dispBlank(x, y); | |
| 95 | break; | |
| 96 | case 1: | |
| 97 | glColor3f(0,0,1); | |
| 98 | glBegin(GL_POLYGON); | |
| 99 | glVertex3f(y*step,(len-x)*step,0); | |
| 100 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 101 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 102 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 103 | glEnd(); | |
| 104 | break; | |
| 105 | case 2: | |
| 106 | glColor3f(0,1,0); | |
| 107 | glBegin(GL_POLYGON); | |
| 108 | glVertex3f(y*step,(len-x)*step,0); | |
| 109 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 110 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 111 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 112 | glEnd(); | |
| 113 | break; | |
| 114 | case 3: | |
| 115 | glColor3f(0,1,1); | |
| 116 | glBegin(GL_POLYGON); | |
| 117 | glVertex3f(y*step,(len-x)*step,0); | |
| 118 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 119 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 120 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 121 | glEnd(); | |
| 122 | break; | |
| 123 | case 4: | |
| 124 | glColor3f(1,0,0); | |
| 125 | glBegin(GL_POLYGON); | |
| 126 | glVertex3f(y*step,(len-x)*step,0); | |
| 127 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 128 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 129 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 130 | glEnd(); | |
| 131 | break; | |
| 132 | case 5: | |
| 133 | glColor3f(1,0,1); | |
| 134 | glBegin(GL_POLYGON); | |
| 135 | glVertex3f(y*step,(len-x)*step,0); | |
| 136 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 137 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 138 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 139 | glEnd(); | |
| 140 | break; | |
| 141 | case 6: | |
| 142 | glColor3f(1,1,0); | |
| 143 | glBegin(GL_POLYGON); | |
| 144 | glVertex3f(y*step,(len-x)*step,0); | |
| 145 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 146 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 147 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 148 | glEnd(); | |
| 149 | break; | |
| 150 | case 7: | |
| 151 | glColor3f(1,1,1); | |
| 152 | glBegin(GL_POLYGON); | |
| 153 | glVertex3f(y*step,(len-x)*step,0); | |
| 154 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 155 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 156 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 157 | glEnd(); | |
| 158 | break; | |
| 159 | case 8: | |
| 160 | glColor3f(0.5,0.5,0.5); | |
| 161 | glBegin(GL_POLYGON); | |
| 162 | glVertex3f(y*step,(len-x)*step,0); | |
| 163 | glVertex3f((y+1)*step,(len-x)*step,0); | |
| 164 | glVertex3f((y+1)*step,(len-x-1)*step,0); | |
| 165 | glVertex3f(y*step,(len-x-1)*step,0); | |
| 166 | glEnd(); | |
| 167 | break; | |
| 168 | } | |
| 169 | ||
| 170 | if(num==0) {
| |
| 171 | if(x-1 >= 0 && (b[x-1][y] == '*' || b[x-1][y] == '9')) | |
| 172 | dispLeft(x-1,y); | |
| 173 | ||
| 174 | if(x+1 < len && (b[x+1][y] == '*' || b[x+1][y] == '9')) | |
| 175 | dispLeft(x+1,y); | |
| 176 | ||
| 177 | if(y-1 >= 0 && (b[x][y-1] == '*' || b[x][y-1] == '9')) | |
| 178 | dispLeft(x,y-1); | |
| 179 | ||
| 180 | if(y+1 < len && (b[x][y+1] == '*' || b[x][y+1] == '9')) | |
| 181 | dispLeft(x,y+1); | |
| 182 | ||
| 183 | if(x-1 >= 0 && y+1 < len && (b[x-1][y+1] == '*' || b[x-1][y+1] == '9')) | |
| 184 | dispLeft(x-1,y+1); | |
| 185 | ||
| 186 | if(x+1 < len && y+1 < len && (b[x+1][y+1] == '*' || b[x+1][y+1] == '9')) | |
| 187 | dispLeft(x+1,y+1); | |
| 188 | ||
| 189 | if(x-1 >= 0 && y-1 >= 0 && (b[x-1][y-1] == '*' || b[x-1][y-1] == '9')) | |
| 190 | dispLeft(x-1,y-1); | |
| 191 | ||
| 192 | if(x+1 < len && y-1 >= 0 && (b[x+1][y-1] == '*' || b[x+1][y-1] == '9')) | |
| 193 | dispLeft(x+1,y-1); | |
| 194 | } | |
| 195 | ||
| 196 | glFlush(); | |
| 197 | } | |
| 198 | ||
| 199 | void display() {
| |
| 200 | glClear (GL_COLOR_BUFFER_BIT); | |
| 201 | ||
| 202 | glColor3f (0.0, 0.0, 0.0); | |
| 203 | glLineWidth (3); | |
| 204 | ||
| 205 | float i; | |
| 206 | for(i = step; i< 1; i+=step) {
| |
| 207 | glBegin(GL_LINES); | |
| 208 | glVertex3f(i,0,0); | |
| 209 | glVertex3f(i,1.0,0); | |
| 210 | glEnd(); | |
| 211 | glBegin(GL_LINES); | |
| 212 | glVertex3f(0,i,0); | |
| 213 | glVertex3f(1.0,i,0); | |
| 214 | glEnd(); | |
| 215 | } | |
| 216 | ||
| 217 | glFlush (); | |
| 218 | } | |
| 219 | ||
| 220 | void onMouse(int button, int state, int x, int y) {
| |
| 221 | x = x / (500 / len); | |
| 222 | y = y / (500 / len); | |
| 223 | // cout<<y<<"\t"<<x; | |
| 224 | ||
| 225 | if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) | |
| 226 | dispPress(y,x); | |
| 227 | ||
| 228 | if(button == GLUT_LEFT_BUTTON && state == GLUT_UP) | |
| 229 | dispLeft(y,x); | |
| 230 | } | |
| 231 | ||
| 232 | void reshape (int width, int height) {
| |
| 233 | glViewport(0, 0, width, height); | |
| 234 | glMatrixMode(GL_PROJECTION); | |
| 235 | glLoadIdentity(); | |
| 236 | glOrtho(0, width, 0, height, -1.0, 1.0); | |
| 237 | ||
| 238 | glFlush(); | |
| 239 | } | |
| 240 | ||
| 241 | void init (void) {
| |
| 242 | /* select clearing (background) color */ | |
| 243 | glClearColor (1.0, 1.0, 1.0, 0.0); | |
| 244 | ||
| 245 | /* initialize viewing values */ | |
| 246 | glMatrixMode(GL_PROJECTION); | |
| 247 | glLoadIdentity(); | |
| 248 | glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); | |
| 249 | } | |
| 250 | ||
| 251 | int main(int argc, char** argv) {
| |
| 252 | glutInit(&argc, argv); | |
| 253 | glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); | |
| 254 | glutInitWindowSize (500, 500); | |
| 255 | glutInitWindowPosition (400, 300); | |
| 256 | glutCreateWindow ("MineSweeper");
| |
| 257 | init (); | |
| 258 | ||
| 259 | // srand(time(NULL)); | |
| 260 | int i,j,x,y; | |
| 261 | ||
| 262 | for (i = 0;i<len;i++) | |
| 263 | for(j=0;j<len;j++) | |
| 264 | b[i][j]='*'; | |
| 265 | ||
| 266 | /* for (i =0 ;i<minesnum;i++) {
| |
| 267 | ranst: | |
| 268 | x = rand()%8; | |
| 269 | y = rand()%8; | |
| 270 | if(a[x][y] != 9) | |
| 271 | a[x][y] = 9; | |
| 272 | else | |
| 273 | goto ranst; | |
| 274 | } | |
| 275 | */ | |
| 276 | ||
| 277 | glutDisplayFunc(display); | |
| 278 | // glutReshapeFunc(reshape); | |
| 279 | // display(); | |
| 280 | // dispLeft(6,6); | |
| 281 | // reshape(); | |
| 282 | glutMouseFunc(onMouse); | |
| 283 | glutMainLoop(); | |
| 284 | return 0; | |
| 285 | } |