Q: In Java and using recursion and a 2d array. You need to make a maze solver. T
ID: 3690817 • Letter: Q
Question
Q:
In Java and using recursion and a 2d array.
You need to make a maze solver. The program will take in from a file 2 things. The size of the square maze, and the maze itself. The maze will consists of numbers between 0 and 3, where 0 is the starting point of the maze, 1 is an open path, 3 is a wall and blocked , and 2 is the end. The maze will be no larger than 40x40. For example a 6x6 maze could be represented by the following file, there will be no spaces seperating the elements of the maze:
The program must then solve the maze. It will then output the correct path through the maze marked by 0’s to the command line. For example for the maze above could output the following.
000003
333301
110003
330333
330000
333330
You can assume that the input contains the exact amount of numbers needed and that it is a solvable maze, following the rules outlined above.
Explanation / Answer
Please follow the code and the comments for description :
CODE :
import java.util.*;
import java.io.File;
public class MazeGame {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("maze.txt"));
Scanner user = new Scanner(System.in);
int rows = scan.nextInt();
int columns = scan.nextInt();
int px = 0;
int py = 0;
String [][] maze = new String[rows][columns];
String junk = scan.nextLine();
for (int i = 0; i < rows; i++){
String temp = scan.nextLine();
String[] arrayPasser = temp.split("");
for (int j = 0; j < columns; j++){
maze[i][j] = arrayPasser[i];
}
}
boolean gotPath = false;
while (gotPath == false){
for (int i = 0; i < rows; i++){
for (int j = 0; j < columns; j++){
System.out.print(maze[i][j]);
System.out.print(" ");
}
System.out.print(" ");
}
System.out.printf(" ");
System.out.println("You may:");
System.out.println("1) Move up");
System.out.println("2) Move down");
System.out.println("3) Move left");
System.out.println("4) Move right");
System.out.println("0) Quit");
int choice = user.nextInt();
int i = 0;
if (choice == 1 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py-1].equals("3") == false){
maze[px][py] = "0";
maze[k][l-1] = "1";
maze[px][py] = maze[k][l-1];
}else if (maze[px][py-1] == "3"){
System.out.println("Cannot move! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 2 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px][py+1].equals("3") == false){
maze[px][py] = "0";
maze[k][l+1] = "1";
maze[px][py] = maze[k][l+1];
}else if (maze[px][py+1] == "3"){
System.out.println("Cannot move! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 3 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px-1][py].equals("3") == false){
maze[px][py] = "0";
maze[k-1][l] = "1";
maze[px][py] = maze[k-1][l];
}else if (maze[px-1][py] == "3"){
System.out.println("Cannot move! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 4 && i >= 0 && i < columns){
for (int k = 0; k < rows; k++){
for (int l = 0; l < columns; l++){
if (maze[k][l].equals(maze[px][py]) && maze[px+1][py].equals("3") == false){
maze[px][py] = "0";
maze[k+1][l] = "1";
maze[px][py] = maze[k+1][l];
}else if (maze[px+1][py] == "3"){
System.out.println("Cannot move! Try something else.");
}else {
continue;}
}
}
}
else if (choice == 0){
System.exit(0);
}
}
System.out.println("Congratulations, you found the path!");
scan.close();
user.close();
}
}
The text file can be user defined with the given conditions.
Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.