Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Maze Game.java #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Maze Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.Scanner;

public class MazeGame {

public static void main(String[] args) {
char[][] maze = {
{'S', ' ', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', ' ', '#', ' ', ' ', ' ', '#', ' ', ' ', '#'},
{'#', ' ', '#', '#', '#', ' ', '#', ' ', '#', '#'},
{'#', ' ', ' ', ' ', '#', ' ', '#', ' ', ' ', '#'},
{'#', '#', '#', ' ', '#', ' ', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', ' ', 'E'}
};

int playerRow = 0;
int playerCol = 0;

Scanner scanner = new Scanner(System.in);

while (true) {
// Print the current state of the maze
printMaze(maze);

// Get the player's move
System.out.print("Enter your move (U/D/L/R): ");
String move = scanner.next().toUpperCase();

// Update the player's position based on the move
switch (move) {
case "U":
if (playerRow > 0 && maze[playerRow - 1][playerCol] != '#') {
maze[playerRow][playerCol] = ' ';
playerRow--;
}
break;
case "D":
if (playerRow < maze.length - 1 && maze[playerRow + 1][playerCol] != '#') {
maze[playerRow][playerCol] = ' ';
playerRow++;
}
break;
case "L":
if (playerCol > 0 && maze[playerRow][playerCol - 1] != '#') {
maze[playerRow][playerCol] = ' ';
playerCol--;
}
break;
case "R":
if (playerCol < maze[0].length - 1 && maze[playerRow][playerCol + 1] != '#') {
maze[playerRow][playerCol] = ' ';
playerCol++;
}
break;
default:
System.out.println("Invalid move. Use U/D/L/R.");
}

// Check if the player has reached the exit
if (maze[playerRow][playerCol] == 'E') {
System.out.println("Congratulations! You reached the exit.");
break;
}
}

scanner.close();
}

private static void printMaze(char[][] maze) {
for (char[] row : maze) {
for (char cell : row) {
System.out.print(cell + " ");
}
System.out.println();
}
System.out.println();
}
}