-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaberinto.java
324 lines (286 loc) · 11.6 KB
/
Laberinto.java
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.StringTokenizer;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Laberinto extends JPanel {
private static final long serialVersionUID = 1L;
private Cuadro[][] cuadros; // Matriz de cuadros
private QueueLE<Camino> cola;
private StackLE<Camino> pila;
private CoordinateConverter converter;
private int state,
velocidad,
aglomeracion,
tamanoPixel;
private Camino fin,
inicio;
private ControlPanel cp;
private Image brick;
public Laberinto(ControlPanel cp) {
super();
this.cp = cp;
this.tamanoPixel = 40;
this.setPreferredSize(new Dimension(this.tamanoPixel * 20, this.tamanoPixel * 20 - 10));
this.cuadros = new Cuadro[20][20];
this.converter = new CoordinateConverter();
this.state = 1;
this.velocidad = 75;
this.aglomeracion = 4;
try {
this.brick=ImageIO.read(new File("brick.jpeg"));
}catch(IOException ex) {}
this.reset();
this.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseClicked(MouseEvent e) {
try {
if (Laberinto.this.cuadros[e.getY() / tamanoPixel][e.getX() / tamanoPixel] instanceof Pared) {
return;
}
if (Laberinto.this.state == 1) {
Laberinto.this.inicio = (Camino) Laberinto.this.cuadros[e.getY() / tamanoPixel][e.getX() / tamanoPixel];
Laberinto.this.inicio.changeColor("#AAEFF8"); //Color inicio
Laberinto.this.repaint();
Laberinto.this.state = 2;
Laberinto.this.cp.setNotesText("Selecciona el punto final");
} else if (Laberinto.this.state == 2) {
if (Laberinto.this.cuadros[e.getY() / tamanoPixel][e.getX() / tamanoPixel] != Laberinto.this.inicio) {
Laberinto.this.fin = (Camino) Laberinto.this.cuadros[e.getY() / tamanoPixel][e.getX() / tamanoPixel];
Laberinto.this.fin.changeColor("#AAEFF8"); //Color fin
Laberinto.this.repaint();
Laberinto.this.state = 4;
Laberinto.this.cp.setNotesText("Buscando ruta");
Laberinto.this.startSimulation();
}
}
} catch (ArrayIndexOutOfBoundsException ex) {}
}
});
}
public void startSimulation() {
Thread hilo = new Thread(new Runnable() {
public void run() {
boolean found = false;
Laberinto.this.cola.enqueue(Laberinto.this.inicio);
Camino current = null;
while (!Laberinto.this.cola.isEmpty()) {
try {
current = Laberinto.this.cola.dequeue();
Laberinto.this.pila.push(current);
if (current != Laberinto.this.inicio) {
current.changeColor("#FFFFFF"); //Color cuando esta buscando en el camino
}
if (current == Laberinto.this.fin) {
current.changeColor("#AAEFF8"); //Cuadro final
Laberinto.this.repaint();
current.addPath(new int[] { Laberinto.this.converter.matrixToLinear(current.getY() / tamanoPixel,
current.getX() / tamanoPixel, Laberinto.this.cuadros.length) });
found = true;
break;
} else {
// Sacar el camino previo de current
QueueLE<Integer> caminoPrevio = current.getPath();
int[] path = new int[caminoPrevio.size() + 1];
int i = 0; // Contador
while (!caminoPrevio.isEmpty()) {
path[i] = caminoPrevio.dequeue();
i++;
}
// Aniadir la coordenada del nodo actual
path[i] = Laberinto.this.converter.matrixToLinear(current.getY() / tamanoPixel,
current.getX() / tamanoPixel, Laberinto.this.cuadros.length);
// Agregar vecinos
if (current.getX() / tamanoPixel + 1 < Laberinto.this.cuadros.length) {
// Vecino de la derecha
if (Laberinto.this.cuadros[current.getY() / tamanoPixel][current.getX() / tamanoPixel
+ 1] instanceof Camino) { // Asegurar que el vecino no es una pared
if (!Laberinto.this.pila.contains((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel + 1])
&& !Laberinto.this.cola.contains((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel + 1])) {
((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel][current.getX()
/ tamanoPixel + 1]).addPath(path);
Laberinto.this.cola.enqueue((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel + 1]);
}
}
}
if (current.getX() / tamanoPixel - 1 >= 0) {
// Vecino de la izquierda
if (Laberinto.this.cuadros[current.getY() / tamanoPixel][current.getX() / tamanoPixel
- 1] instanceof Camino) { // Asegurar que el vecino no es una pared
if (!Laberinto.this.pila.contains((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel - 1])
&& !Laberinto.this.cola.contains((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel - 1])) {
((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel][current.getX()
/ tamanoPixel - 1]).addPath(path);
Laberinto.this.cola.enqueue((Camino) Laberinto.this.cuadros[current.getY()
/ tamanoPixel][current.getX() / tamanoPixel - 1]);
}
}
}
if (current.getY() / tamanoPixel - 1 >= 0) {
// Vecino de arriba
if (Laberinto.this.cuadros[current.getY() / tamanoPixel - 1][current.getX()
/ tamanoPixel] instanceof Camino) { // Asegurar que el vecino no es una pared
if (!Laberinto.this.pila.contains(
(Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel - 1][current.getX()
/ tamanoPixel])
&& !Laberinto.this.cola
.contains((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel
- 1][current.getX() / tamanoPixel])) {
((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel - 1][current.getX()
/ tamanoPixel]).addPath(path);
Laberinto.this.cola.enqueue((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel
- 1][current.getX() / tamanoPixel]);
}
}
}
if (current.getY() / tamanoPixel + 1 < Laberinto.this.cuadros.length) {
// Vecino de abajo
if (Laberinto.this.cuadros[current.getY() / tamanoPixel + 1][current.getX()
/ tamanoPixel] instanceof Camino) {
// Asegurar que el vecino no es una pared
if (!Laberinto.this.pila.contains(
(Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel + 1][current.getX()
/ tamanoPixel])
&& !Laberinto.this.cola
.contains((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel
+ 1][current.getX() / tamanoPixel])) {
((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel + 1][current.getX()
/ tamanoPixel]).addPath(path);
Laberinto.this.cola.enqueue((Camino) Laberinto.this.cuadros[current.getY() / tamanoPixel
+ 1][current.getX() / tamanoPixel]);
}
}
}
}
Laberinto.this.repaint();
Thread.sleep(Laberinto.this.velocidad);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (found) {
Laberinto.this.showPath(current.getPath());
Laberinto.this.cp.setNotesText("Se encontro la solucion");
} else {
Laberinto.this.cp.setNotesText("No se encontro una solucion al laberinto");
Laberinto.this.state = 5;
}
}
});
hilo.start();
}
public void reset() {
this.cola = new QueueLE<Camino>();
this.pila = new StackLE<Camino>();
this.state = 1;
this.cp.setNotesText("Selecciona el punto de inicio");
Random ran = new Random();
for (int i = 0; i < cuadros.length; i++) {
for (int j = 0; j < cuadros[i].length; j++) {
//Implementar paredes y caminos
if (ran.nextInt(this.aglomeracion) == 0) {
cuadros[i][j] = new Pared(j * tamanoPixel, i * tamanoPixel, this.tamanoPixel);
} else {
cuadros[i][j] = new Camino(j * tamanoPixel, i * tamanoPixel, this.tamanoPixel);
}
}
}
this.repaint();
}
//Axrchivo importado
public void reset(String filename){
// Resetea el laberinto con laberinto prefabricado Debe ser 20 x 20
this.cola = new QueueLE<Camino>();
this.pila = new StackLE<Camino>();
this.state = 1;
this.cp.setNotesText("Selecciona el punto de inicio");
try{
BufferedReader bf = new BufferedReader(new FileReader(filename));
for(int i = 0; i < cuadros.length; i++){
String line = bf.readLine();
StringTokenizer tk = new StringTokenizer(line, ",");
for(int j = 0; j < cuadros[i].length; j++){
if(tk.nextToken().equals("1")){
cuadros[i][j] = new Pared(j*tamanoPixel, i*tamanoPixel, this.tamanoPixel);
}else{
cuadros[i][j] = new Camino(j*tamanoPixel, i*tamanoPixel, this.tamanoPixel);
}
}
}
bf.close();
}catch(Exception ex){
// En caso de excepcion resetearlo de manera aleatoria
this.reset();
System.out.println("Hubo un error");
}
this.repaint();
}
public void setMatriz(int x, int y) {
this.cuadros= new Cuadro[x][y];
}
public void showPath(QueueLE<Integer> path) {
Thread hilo = new Thread(new Runnable() {
public void run() {
try {
int num;
while (!path.isEmpty()) {
num = path.dequeue();
Thread.sleep(Laberinto.this.velocidad + 50);
((Camino) Laberinto.this.cuadros[Laberinto.this.converter.linearToMatrix(num,
Laberinto.this.cuadros.length)[0]][Laberinto.this.converter.linearToMatrix(num,
Laberinto.this.cuadros.length)[1]]).changeColor("#81A4E2"); //Color trazando camino AAEFF8
Laberinto.this.repaint();
}
Laberinto.this.state = 5;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
hilo.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < cuadros.length; i++) {
for (int j = 0; j < cuadros[i].length; j++) {
g.setColor(cuadros[i][j].getColor());
g.fillRect(cuadros[i][j].getX(), cuadros[i][j].getY(), cuadros[i][j].getSide(),
cuadros[i][j].getSide());
g.setColor(Color.BLACK);
g.drawRect(cuadros[i][j].getX(), cuadros[i][j].getY(), cuadros[i][j].getSide(),
cuadros[i][j].getSide());
if(cuadros[i][j] instanceof Pared) {
g.drawImage(this.brick, cuadros[i][j].getX(), cuadros[i][j].getY(), cuadros[i][j].getSide(), cuadros[i][j].getSide(), this);
}
}
}
}
public void setVelocidad(int velocidad) {
if (velocidad >= 5 && velocidad <= 1000) {
this.velocidad = velocidad;
}
}
public int getState() {
return this.state;
}
public void setWallAglomeration(int num) {
this.aglomeracion = num;
}
}