Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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; }

image text in transcribed

image text in transcribed

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

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

Step: 3

blur-text-image

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

More Books

Students also viewed these Databases questions