-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze_Game.java
More file actions
337 lines (281 loc) · 12.5 KB
/
Maze_Game.java
File metadata and controls
337 lines (281 loc) · 12.5 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
public class Maze_Game {
private static final char WALL = '#';
private static final char PATH = '.';
private static final char PLAYER = '@';
private static final char HINT_PATH = '+';
private static final int ROWS = 11;
private static final int COLS = 21;
public static void main(String[] args) {
printGameOverview();
System.out.println("Press any key to start the game...");
new Scanner(System.in).nextLine(); // Wait for user input to start
// Create a Scanner for user input
Scanner scanner = new Scanner(System.in);
boolean playAgain = true;
// Main game loop
do {
char[][] maze = generateMaze(ROWS, COLS);
int startX = 1;
int startY = 0; // Initial player position
int destX = ROWS - 2;
int destY = COLS - 1; // Destination position
generatePathDFS(maze, startX, startY+1, destX, destY); // randomly generate path using depth-first search and backtracking
printMazeExteriorWalls(maze, startX, startY);
System.out.println(""); // spacing
// Create a memoized maze for hints
char[][] hintMaze = new char[ROWS][COLS];
copyMaze(maze, hintMaze);
// Find the optimal path from the current position to the destination
findOptimalPath(hintMaze, startX, startY, destX, destY); // path is computed only one time
char move;
String line;
// Inner game loop
while (true) {
printMazeWithPlayerView(maze, startX, startY);
System.out.print("Select movement: ");
line = scanner.nextLine();
if (line.length() == 0) {
move = '0';
} else {
move = line.toUpperCase().charAt(0);
}
int newX = startX;
int newY = startY;
switch (move) {
case 'W':
newX = startX - 1;
break;
case 'S':
newX = startX + 1;
break;
case 'A':
newY = startY - 1;
break;
case 'D':
newY = startY + 1;
break;
case 'H':
printMazeWithPlayerView(hintMaze, startX, startY);
break;
default:
System.out.println("Invalid input. Please use W, A, S, D for movement or H for a hint.");
}
if (isValidMove(maze, startX, startY, newX, newY)) {
movePlayer(maze, startX, startY, newX, newY);
movePlayer(hintMaze, startX, startY, newX, newY);
startX = newX;
startY = newY;
} else {
System.out.println("Invalid move. You can't pass through walls.");
}
System.out.println(""); // spacing
if (startX == destX && startY == destY) {
System.out.println("Congratulations! You've reached the destination.");
break;
}
}
System.out.print("Enter Y to play again: ");
line = scanner.nextLine();
if (line.length() == 0) {
playAgain = false;
} else if (line.toUpperCase().charAt(0) != 'Y') {
playAgain = false;
}
} while (playAgain);
System.out.println("Goodbye!");
System.out.println(); // spacing
} // end main
public static void printGameOverview() {
System.out.println("Welcome to the Maze Game!");
System.out.println("You are represented by the '@' symbol.");
System.out.println("Your goal is to reach the destination at maze[rows - 2][cols - 1].");
System.out.println("Use the following keys for movement:");
System.out.println(" W - Move North");
System.out.println(" S - Move South");
System.out.println(" A - Move West");
System.out.println(" D - Move East");
System.out.println(" H - Ask for a Hint");
System.out.println("Navigate the maze and find your way to the destination.");
} // end printGameOverview
public static char[][] generateMaze(int rows, int cols) {
char[][] maze = new char[rows][cols];
// Initialize the maze with walls
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
maze[row][col] = WALL;
}
}
// Set start and destination positions
maze[1][0] = PLAYER;
maze[rows - 2][cols - 1] = PATH;
return maze;
} // end generateMaze
public static void generatePathDFS(char[][] maze, int startX, int startY, int destX, int destY) {
Stack<Integer> stackX = new Stack<>();
Stack<Integer> stackY = new Stack<>();
Random random = new Random();
int currentX = startX;
int currentY = startY;
stackX.push(currentX);
stackY.push(currentY);
while (!stackX.isEmpty()) {
maze[currentX][currentY] = PATH;
if (currentX == destX && currentY == destY) {
return;
}
int[] directions = {0, 1, 2, 3};
shuffleArray(directions, random);
boolean moved = false;
for (int direction : directions) {
int newX = currentX;
int newY = currentY;
if (direction == 0 && currentX > 2) { // Move north
newX -= 2;
} else if (direction == 1 && currentX < maze.length - 3) { // Move south
newX += 2;
} else if (direction == 2 && currentY > 2) { // Move west
newY -= 2;
} else if (direction == 3 && currentY < maze[0].length - 3) { // Move east
newY += 2;
}
if (maze[newX][newY] == WALL) {
maze[newX - (newX - currentX) / 2][newY - (newY - currentY) / 2] = PATH;
stackX.push(newX);
stackY.push(newY);
currentX = newX;
currentY = newY;
moved = true;
break;
}
}
if (!moved) {
stackX.pop();
stackY.pop();
if (!stackX.isEmpty()) {
currentX = stackX.peek();
currentY = stackY.peek();
}
}
}
} // end generatePathDFS
public static void shuffleArray(int[] arr, Random random) {
for (int i = arr.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
} // end shuffleArray
// Define a method to copy the maze
public static void copyMaze(char[][] source, char[][] destination) {
for (int i = 0; i < source.length; i++) {
System.arraycopy(source[i], 0, destination[i], 0, source[i].length);
}
} // end copyMaze
public static void movePlayer(char[][] maze, int startX, int startY, int destX, int destY) {
maze[startX][startY] = PATH;
maze[destX][destY] = PLAYER;
} // end movePlayer
public static boolean isValidMove(char[][] maze, int startX, int startY, int newX, int newY) {
int rows = maze.length;
int cols = maze[0].length;
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols) {
return maze[newX][newY] != WALL;
}
return false;
} // end isValidMove
// Find the optimal path using DFS
public static boolean findOptimalPath(char[][] maze, int currentX, int currentY, int destX, int destY) {
// Check if we've reached the destination
if (currentX == destX && currentY == destY) {
return true;
}
// Check if we are out of bounds or hitting a wall
if (currentX < 0 || currentX >= maze.length || currentY < 0 || currentY >= maze[0].length || maze[currentX][currentY] == WALL) {
return false;
}
// Check if this cell has already been visited (memoization)
if (maze[currentX][currentY] == HINT_PATH) {
return false;
}
// Mark the current cell as visited
maze[currentX][currentY] = HINT_PATH;
// Try all possible directions
if (findOptimalPath(maze, currentX - 1, currentY, destX, destY)) {
return true;
}
if (findOptimalPath(maze, currentX + 1, currentY, destX, destY)) {
return true;
}
if (findOptimalPath(maze, currentX, currentY - 1, destX, destY)) {
return true;
}
if (findOptimalPath(maze, currentX, currentY + 1, destX, destY)) {
return true;
}
// If none of the directions lead to the destination, backtrack
maze[currentX][currentY] = PATH; // Mark as part of the memoized solution
return false;
} // end findOptimalPath
public static void printMaze(char[][] maze, int playerX, int playerY) {
int radius = 1; // The radius of the square around the player
int startRow = Math.max(0, playerX - radius);
int endRow = Math.min(maze.length - 1, playerX + radius);
int startCol = Math.max(0, playerY - radius);
int endCol = Math.min(maze[0].length - 1, playerY + radius);
for (int row = startRow; row <= endRow; row++) {
for (int col = startCol; col <= endCol; col++) {
System.out.print(maze[row][col] + " ");
}
System.out.println();
}
} // end printMaze
public static void printMazeWithPlayerView(char[][] maze, int playerX, int playerY) {
int mazeRows = maze.length;
int mazeCols = maze[0].length;
int radius = 1; // The radius of the square around the player
// Iterate through the 3x3 square around the player
for (int row = Math.max(0, playerX - radius); row <= Math.min(mazeRows - 1, playerX + radius); row++) {
for (int col = Math.max(0, playerY - radius); col <= Math.min(mazeCols - 1, playerY + radius); col++) {
//if (playerX == col && playerY == row) {
//System.out.print(PLAYER + " ");
//} else {
System.out.print(maze[row][col] + " ");
//}
}
System.out.println();
}
} // end printMazeWithPlayerView
// I was going to use this to show the 3x3 square within the maze
public static void printMazeExteriorWalls(char[][] maze, int playerX, int playerY) {
int mazeRows = maze.length;
int mazeCols = maze[0].length;
// Iterate through the maze and print the exterior walls
for (int row = 0; row < mazeRows; row++) {
for (int col = 0; col < mazeCols; col++) {
boolean exteriorWall = (row == 0 || row == mazeRows - 1 || col == 0 || col == mazeCols-1);
if (row == playerX && col == playerY) {
System.out.print(PLAYER + " ");
} else if ((row == 1 && col == 0) || (row == mazeRows-2 && col == mazeCols-1)) {
System.out.print(PATH + " ");
} else if (exteriorWall) {
System.out.print(WALL + " ");
} else {
System.out.print(" "); // Empty space inside the maze
}
}
System.out.println();
}
} // end printMazeExteriorWalls
public static void printEntireMaze(char[][] maze) {
for (int row = 0; row < maze.length; row++) {
for (int col = 0; col < maze[row].length; col++) {
System.out.print(maze[row][col] + " "); // Add a space
}
System.out.println();
}
} // end printEntireMaze
}