Maze Paths

My program simulates a standard maze. After it runs, you would be asked the maze’s size; by entering an odd integer which signifies the number of rows and columns you would like. You are able to make it any size you wish.
You press the ‘u’, ‘d’, ‘r’, and ‘l’ keys to move the sphere, which is the marker.

My problem is it doesn't leave any open areas on the edges to create an entrance and an exit. I'm not sure how that is done. Can anyone help?
Here is the code:
#include <iostream>
using namespace std;
#include <glut/glut.h>

int m[31][31], n;
static int flag=1;
int X=0, Y=1, Z=0, wire=0;
GLfloat x=0.,y=0.,z=0., spin=0., x1=1., yy1=1., z1=1.;
GLfloat a=0, b=0, c=0, d=0, e=0, f=0;
//notice y1 is a reserved name, so I used yy1 instead
GLfloat mat_ambient[]={.8,.8,.8, 1.0};
GLfloat mat_diffuse[]={1.,.5,.0, 1.0};
GLfloat mat_diffuse2[]={1.,.2,.0,1.0};
GLfloat mat_diffuse3[]={1.,.7,.0,1.0};
GLfloat mat_specular[]={1.,1.,.5, 1.0};
GLfloat mat_shininess[]={128.}; //this needs specular (work together)
GLfloat light0_position[]={1., 1., -5., .0};


void Ball(int h, int k, double g)
{
glPushMatrix();
glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_diffuse2);
glTranslatef((a+x)+(h*g),(b+y)-(k*g),c);
glutSolidSphere(.023,64,16);
glPopMatrix();
}

void Goal(int h, int k, double g)
{
glPushMatrix();
glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_diffuse3);
glTranslatef((d+x)+(h*g),(e+y)-(k*g),f);
glutSolidSphere(.023,64,16);
glPopMatrix();
}

void Design()
{
double rad =.055;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(m[i][j]==1)
{
glPushMatrix();
glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_diffuse);
glTranslatef(x+(i*rad),y-(j*rad),z);
glRotatef(0,X,Y,Z);
glScalef(x1,yy1,z1);
glutSolidCube(.055);
glPopMatrix();

}
if(i==1&&j==1)
{
Ball(i,j,rad);
}
/* if(i==17&&j==13)
{
Goal(i,j,rad);
} */

}
}
}

int in3Walls(int a,int b)
{ int w = m[a+1][b] + m[a-1][b] + m[a][b+1] + m[a][b-1];
if (w > 2)
return 1;
else return 0; }

void MYdesignMaze()
{for (int a = 1; a < n-1; a++) for (int b = 1; b < n-1; b++)
if (in3Walls(a, b)) //design maze
{
if (rand() % 50 > 38)
{ if (a<n-2) m[a + 1][b] = 0;}
else
{ if (b<n-2) m[a][b + 1] = 0;}
}
}
void printMaze()
{
cout << " ";
for (int j = 0; j < n; j++)
if (j < 10)cout << " " << j;
else cout << j; cout << endl; //print top line indices
for (int i = 0; i < n; i++)
{ if (i < 10)cout << " " << i;
else cout << i; //print left column indices
for (int j = 0; j < n; j++)
if (m[i][j]) cout << " *"; else cout << " "; cout << endl;
}
}


void initMaze()
{ for (int a = 0; a < n; a++)
for (int b = 0; b < n; b++)
if (a % 2 && b % 2) m[a][b] = 0;
else m[a][b] = 1;
}

void init(void)
{ glClearColor(.4,.8,.5,.5); //background

glShadeModel(GL_FLAT); //default: SMOOTH
//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
//glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);

glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light0_position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_diffuse);
glLightfv(GL_LIGHT0,GL_DIFFUSE,mat_diffuse2);
glLightfv(GL_LIGHT0,GL_SPECULAR,mat_ambient);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
cout<<"\nUse:\n '?' 'x' 'y' 'z' 'a' 'b' 'c' 'w' 's' 'S' ' ' keys for animation\n"<<endl;
cout << "Enter an odd n: ";
cin >> n;
initMaze(); printMaze(); MYdesignMaze(); printMaze();
glEnable(GL_DEPTH_TEST);


}


void display(void)
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Design();

glutSwapBuffers();
}

void spinDisplay()
{ spin+=1.; if(spin>360.)spin=0.;
glutPostRedisplay();
}

void keyboard(unsigned char key, int xx, int yy)
{ switch(key)
{
case '?' : cout<<"\nUse:\n '?' 'x' 'y' 'z' 'a' 'b' 'c' 'w' 's' 'S' ' ' keys for animation\n"<<endl; break;
case 'x' : X=1; Y=Z=0; break;
case 'y' : Y=1; X=Z=0; break;
case 'z' : Z=1; X=Y=0; break;
case 'a' : x+=.01; break;
case 'b' : y+=.01; break;
case 'c' : z+=.01; break;
case 'A' : x-=.01; break;
case 'B' : y-=.01; break;
case 'C' : z-=.01; break;

case 'r' : a+=.055; break;
case 'u' : b+=.055; break;
// case 'f' : c+=.055; break;
case 'l' : a-=.055; break;
case 'd' : b-=.055; break;
// case 'F' : c-=.03; break;


case 'h': x1 += .1; break;
case 'v': yy1 += .1; break;
case 'p': z1 += .1; break;
case 'H': x1 -= .1; break;
case 'V': yy1 -= .1; break;
case 'D': z1 -= .1; break;

case ' ' : spin=x=y=z=.0; break;
case 'w' : wire= ++wire%2; break;
case 's' : glShadeModel(GL_SMOOTH); break;
case 'S' : glShadeModel(GL_FLAT); break;
}
glutPostRedisplay();
}

void mouse(int button, int state, int xx, int yy)
{glutIdleFunc(spinDisplay);
switch(button)
{
case GLUT_LEFT_BUTTON : if(state == GLUT_DOWN) {X=1; Y=Z=0;} break;
case GLUT_RIGHT_BUTTON : if(state == GLUT_UP) glutIdleFunc(NULL);
}
glutPostRedisplay();
}

void reshape(int w, int h)
{ glViewport(0.,0.,900.,900.);
glLoadIdentity();
gluPerspective(80., (GLfloat) w/(GLfloat) h, .1, 190.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); //important: last line must be glLoadIdentity() for coloring with openGL
//glTranslatef (.0, .0, -1.); //do not translate at all if glLoadIdentity() is used
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (900, 900); glutInitWindowPosition(0,0);
glutCreateWindow("Symposium Project");
init();
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);
glutMainLoop();}

Last edited on
Topic archived. No new replies allowed.