-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab.js
402 lines (330 loc) · 12.1 KB
/
lab.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*Proyecto Laberinto
*Armando Fabian Gonzalez Valentin
Beatriz edolla Martinez
802-A
*/
//Matrices para el escenario
var laberinto = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,1,0,1,0,0,1,1,1,1,0,0,0,0,1],
[1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1],
[1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1],
[1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1],
[1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,1],
[1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1],
[1,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1],
[1,0,0,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,0,1],
[1,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1],
[1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];
var canvas;
var ctx;
var FPS = 100;
//ESCENARIO / TABLERO
var columnas = 25;
var filas = 25;
var escenario; //MATRIZ
//TAMAÑO DE LAS CASILLAS
var anchoT;
var altoT;
const muro = "#17202A";
const tierra = '#CACFD2';
//RUTA
var principio;
var fin;
//ARRAYS PARA GUARDAR LAS CASILLAS
var openSet = [];
var closedSet = [];
var camino = [];
var terminado = false;
var xf = columnas-2,yf = filas-2;
var boton = document.getElementById('ruta');
var matAux;
var cont=Math.floor(Math.random()*4); //se genera un mapa aleatorio
var id;
canvas = document.getElementById("escenario");
ctx = canvas.getContext('2d');
//SE CARGA EL MAPA
if (cont==1) {
matAux= laberinto;
inicializa();
}
if (cont==2) {
matAux= laberinto2;
inicializa();
}
if (cont==3) {
matAux= laberinto3;
inicializa();
}
//SE AGREGA EL EVENTO DEL BOTON
boton.addEventListener("click",function(){
window.location.reload();
});
//CREAMOS UN ARRAY 2D
function creaArray2D(f,c){
var obj = new Array(f);
for(a=0; a<f; a++){
obj[a] = new Array(c);
}
return obj;
}
//CALCULA LAS DISTANCIAS
function heuristica(a,b){
var x = Math.abs(a.x - b.x);
var y = Math.abs(a.y - b.y);
var dist = x+y;
return dist;
}
function borraDelArray(array,elemento){
for(i=array.length-1; i>=0; i--){
if(array[i] == elemento){
array.splice(i,1);
}
}
}
//CREA CASILLAS
function Casilla(x,y, s){
//POSICIÓN
this.x = x;
this.y = y;
//TIPO (PARED=1, CAMINO=0)
if(s == 0){
this.tipo = 0;
}
if(s == 1){
this.tipo = 1;
}
//PESOS
this.f = 0; //coste total (g+h)
this.g = 0; //pasos dados
this.h = 0; //heurística (estimación de lo que queda)
this.vecinos = [];
this.padre = null;
//MÉTODO QUE CALCULA SUS VECNIOS
this.addVecinos = function(){
if(this.x > 0)
this.vecinos.push(escenario[this.y][this.x-1]); //vecino izquierdo
if(this.x < filas-1)
this.vecinos.push(escenario[this.y][this.x+1]); //vecino derecho
if(this.y > 0)
this.vecinos.push(escenario[this.y-1][this.x]); //vecino de arriba
if(this.y < columnas-1)
this.vecinos.push(escenario[this.y+1][this.x]); //vecino de abajo
}
//MÉTODO QUE DIBUJA LA CASILLA
this.dibuja = function(){
var color;
if(this.tipo == 0){
color = tierra;
}
if(this.tipo == 1)
{
color = muro;
}
//DIBUJAMOS EL CUADRO EN EL CANVAS
ctx.fillStyle = color;
ctx.strokeRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
ctx.fillRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
}
//DIBUJA OPENSET
this.dibujaOS = function(){
ctx.fillStyle = '#F4D03F';
ctx.fillRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
ctx.strokeRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
ctx.font="20pt Verdana";
ctx.fillStyle = "red";
ctx.fillText(y+","+x,this.x*anchoT,this.y*altoT,anchoT,altoT);
}
//DIBUJA CLOSEDSET
this.dibujaCS = function(){
ctx.fillStyle = '#58D68D';
ctx.fillRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
ctx.strokeRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
}
//DIBUJA CAMINO
this.dibujaCamino = function(){
ctx.fillStyle = '#CD6155 '; //cyan
ctx.fillRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
ctx.strokeRect(this.x*anchoT,this.y*altoT,anchoT,altoT);
}
}
function inicializa(){
//CALCULAMOS EL TAMAÑO DE LAS CASILLAS (Proporcionalmente)
anchoT = parseInt(canvas.width/columnas);
altoT = parseInt(canvas.height/filas);
//CREAMOS LA MATRIZ
escenario = creaArray2D(filas,columnas);
//AÑADIMOS LOS OBJETOS CASILLAS Y PASAMOS EL MAPA
for(i=0;i<filas;i++){
for(j=0;j<columnas;j++){
escenario[i][j] = new Casilla(j,i,matAux[i][j]);
}
}
//AÑADIMOS LOS VECINOS
for(i=0;i<filas;i++){
for(j=0;j<columnas;j++){
escenario[i][j].addVecinos();
}
}
//CREAMOS ORIGEN Y DESTINO DE LA RUTA
principio = escenario[1][1];
fin = escenario[xf][yf];
//INICIALIZAMOS OPENSET
openSet.push(principio);
//EMPEZAMOS A EJECUTAR EL BUCLE PRINCIPAL
id = setInterval(function(){principal();},1000/FPS);
}
function dibujaEscenario(){
for(i=0;i<filas;i++){
for(j=0;j<columnas;j++){
escenario[i][j].dibuja();
}
}
//DIBUJA OPENSET
for(i=0; i<openSet.length; i++){
openSet[i].dibujaOS();
}
//DIBUJA CLOSEDSET
for(i=0; i<closedSet.length; i++){
closedSet[i].dibujaCS();
}
for(i=0; i<camino.length; i++){
camino[i].dibujaCamino();
}
}
function borraCanvas(){
canvas.width = canvas.width;
canvas.height = canvas.height;
}
function algoritmo(){
//SEGUIMOS HASTA ENCONTRAR SOLUCIÓN
if(terminado!=true){
//SEGUIMOS SI HAY AlGO EN OPENSET
if(openSet.length>0){
var ganador = 0; //índiCe o posición dentro del array openset
//evaluamos que OpenSet tiene un menor coste / esfuerzo
for(i=0; i<openSet.length; i++){
if(openSet[i].f < openSet[ganador].f){
ganador = i;
}
}
//Analizamos la casilla ganadora
var actual = openSet[ganador];
//SI HEMOS LLEGADO AL FINAL BUSCAMOS EL CAMINO DE VUELTA
if(actual === fin){
var temporal = actual;
camino.push(temporal);
while(temporal.padre!=null){
temporal = temporal.padre;
camino.push(temporal);
}
console.log('camino encontrado');
terminado = true;
}
//SI NO HEMOS LLEGADO AL FINAL, SEGUIMOS
else{
borraDelArray(openSet,actual);
closedSet.push(actual);
var vecinos = actual.vecinos;
//RECORRO LOS VECINOS DE MI GANADOR
for(i=0; i<vecinos.length; i++){
var vecino = vecinos[i];
//SI EL VECINO NO ESTÁ EN CLOSEDSET Y NO ES UNA PARED, HACEMOS LOS CÁLCULOS
if(!closedSet.includes(vecino) && vecino.tipo!=1){
var tempG = actual.g + 1;
//si el vecino está en OpenSet y su peso es mayor
if(openSet.includes(vecino)){
if(tempG < vecino.g){
vecino.g = tempG; //camino más corto
}
}
else{
vecino.g = tempG;
openSet.push(vecino);
}
//ACTUALIZAMOS VALORES
vecino.h = heuristica(vecino,fin);
vecino.f = vecino.g + vecino.h;
//GUARDAMOS EL PADRE (DE DÓNDE VENIMOS)
vecino.padre = actual;
}
}
}
}
else{
console.log('No hay un camino posible');
terminado = true; //el algoritmo ha terminado
}
}
}
function principal(){
borraCanvas();
algoritmo();
dibujaEscenario();
}