Skip to content

Commit c88343d

Browse files
authored
Merge pull request #7414 from danhingar/ejercicio33
#33 - Java
2 parents a0d021f + 3e0f047 commit c88343d

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.Objects;
2+
import java.util.Scanner;
3+
4+
public class danhingar {
5+
6+
public static void main(String[] args) {
7+
8+
String[][] maze = { { "🐭", "⬛", "⬛", "⬛", "⬛", "⬜" },
9+
{ "⬜", "⬛", "⬜", "⬛", "⬜", "⬜" },
10+
{ "⬜", "⬜", "⬜", "⬛", "⬜", "⬛" },
11+
{ "⬜", "⬛", "⬜", "⬜", "⬜", "⬛" },
12+
{ "⬜", "⬜", "⬛", "⬜", "⬜", "⬜" },
13+
{ "⬜", "⬛", "⬛", "⬛", "⬛", "🚪" } };
14+
15+
print(maze);
16+
17+
int[] mickey = { 0, 0 };
18+
Scanner sc = new Scanner(System.in);
19+
while (Boolean.TRUE) {
20+
System.out.println("¿Hacia dónde se mueve Mickey?");
21+
System.out.println("[u] Arriba");
22+
System.out.println("[d] Abajo");
23+
System.out.println("[l] Izquierda");
24+
System.out.println("[r] Derecha");
25+
System.out.print("Dirección: ");
26+
27+
String direction = sc.nextLine();
28+
29+
int currentRow = mickey[0], currentColumn = mickey[1];
30+
int newRow = currentRow, newColumn = currentColumn;
31+
32+
switch (direction) {
33+
case "u":
34+
newRow = currentRow - 1;
35+
break;
36+
case "d":
37+
newRow = currentRow + 1;
38+
break;
39+
case "r":
40+
newColumn = currentColumn + 1;
41+
break;
42+
case "l":
43+
newColumn = currentColumn - 1;
44+
break;
45+
default:
46+
System.out.println("Dirección no válida.\n");
47+
break;
48+
}
49+
50+
if ((newRow < 0 || newRow > 5) || (newColumn < 0 || newColumn > 5)) {
51+
System.out.println("No puedes desplazarte fuera del Laberinto.\n");
52+
} else if (Objects.equals(maze[newRow][newColumn], "⬛")) {
53+
System.out.println("!En esa dirección hay un obstáculo!\n");
54+
continue;
55+
} else if (Objects.equals(maze[newRow][newColumn], "🚪")) {
56+
System.out.println("!Has encontrado la salida!");
57+
break;
58+
} else {
59+
maze[currentRow][currentColumn] = "⬜";
60+
maze[newRow][newColumn] = "🐭";
61+
mickey[0] = newRow;
62+
mickey[1] = newColumn;
63+
print(maze);
64+
}
65+
66+
}
67+
sc.close();
68+
}
69+
70+
public static void print(String[][] maze) {
71+
for (int i = 0; i < 6; i++) {
72+
for (int j = 0; j < 6; j++) {
73+
System.out.print("".concat(maze[i][j]));
74+
75+
}
76+
System.out.println();
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)