Skip to content

Commit decd7f4

Browse files
authored
Merge pull request mouredev#6417 from martinbohorquez/java#33
mouredev#33 - java
2 parents da4b24a + a2c06fd commit decd7f4

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
import java.util.Scanner;
5+
6+
/**
7+
* #33 RESCATANDO A MICKEY
8+
*
9+
* @author martinbohorquez
10+
*/
11+
public class martinbohorquez {
12+
13+
static Scanner sc = new Scanner(System.in);
14+
15+
static List<List<String>> maze = new ArrayList<>(
16+
Arrays.asList(
17+
Arrays.asList("🐭", "⬛️", "⬛️", "⬛️", "⬛️", "⬛️"),
18+
Arrays.asList("⬜️", "⬛️", "⬛️", "⬛️", "⬜️", "⬛️"),
19+
Arrays.asList("⬜️", "⬛️", "⬛️", "⬛️", "⬜️", "⬛️"),
20+
Arrays.asList("⬜️", "⬜️", "⬜️", "⬜️", "⬜️", "⬜️"),
21+
Arrays.asList("⬛️", "⬜️", "⬛️", "⬜️", "⬛️", "⬛️"),
22+
Arrays.asList("⬛️", "⬜️", "⬛️", "⬜️", "⬜️", "🚪")
23+
)
24+
);
25+
26+
static Position mickey = new Position("Mickey");
27+
static boolean flag = false;
28+
29+
public static void main(String[] args) {
30+
31+
while (!flag) {
32+
System.out.println("<-------------- JUEGO DE MICKY ------------------>");
33+
printMaze(maze);
34+
System.out.println("""
35+
¿Hacia dónde se mueve Mickey?
36+
[w] arriba
37+
[s] abajo
38+
[a] izquierda
39+
[d] derecha
40+
""");
41+
System.out.print("Dirección: ");
42+
String direction = sc.nextLine().toLowerCase();
43+
44+
mickey.move(direction);
45+
}
46+
}
47+
48+
private static void printMaze(List<List<String>> maze) {
49+
maze.forEach(row -> {
50+
row.forEach(cell -> System.out.print(cell + " "));
51+
System.out.println();
52+
});
53+
}
54+
55+
static class Position {
56+
private String name;
57+
private Integer row;
58+
private Integer column;
59+
60+
public Position(String name) {
61+
this.name = name;
62+
this.row = 0;
63+
this.column = 0;
64+
}
65+
66+
private String getName() {
67+
return name;
68+
}
69+
70+
private Integer getRow() {
71+
return row;
72+
}
73+
74+
private void setRow(Integer row) {
75+
this.row = row;
76+
}
77+
78+
private Integer getColumn() {
79+
return column;
80+
}
81+
82+
private void setColumn(Integer column) {
83+
this.column = column;
84+
}
85+
86+
private void move(String direction) {
87+
switch (direction) {
88+
case "w" -> update(-1, 0);
89+
case "s" -> update(1, 0);
90+
case "a" -> update(0, -1);
91+
case "d" -> update(0, 1);
92+
default -> System.out.printf("Dirección '%s' es incorrecta!%n", direction);
93+
}
94+
}
95+
96+
private void update(int i, int j) {
97+
if (getRow() + i < 0 || getRow() + i > maze.size() || getColumn() + j < 0 || getColumn() + j > maze.getFirst().size())
98+
System.out.println("¡No puedes desplazarte fuera del laberinto!");
99+
else if (maze.get(getRow() + i).get(getColumn() + j).equals("⬛️"))
100+
System.out.println("¡En esa dirección hay un obstáculo!");
101+
else {
102+
maze.get(getRow()).set(getColumn(), "⬜️");
103+
setRow(getRow() + i);
104+
setColumn(getColumn() + j);
105+
if (maze.get(getRow()).get(getColumn()).equals("🚪")) {
106+
System.out.printf("¡%s ha encontrado la salida!%n", mickey.getName());
107+
flag = true;
108+
}
109+
maze.get(getRow()).set(getColumn(), "🐭");
110+
}
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)