Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ please complete the functions ( void Rover::move(Mars& mars) ) and ( void Rover::autoMove(Mars& mars) ) to run // test6 and correct the errors if

/ please complete the functions ( void Rover::move(Mars& mars) ) and ( void Rover::autoMove(Mars& mars) ) to run

// test6 and correct the errors if there is any , thanks

#include

#include //for setw()

#include

#include

#include

#include //for time() in srand( time(NULL) );

// #include //for Sleep()

using namespace std;

class Mars {

private:

vector < vector > map;

int dimX, dimY;

public:

Mars(){

init(); }

void init();

void display();

int getDimX() const;

int getDimY() const;

char getObject(int,int) const;

void setObject( int, int, char);

bool isEmpty(int,int);

bool isInsideMap(int,int);};

class Rover

{private:

int x,y;

char heading;

public:

Rover(){}

void land(Mars& mars);

void turnLeft(Mars& mars);

void turnRight(Mars& mars);

void move(Mars& mars);

void autoMove(Mars& mars);};

void Rover::land(Mars& mars)

{char possibleHeading[] = {'^', '>', '<', 'v'};

x = rand() % mars.getDimX() + 1;

y = rand() % mars.getDimY() + 1;

heading = possibleHeading[ rand() % 4 ];

mars.setObject(x,y,heading);}

void Rover::turnLeft(Mars& mars)

{ if (heading == '^') heading = '<';

else if (heading == '>') heading = '^';

else if (heading == '<') heading = 'V';

else heading = '>';

mars.setObject(x,y,heading);}

void Rover::turnRight(Mars& mars){}

void Rover::move(Mars& mars){

// Step 1: get the current x and y of rover and the current heading (direction)

// Step 2: if the direction os for example >, then change the new position of the rover to mars.setObject(x+1, y, heading)

// Do the same for all the directions

// Before moving, check if the new coordinates are empty (Example mars.isEmpty(x+1, y))

}

void Rover::autoMove(Mars& mars)

{ while (true) // infinite loop or until user quits

{ // check if the next tile in the current heading (direction) is empty

// if(mars.isEmpty(...)) move();

// else if not empty, randomly select to go right or left, then

turnRight(mars); // or turnLeft(mars); }}

bool Mars::isEmpty(int x,int y)

{ if (map[dimY-y][x-1] == ' ' )

return true;

else

return false;}

bool Mars::isInsideMap(int x,int y)

{ if((y > 0 && y <= dimY) && (x > 0 && x<=dimX))

return true;

else

return false;}

void Mars::setObject( int x, int y, char ch)

{ map[dimY-y][x-1]= ch;}

char Mars::getObject(int x,int y) const{

return map[dimY-y][x-1];}

int Mars::getDimX() const

{ return dimX;}

int Mars::getDimY() const

{ return dimY;}

void Mars::init()

{ char objects[] = {' ',' ', ' ', ' ', ' ', ' ','X', '#', '@', '$' };

int noOfObjects = 10; //number of objects in the objects array

cout << "enter the x axis => ";

cin>> dimX;

cout << "enter the y axis => ";

cin>> dimY;

//create dynamic 2D array using vector

map.resize(dimY); //create rows

for (int i=0; i

map[i].resize(dimX); //resize each rows }

//put random chars into the vector array

for (int i=0; i

{ for (int j=0; j

{ int objNo = rand() % noOfObjects;

map[i][j] = objects[ objNo ];} } }

void Mars::display()

{ system("clear");

cout << " --__--__--__--__--__--__--__--_" << endl;

cout << " = Curiosity, welcome to Mars! =" << endl;

cout << " __--__--__--__--__--__--__--__-" << endl;

cout << endl;

for (int i=0; i

{ cout << " ";

for (int j=0; j

cout << "+-"; }

cout << "+" << endl;

cout << setw(2) << (dimY-i);

for (int j=0; j

cout << "|" << map[i][j];}

cout << "|" << endl;}

cout << " ";

for (int j=0; j

cout << "+-";}

cout << "+" << endl;

cout << " ";

for (int j=0; j

{ int digit = (j+1)/10;

cout << " ";

if (digit==0)

cout << " ";

else

cout << digit;}

cout << endl;

cout << " ";

for (int j=0; j

cout << " " << (j+1)%10;}

cout << endl << endl;}void test6()

// the test function

void test6()

{

Mars mars;

Rover curiosity;

curiosity.land(mars);

mars.display();

system("pause");

curiosity.turnLeft(mars);

mars.display();

}

int main()

{ system("cls");

srand( time(NULL) );

test6();

return 0;}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

Students also viewed these Databases questions

Question

What is the difference between cost allocation and cost assignment?

Answered: 1 week ago

Question

Understand how environmental scanning is practised.

Answered: 1 week ago