Question
I need help figuring out a method to move the robot randomly. JAVA ***** Create a robot that moves through your maze. The robot makes
I need help figuring out a method to move the robot randomly.
JAVA *****
Create a robot that moves through your maze.
The robot makes random moves up, down, left, and right. It can only see locations that are directly adjacent to its current location. After eachmove, display the robot in the maze, the number of moves so far, and pause for a moment. Here is an example display. X represents the robot:
++++++++++ E + ++ +++ + + ++ +++++++ + X ++ +++++++ ++ +++ +++ ++ + E ++++++++++ ++++++++++ Move number 138
Begin this homework with your code for the Display Maze lab. Add an array robotPosition[] of two integers to store the current row and column of the robot.
Add a method to determine the new robot position after each random move: public static void randomMove( Values maze[][], int robotPosition[] )
Use this code to pause the program 1000 milliseconds between each robot move: try { Thread.sleep(1000); } catch (InterruptedException e) {}
To clear the screen prior to displaying the maze, print 50 blank lines.
OPTIONAL (1 bonus point) Make your robot smarter, so that it escapes the maze faster. For example, you can give your robot a memory. Ask the user to select dumb or smart robot before each run.
HERE IS WHAT I HAVE SO FAR
package mazecreation;
public class MazeCreation {
public enum Values { space, wall, escape, robot; }
public static void main(String[] args) { Values maze[][] = new Values[10][10]; int[] robotPosition = new int[10]; createMaze(maze); showMaze(maze); randomMove(maze, robotPosition); }
public static void initializeSpace(Values maze[][]){ maze[1][8] = Values.space; maze[2][9] = Values.space; for (int i = 1; i < 10; i++) { for (int j = 1; j < 3; j++) { maze[i][j] = Values.space; } } for (int i = 1; i < 8; i++) { for (int j = 7; j < 9; j++) { maze[i][j] = Values.space; } } for (int i = 4; i < 8; i++) { for (int j = 4; j < 8; j++) { maze[i][j] = Values.space; } } for (int i = 0; i <10; i++){ maze[i][i] = Values.space; } for (int i = 8; i >= 0 ; i--){ maze[8-i][i] = Values.space; } maze[2][9] = Values.escape; maze[8][0] = Values.escape; } public static void createMaze(Values maze[][]) { for (int i = 0; i < maze.length; i++) { for (int j = 0; j < maze[0].length; j++) { maze[i][j] = Values.wall; } }
initializeSpace(maze); }
public static void showMaze(Values maze[][]) { for (int i = 0; i < maze.length; i++) { System.out.println(""); for (int j = 0; j < maze[0].length; j++) { switch (maze[i][j]) { case wall: System.out.print("+"); break;
case escape: System.out.print("E"); break; case robot: System.out.print("R");
default: System.out.print(" "); break;
}
} }
} public static void randomMove( Values maze[][], int robotPosition[] ){ maze[1][1] = Values.robot; for (int i = 0; i < maze.length; i++) { for (int j = 0; j < maze.length; j++) { if (the array value next to the robot != Values.wall) { halt move continue; } else if (the array value next to the robot == Values.space){ halt move; continue; } else { break; System.out.println("Your robot has made its way through " + "the maze"); }
} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started