forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze Game Project
96 lines (81 loc) · 2.43 KB
/
Maze Game Project
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
import java.util.Scanner;
public class MazeGame {
private static final int WALL = '#';
private static final int PLAYER = 'P';
private static final int EXIT = 'X';
private static char[][] maze;
private static int playerRow;
private static int playerCol;
public static void main(String[] args) {
// Load the maze
maze = new char[][]{{'#', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', '#'},
{'#', ' ', '#', '#', '#'},
{'#', ' ', ' ', ' ', '#'},
{'#', '#', '#', '#', '#'}};
// Initialize the player position
playerRow = 1;
playerCol = 1;
// Play the game
while (true) {
// Print the maze
printMaze();
// Get the player's move
Scanner scanner = new Scanner(System.in);
char move = scanner.next().charAt(0);
// Move the player
switch (move) {
case 'w':
moveUp();
break;
case 'a':
moveLeft();
break;
case 's':
moveDown();
break;
case 'd':
moveRight();
break;
}
// Check if the player has won
if (maze[playerRow][playerCol] == EXIT) {
System.out.println("You won!");
break;
}
// Check if the player has hit a wall
if (maze[playerRow][playerCol] == WALL) {
System.out.println("You hit a wall!");
break;
}
}
}
private static void printMaze() {
for (int i = 0; i < maze.length; i++) {
for (int j = 0; j < maze[0].length; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
private static void moveUp() {
if (playerRow > 0) {
playerRow--;
}
}
private static void moveLeft() {
if (playerCol > 0) {
playerCol--;
}
}
private static void moveDown() {
if (playerRow < maze.length - 1) {
playerRow++;
}
}
private static void moveRight() {
if (playerCol < maze[0].length - 1) {
playerCol++;
}
}
}