-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab.c
More file actions
244 lines (209 loc) · 6.24 KB
/
lab.c
File metadata and controls
244 lines (209 loc) · 6.24 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
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
#include </usr/include/GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>
#define N 20
void cargaMatriz(int matriz[N][N]);
void prueba(void);
void pared(int x, int y, int r, int g, int b);
void estado(int x, int y, int tX, int tY);
int search(int [2],int);
void push(int[2]);
int sonIguales(int[2],int[2]);
void pop(void);
bool var;
int stck=100;
int cont=0;
int stack[100][2];
int matriz[N][N];
int inicio[2] = {0,0};// el inicio estará en el origen del laberinto
int salida[2] = {9,2}; //la salida estará en la esquina que se encuentra en la diagonal
int tx = 0;
int ty = 0;
void display() //Funcion de dibujado
{
int i;
int j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (matriz[i][j] == 1)
pared(j,i,0,0,0);
else
pared(j,i,1,1,1);
}
}
estado(inicio[1],inicio[0],ty,tx);
glFlush(); //Forza el limpiado de pantalla
}
int search(int vector[2],int profundidad){
int aux[2];
push(vector);
profundidad++;
printf("vector:%d %d\n",vector[0],vector[1]);
printf("%d\n",sonIguales(vector,salida));
if(sonIguales(vector,salida)==2){
return 1;
}
//copiar(matriz,aux);
//imprimir(aux);
if(profundidad<stck){
int x=vector[0];
int y=vector[1];
if(y>0 && matriz[x][y-1]==0){
printf("izq\n");
aux[0]=x;
aux[1]=y-1;
tx=aux[1];
ty=aux[0];
//glutPostRedisplay();
if(search(aux,profundidad)==1)return 1;
}
if(y<19 && matriz[x][y+1]==0){
printf("der\n");
aux[0]=x;
aux[1]=y+1;
tx=aux[0];
ty=aux[1];
//glutPostRedisplay();
if(search(aux,profundidad)==1)return 1;
//else imprimir(matriz);
}
if(x>0 && matriz[x-1][y]==0){
printf("abj\n");
aux[0]=x-1;
aux[1]=y;
tx=aux[0];
ty=aux[1];
//glutPostRedisplay();
if(search(aux,profundidad)==1)return 1;
}
if(x<19 && matriz[x+1][y]==0){
printf("arr\n");
aux[0]=x+1;
aux[1]=y;
tx=aux[0];
ty=aux[1];
//glutPostRedisplay();
if(search(aux,profundidad)==1)return 1;
}
}
pop();
return 0;
}
int sonIguales(int mat1[2],int mat2[2]){
int var=0;
for (int j = 0; j < 2; ++j){
if(mat1[j]==mat2[j])var++;
}
return var;
}
void push(int ele[2]){
for (int i = 0; i < 2; ++i){
stack[cont][i]=ele[i];
}
cont++;
}
void pop(){
cont--;
for (int j = 0; j < 3; ++j){
stack[cont][j]=50;
}
}
void initstack(){
for (int i = 0; i < 30; ++i){
for (int j = 0; j < 2; ++j){
stack[i][j]=50;
}
}
}
void init()
{
glClearColor(1.0,1.0,1.0,0.0); //Limpiar Pantalla
}
void reshape(int width, int height)
{
glClear(GL_COLOR_BUFFER_BIT); //Buffer
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //Genera proyeccion
glLoadIdentity();
glOrtho(0.0,N,0.0,N,-1.0,1.0); //Perspectiva
glMatrixMode(GL_MODELVIEW); //Tipo de Proyeccion
}
int main(int argc, char ** argv)
{
cargaMatriz(matriz);//se llena la forma del laberinto mediante una matriz binaria
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA); //Funcion Main
glutInitWindowPosition(100,100); //Posicion de Ventana
glutInitWindowSize(500,500); //Cambiar Tamaño de Ventana
glutCreateWindow("Inteligencia Artificial - Laberinto - Instituto Tecnologico de Oaxaca"); //Crea y cambia el nombre de la ventana
glutDisplayFunc(display); //Funcion de Dibujado anteriormente creada
glutReshapeFunc(reshape);
glutTimerFunc(10,prueba,0);
glutMainLoop(); //Loop
return 0;
}
void prueba(){
search(inicio,0);
glutPostRedisplay(); //Tell GLUT that the scene has changed
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(5,search, 0);
}
void cargaMatriz(int matriz[N][N]){
int i;
int j;
int matriz1[N][N] = { //el laberinto está dado por una mtriz binaria en donde 0 indica que es un / //pasillo y 0 que es un muro.
{0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,1},
{0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1},
{0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0},
{1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1},
{0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,0,1,0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1},
{0,1,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1,0},
{1,1,1,1,0,1,1,1,0,1,0,0,0,0,1,0,0,0,1,0},
{0,0,0,0,0,1,0,1,0,0,0,1,1,0,1,0,1,1,1,0},
{0,1,1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,0,0,0},
{0,1,0,0,0,1,0,1,0,0,1,1,1,1,1,1,1,0,1,1},
{0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,1,1,1,1,0,1,1,1,1,1,1,1,0},
{0,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0},
{0,0,0,0,0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1},
{0,1,1,1,0,1,0,1,0,1,0,1,1,0,1,0,1,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,1,1,1,0}
};
for (i = 0 ; i< N; i ++)
for (j = 0 ; j< N; j ++)
{
matriz[j][i] = matriz1[j][i];
}
}
void pared(int x, int y, int r, int g, int b)
{
glBegin(GL_QUADS); //Modelo a Dibujar
glColor3f(r,g,b);
glVertex3f(x,y,0.0);
glVertex3f(x + 1,y,0.0);
glVertex3f(x + 1,y + 1,0.0);
glVertex3f(x,y + 1,0.0);
glEnd(); //Finaliza el dibujo
}
void estado(int x, int y, int tX, int tY)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColor3f(1.0,1.0,0.0);
glPointSize(10.0f);
glBegin(GL_POINTS);
glVertex2f(x +0.5 + tX,y + 0.5 +tY);
glEnd();
glTranslatef(tX,tY,0.0f);
glPopMatrix();
}