Skip to content

Commit 50263c2

Browse files
authored
Create Maze Game Project
1 parent be1a4f6 commit 50263c2

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

Maze Game Project

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import java.util.Scanner;
2+
3+
public class MazeGame {
4+
5+
private static final int WALL = '#';
6+
private static final int PLAYER = 'P';
7+
private static final int EXIT = 'X';
8+
9+
private static char[][] maze;
10+
private static int playerRow;
11+
private static int playerCol;
12+
13+
public static void main(String[] args) {
14+
// Load the maze
15+
maze = new char[][]{{'#', '#', '#', '#', '#'},
16+
{'#', ' ', ' ', ' ', '#'},
17+
{'#', ' ', '#', '#', '#'},
18+
{'#', ' ', ' ', ' ', '#'},
19+
{'#', '#', '#', '#', '#'}};
20+
21+
// Initialize the player position
22+
playerRow = 1;
23+
playerCol = 1;
24+
25+
// Play the game
26+
while (true) {
27+
// Print the maze
28+
printMaze();
29+
30+
// Get the player's move
31+
Scanner scanner = new Scanner(System.in);
32+
char move = scanner.next().charAt(0);
33+
34+
// Move the player
35+
switch (move) {
36+
case 'w':
37+
moveUp();
38+
break;
39+
case 'a':
40+
moveLeft();
41+
break;
42+
case 's':
43+
moveDown();
44+
break;
45+
case 'd':
46+
moveRight();
47+
break;
48+
}
49+
50+
// Check if the player has won
51+
if (maze[playerRow][playerCol] == EXIT) {
52+
System.out.println("You won!");
53+
break;
54+
}
55+
56+
// Check if the player has hit a wall
57+
if (maze[playerRow][playerCol] == WALL) {
58+
System.out.println("You hit a wall!");
59+
break;
60+
}
61+
}
62+
}
63+
64+
private static void printMaze() {
65+
for (int i = 0; i < maze.length; i++) {
66+
for (int j = 0; j < maze[0].length; j++) {
67+
System.out.print(maze[i][j]);
68+
}
69+
System.out.println();
70+
}
71+
}
72+
73+
private static void moveUp() {
74+
if (playerRow > 0) {
75+
playerRow--;
76+
}
77+
}
78+
79+
private static void moveLeft() {
80+
if (playerCol > 0) {
81+
playerCol--;
82+
}
83+
}
84+
85+
private static void moveDown() {
86+
if (playerRow < maze.length - 1) {
87+
playerRow++;
88+
}
89+
}
90+
91+
private static void moveRight() {
92+
if (playerCol < maze[0].length - 1) {
93+
playerCol++;
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)