Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glut.h>
- #include <iostream>
- #include<cstdlib>
- #include <time.h>
- using namespace std;
- #define len 8.0
- int a[int(len)][int(len)] = {{0,0,0,0,9,9,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0},
- {0,0,9,0,0,0,0,0},
- {0,9,0,0,0,0,9,9},
- {0,0,9,0,0,0,9,0},
- {0,0,0,0,0,0,0,9},
- {0,0,0,0,0,0,0,9}};
- char b[int(len)][int(len)];
- int minesnum = 10;
- float step = 1 / len, delta = 0.005;
- void dispPress(int x, int y) {
- glColor3f(0.95,0.95,0.95);
- glBegin(GL_TRIANGLES);
- glVertex3f(y*step+delta,(len-x)*step-delta,0);
- glVertex3f((y+1)*step-delta,(len-x)*step-delta,0);
- glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0);
- glEnd();
- glColor3f(0.9,0.9,0.9);
- glBegin(GL_TRIANGLES);
- glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0);
- glVertex3f(y*step+delta,(len-x-1)*step+delta,0);
- glVertex3f(y*step+delta,(len-x)*step-delta,0);
- glEnd();
- glFlush();
- }
- void dispBlank(int x, int y) {
- glColor3f(0.75,0.75,0.75);
- glBegin(GL_POLYGON);
- glVertex3f(y*step+delta,(len-x)*step-delta,0);
- glVertex3f((y+1)*step-delta,(len-x)*step-delta,0);
- glVertex3f((y+1)*step-delta,(len-x-1)*step+delta,0);
- glVertex3f(y*step+delta,(len-x-1)*step+delta,0);
- glEnd();
- }
- void dispLeft(int x, int y) {
- int sum = 0;
- if(x-1 >= 0)
- sum += a[x-1][y];
- if(x+1 < len)
- sum += a[x+1][y];
- if(y-1 >= 0)
- sum += a[x][y-1];
- if(y+1 < len)
- sum += a[x][y+1];
- if(x-1 >= 0 && y+1 < len)
- sum += a[x-1][y+1];
- if(x+1 < len && y+1 < len)
- sum += a[x+1][y+1];
- if(x-1 >= 0 && y-1 >= 0)
- sum += a[x-1][y-1];
- if(x+1 < len && y-1 >= 0)
- sum += a[x+1][y-1];
- int num = sum / 9;
- b[x][y] = 48 + num;
- if(num==9) {
- glColor3f(0,0,0.5);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- }
- switch(num) {
- case 0:
- dispBlank(x, y);
- break;
- case 1:
- glColor3f(0,0,1);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 2:
- glColor3f(0,1,0);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 3:
- glColor3f(0,1,1);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 4:
- glColor3f(1,0,0);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 5:
- glColor3f(1,0,1);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 6:
- glColor3f(1,1,0);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 7:
- glColor3f(1,1,1);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- case 8:
- glColor3f(0.5,0.5,0.5);
- glBegin(GL_POLYGON);
- glVertex3f(y*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x)*step,0);
- glVertex3f((y+1)*step,(len-x-1)*step,0);
- glVertex3f(y*step,(len-x-1)*step,0);
- glEnd();
- break;
- }
- if(num==0) {
- if(x-1 >= 0 && (b[x-1][y] == '*' || b[x-1][y] == '9'))
- dispLeft(x-1,y);
- if(x+1 < len && (b[x+1][y] == '*' || b[x+1][y] == '9'))
- dispLeft(x+1,y);
- if(y-1 >= 0 && (b[x][y-1] == '*' || b[x][y-1] == '9'))
- dispLeft(x,y-1);
- if(y+1 < len && (b[x][y+1] == '*' || b[x][y+1] == '9'))
- dispLeft(x,y+1);
- if(x-1 >= 0 && y+1 < len && (b[x-1][y+1] == '*' || b[x-1][y+1] == '9'))
- dispLeft(x-1,y+1);
- if(x+1 < len && y+1 < len && (b[x+1][y+1] == '*' || b[x+1][y+1] == '9'))
- dispLeft(x+1,y+1);
- if(x-1 >= 0 && y-1 >= 0 && (b[x-1][y-1] == '*' || b[x-1][y-1] == '9'))
- dispLeft(x-1,y-1);
- if(x+1 < len && y-1 >= 0 && (b[x+1][y-1] == '*' || b[x+1][y-1] == '9'))
- dispLeft(x+1,y-1);
- }
- glFlush();
- }
- void display() {
- glClear (GL_COLOR_BUFFER_BIT);
- glColor3f (0.0, 0.0, 0.0);
- glLineWidth (3);
- float i;
- for(i = step; i< 1; i+=step) {
- glBegin(GL_LINES);
- glVertex3f(i,0,0);
- glVertex3f(i,1.0,0);
- glEnd();
- glBegin(GL_LINES);
- glVertex3f(0,i,0);
- glVertex3f(1.0,i,0);
- glEnd();
- }
- glFlush ();
- }
- void onMouse(int button, int state, int x, int y) {
- x = x / (500 / len);
- y = y / (500 / len);
- // cout<<y<<"\t"<<x;
- if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
- dispPress(y,x);
- if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
- dispLeft(y,x);
- }
- void reshape (int width, int height) {
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, width, 0, height, -1.0, 1.0);
- glFlush();
- }
- void init (void) {
- /* select clearing (background) color */
- glClearColor (1.0, 1.0, 1.0, 0.0);
- /* initialize viewing values */
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize (500, 500);
- glutInitWindowPosition (400, 300);
- glutCreateWindow ("MineSweeper");
- init ();
- // srand(time(NULL));
- int i,j,x,y;
- for (i = 0;i<len;i++)
- for(j=0;j<len;j++)
- b[i][j]='*';
- /* for (i =0 ;i<minesnum;i++) {
- ranst:
- x = rand()%8;
- y = rand()%8;
- if(a[x][y] != 9)
- a[x][y] = 9;
- else
- goto ranst;
- }
- */
- glutDisplayFunc(display);
- // glutReshapeFunc(reshape);
- // display();
- // dispLeft(6,6);
- // reshape();
- glutMouseFunc(onMouse);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement