Skip to content

Commit dcefa14

Browse files
authored
Merge pull request #5512 from ASJordi/main
#33 - Java
2 parents f221cb8 + 9cfe0f4 commit dcefa14

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.Arrays;
2+
import java.util.Scanner;
3+
4+
public class Main {
5+
6+
private static final String EMPTY = "⬜";
7+
private static final String BARRIER = "⬛";
8+
private static final String MICKEY = "\uD83D\uDC2D";
9+
private static final String EXIT = "\uD83D\uDEAA";
10+
private static String[][] board = {
11+
{MICKEY, EMPTY, EMPTY, EMPTY, EMPTY, BARRIER},
12+
{EMPTY, BARRIER, EMPTY, EMPTY, BARRIER, EMPTY},
13+
{EMPTY, BARRIER, EMPTY, BARRIER, EMPTY, EMPTY},
14+
{EMPTY, EMPTY, EMPTY, EMPTY, BARRIER, EMPTY},
15+
{BARRIER, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY},
16+
{EMPTY, BARRIER, EMPTY, EMPTY, EMPTY, EXIT}
17+
};
18+
19+
public static void main(String[] args) {
20+
Scanner sc = new Scanner(System.in);
21+
System.out.println("Welcome to the maze game! Find the exit to help Mickey Mouse escape!");
22+
System.out.println("Movements available: W (up), A (left), S (down), D (right)");
23+
System.out.println("Write 'exit' to quit the game");
24+
System.out.println("Good luck!");
25+
System.out.println("Where do you want to move?");
26+
27+
String input = "";
28+
29+
while (!input.equalsIgnoreCase("exit")) {
30+
printBoard();
31+
input = sc.nextLine();
32+
if (move(input)) break;
33+
}
34+
}
35+
36+
private static boolean move(String direction) {
37+
int[] mickeyPosition = findMickey();
38+
int x = mickeyPosition[0];
39+
int y = mickeyPosition[1];
40+
41+
if (x == board.length - 1 && y == board[0].length - 1) {
42+
System.out.println("Congratulations! You helped Mickey Mouse escape!");
43+
return true;
44+
}
45+
46+
switch (direction) {
47+
case "W":
48+
if (x > 0 && !board[x - 1][y].equals(BARRIER)) {
49+
board[x][y] = EMPTY;
50+
board[x - 1][y] = MICKEY;
51+
}
52+
break;
53+
case "A":
54+
if (y > 0 && !board[x][y - 1].equals(BARRIER)) {
55+
board[x][y] = EMPTY;
56+
board[x][y - 1] = MICKEY;
57+
}
58+
break;
59+
case "S":
60+
if (x < board.length - 1 && !board[x + 1][y].equals(BARRIER)) {
61+
board[x][y] = EMPTY;
62+
board[x + 1][y] = MICKEY;
63+
}
64+
break;
65+
case "D":
66+
if (y < board[0].length - 1 && !board[x][y + 1].equals(BARRIER)) {
67+
board[x][y] = EMPTY;
68+
board[x][y + 1] = MICKEY;
69+
}
70+
break;
71+
case "EXIT":
72+
System.out.println("Thanks for playing!");
73+
break;
74+
default:
75+
System.out.println("Invalid input. Please try again.");
76+
}
77+
78+
return false;
79+
}
80+
81+
private static int[] findMickey() {
82+
int[] mickeyPosition = new int[2];
83+
84+
for (int i = 0; i < board.length; i++) {
85+
for (int j = 0; j < board[0].length; j++) {
86+
if (board[i][j].equals(MICKEY)) {
87+
mickeyPosition[0] = i;
88+
mickeyPosition[1] = j;
89+
break;
90+
}
91+
}
92+
}
93+
94+
return mickeyPosition;
95+
}
96+
97+
static void printBoard() {
98+
for (String[] strings : board) {
99+
System.out.println(Arrays.toString(strings));
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)