Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Robot in Maze How do I use a Random Number generator to make the robot move rand

ID: 3887332 • Letter: R

Question

Robot in Maze

How do I use a Random Number generator to make the robot move randomly inside the array?

This is my code so far:

#include <stdio.h>

#include <iostream>

using namespace std;

void showMaze(char maze[][10], int columns, int rows, int xLoc, int yLoc);

void robot(char maze[][10], int lastColumn, int lastRow, int &nextColumn, int

&nextRow);

int main()

{

char maze[10][10] =

{ { '*', ' ', '*', '*', '*', '*', '*', '*', '*', '*' },

{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*' },

{ '*', '*', '*', '*', ' ', '*', '*', '*', '*', '*' },

{ '*', '*', ' ', '*', ' ', '*', '*', '*', '*', '*' },

{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', '*' },

{ '*', '*', '*', '*', '*', '*', '*', ' ', '*', '*' },

{ '*', '*', '*', ' ', '*', '*', '*', ' ', '*', '*' },

{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },

{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' },

{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' } };

showMaze(maze, 10, 10, 0, 1);

system("pause");

return 0;

}

void showMaze(char maze[][10], int columns, int rows, int xLoc, int yLoc)

{

for (rows = 0; rows < 10; rows++)

{

for (columns = 0; columns < 10; columns++)

if ((rows == xLoc) && (columns == yLoc))

cout << "X" << " ";

else

cout << maze[rows][columns] << " ";

cout << endl;

}

}

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 844 + t5 84444 Count the number of moves and, if the robot escapes the maze, display the total Your program should have three functions: createMaze(char maze[[columns], int columns, int rows)-initializes your maze showMaze(char mazel)[columns], int columns, int rows) display maze and robot robot(char maze[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.

Explanation / Answer

#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

void createMaze(char maze[][10],int columns,int rows);
void showMaze(char maze[][10], int columns, int rows);
void robot(char maze[][10], int lastColumn, int lastRow, int &nextColumn, int
&nextRow);

int main()
{
srand(time(NULL));
char maze[10][10];
createMaze(maze,10,10);
int lastCol=1,lastRow=0,nextCol,nextRow;
cout<<"Maze At the begining:"<<endl;
showMaze(maze,lastCol,lastRow);
int moveCount=0;
do
{
moveCount++;
cout<<" After "<<moveCount<<" move ";
robot(maze,lastCol,lastRow,nextCol,nextRow);
showMaze(maze,nextCol,nextRow);
lastRow=nextRow;
lastCol=nextCol;
system("pause");
}while(nextRow<8 && nextCol<10);

return 0;
}

void createMaze(char maze[][10],int columns,int rows)
{
char temp[rows][columns]=
{ { '*', ' ', '*', '*', '*', '*', '*', '*', '*', '*' },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*' },
{ '*', '*', '*', '*', ' ', '*', '*', '*', '*', '*' },
{ '*', '*', ' ', '*', ' ', '*', '*', '*', '*', '*' },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '*', '*' },
{ '*', '*', '*', '*', '*', '*', '*', ' ', '*', '*' },
{ '*', '*', '*', ' ', '*', '*', '*', ' ', '*', '*' },
{ '*', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' },
{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' },
{ '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' } };
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
maze[i][j]=temp[i][j];
}

}

void showMaze(char maze[][10],int columns,int rows)
{

for (int i = 0; i< 10; i++)
{
for (int j= 0; j < 10; j++)
if ((rows == i) && (columns == j))
cout << "X" << " ";
else
cout << maze[i][j] << " ";
cout << endl;
}

}

void robot(char maze[][10], int lastColumn, int lastRow, int &nextColumn, int
&nextRow)
{
int flag;
do
{ flag=0;
int temp=rand()%4+1;
if(temp==1) //moving left
{
if(maze[lastRow][lastColumn-1]=='*')
flag=1;
else
{
nextColumn=--lastColumn;
nextRow=lastRow;
}
}
else if(temp==2) //moving right
{
if(maze[lastRow][lastColumn+1]=='*')
flag=1;
else
{
nextColumn=++lastColumn;
nextRow=lastRow;
}
}
else if(temp==3) //moving Up
{
if(maze[lastRow-1][lastColumn]=='*' || lastRow-1<0)
flag=1;
else
{
nextColumn=lastColumn;
nextRow=--lastRow;

}
}
else
{ //moving down
if(maze[lastRow+1][lastColumn]=='*')
flag=1;
else
{
nextColumn=lastColumn;
nextRow=++lastRow;
}
}
}while(flag==1);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote