Homework 2 - Robot in Maze Hand in hard copy of your code. Be prepared to run yo
ID: 3887555 • Letter: H
Question
Homework 2 - Robot in Maze Hand in hard copy of your code. Be prepared to run your code in class. Begin this homework with your code for Homework 1. Create a 'robot that moves through the 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 each move, display the robot as an 'X' in the maze, and press 'enter' to make the next move. Here is an example display. The entrance is on the upper left and the exit is on the lower right: Count the number of moves and, if the robot escapes the maze, display the total. Your program should have three functions: createMaze(char maze[l[columns], int columns, int rows) - initializes your maze showMaze(char maze[][columns], int columns, int rows)- display maze and robot robot(char mazell[columns], int lastColumn, int lastRow, int &nextColumn;, int &nextRow;) - robot makes a random move Be sure to pass maze and robot position as parameters in the function call - do no use global variables OPTIONAL Make your robot 'smarter, so that it escapes the maze faster. For example, you can give your robot a memory.Explanation / Answer
class maze
{
char mz[][];
int rows,columns;
maze()
{
rows=0;
columns=0;
mz=new int[rows][columns];
}
void createMaze(char Maze[][],int r,int c)
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
Maze[i][j]=sc.nextInt();// * for maze and _ for path
maze=Maze;
}
void showMaze(char Maze[][],int r,int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
System.out.print(Maze[i][j]);
}
System.out.println();
}
}
void robot(char Maze[][],int lc,int lr,int nc,int nr)
{
if(Maze[nr-1][nc-1]=='_'){Maze[nr-1][nc-1]='X';lc=nc;lr=nr;}
}
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
int m=0;
maze ob=new maze();
ob.rows=10;
ob.columns=6;
ob.createMaze(ob.maze,ob.rows,ob.columns);
ob.maze[0][0]='X';
void showMaze(ob.maze,ob.rows,ob.columns);
while(true)
{
int nxr=sc.nextInt();
int nxc=sc.nextInt();
ob.robot(ob.maze,0,0,nxc,nxr);
if(nxc==ob.c && nxr==ob.r){System.out.println(m);break;}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.