Question: How to activate the rover so that it can move itself forward, turn left or turn right in this code code : #include #include //for

How to activate the rover so that it can move itself forward, turn left or turn right in this code

code :

#include #include //for setw() #include #include #include #include //for time() in srand( time(NULL) ); #include //for Sleep() using namespace std;

class Mars { private: 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 Rover::land(Mars& mars) { char possibleHeading[] = {'^', '>', '

x = rand() % mars.getDimX() + 1; y = rand() % mars.getDimY() + 1; heading = possibleHeading[ rand() % 4 ]; mars.setObject(x,y,heading); }

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 0 && x

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; /umber of objects in the objects array cout "; cin>> dimX; cout "; 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

cout

cout

cout

void test1() { Mars mars; mars.display(); } void test2() { Mars mars; Mars mars2;

mars.display(); cout

int x,y; char obj;

x = 1; y = 1; obj = mars.getObject(x,y); cout

x = 5; y = 2; obj = mars.getObject(x,y); cout

x = 10; y = 4; obj = mars.getObject(x,y); cout

mars.setObject( 1, 1, 'A'); mars.setObject(15, 1, 'B'); mars.setObject(15, 5, 'C'); mars.setObject( 1, 5, 'D'); mars.display(); cout

void test5() { Mars mars; mars.setObject( 2, 4, 'Z'); mars.setObject( 10, 3, ' '); mars.display();

cout

cout

cout

void test6() { Mars mars; Rover curiosity; curiosity.land(mars); mars.display(); }

int main() { system("cls"); srand( time(NULL) ); test6(); return 0; }

How to activate the rover so that it can move itself forward,

turn left or turn right in this code code : #include #include

1.7 Let's activate the rover so that it can move itself forward, turn left or turn right. Consider the test function below: void test 7() Mars mars; Rover curiosity; curiosity.land (mars); mars.display(); system("pause"); curiosity.turnLeft (mars); mars.display(); system ("pause"); curiosity.move (mars); mars.display(); ypwong Page 7 of 8 TCP1101 Programming Fundamentals Lab09 Trimester 2. 2018/19 system("pause"); curiosity.move (mars); mars.display(); system("pause"); curiosity.turnRight (mars); mars.display(); system ("pause"); curiosity.move (mars); mars.display(); system ("pause"); } int main() //srand( time(NULL)); //different map each time the program is run srand( 1 ); //start with seed number 1, can be any other number //test 1(); //test2(); //test3(); //test 4(); //test5(); //test6(); test(); } N18819 Your task now is to write the necessary member functions for the class Rover so that the rover can navigate itself when the commands are provided as in the test function. Take note that all those functions take Mars as the input, this is logically enough as the rover can only move when it has a surface as a reference. In another words, the rover can only move if it can sense the world as represented as a map within the Mars class. There are other simpler ways to do this if you knew pointers. 1.7 Let's activate the rover so that it can move itself forward, turn left or turn right. Consider the test function below: void test 7() Mars mars; Rover curiosity; curiosity.land (mars); mars.display(); system("pause"); curiosity.turnLeft (mars); mars.display(); system ("pause"); curiosity.move (mars); mars.display(); ypwong Page 7 of 8 TCP1101 Programming Fundamentals Lab09 Trimester 2. 2018/19 system("pause"); curiosity.move (mars); mars.display(); system("pause"); curiosity.turnRight (mars); mars.display(); system ("pause"); curiosity.move (mars); mars.display(); system ("pause"); } int main() //srand( time(NULL)); //different map each time the program is run srand( 1 ); //start with seed number 1, can be any other number //test 1(); //test2(); //test3(); //test 4(); //test5(); //test6(); test(); } N18819 Your task now is to write the necessary member functions for the class Rover so that the rover can navigate itself when the commands are provided as in the test function. Take note that all those functions take Mars as the input, this is logically enough as the rover can only move when it has a surface as a reference. In another words, the rover can only move if it can sense the world as represented as a map within the Mars class. There are other simpler ways to do this if you knew pointers

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!