Modify the program so that the robot stops moving when it runs out of energy. Th
ID: 3568075 • Letter: M
Question
Modify the program so that the robot stops moving when it runs out of energy. This simply requires an additional test in the move() function to make sure that there is sufficient energy left. Now, the robots need a way to gain energy. Change the program so that moving into some types of terrain gives a robot energy. This requires a change in the update() function to check the new terrain and a new function in the robot class that is used to change the energy level (see the previous problem).
void robot::move(int &x, int &y){
if (moved ==1)
return;
switch (rand()%4){
case 0:
turnLeft();
break;
case 1:
turnRight();
break;
case 2:
case 3:
forward(x,y);
break;
default:
cout << "Error in robot move." << endl;
}
moved = 1;
}
update
void world::update(){
int tempx, tempy;
robot *temp;
for(int y =0; y < HEIGHT; y++){
for(int x = 0; x < WIDTH; x++){
if(bots[x][y] != NULL){
tempx = x;
tempy = y;
bots[x][y] -> move(tempx, tempy);
if(tempx < 0 || tempx >= WIDTH)
tempx = x;
if(tempy < 0 || tempy >= HEIGHT)
tempx = y;
if(bots [tempx][tempy] == NULL){
temp = bots[x][y];
bots[x][y] = NULL;
bots[tempx][tempy] = temp;
}
}
}
}
}
Explanation / Answer
#include #include #include #include using namespace std; // Manifest constants const int NROWS = 10; // no. of rows in the valley const int NCOLS = 10; // no. of columns in the valley const int MAXROBOTS = 10; // max no. of robots allowed const int MAXSOURCES = NROWS*NCOLS; // max no. of energy sources const int FULL_ENERGY = 60; // no. of units when fully charged const int SHARE_THRESHHOLD = 30; // will share energy if have at least this const int SHARE_AMOUNT = 10; // amount of energy to share // Type definitions enum Dir { NORTH, EAST, SOUTH, WEST }; class Valley; class Robot { public: // Constructor Robot(string nm, Valley* vp, int r, int c, Dir d); // Accessors string name() const; int energy() const; int row() const; int col() const; Dir dir() const; // Mutators bool step(); private: string m_name; int m_energy; int m_row; int m_col; Dir m_dir; Valley* m_valley; }; class EnergySource { public: // Constructor EnergySource(int r, int c); // Accessors int row() const; int col() const; private: int m_row; int m_col; }; class Valley { public: // Constructor Valley(); ~Valley(); // Accessors Robot* otherRobotAt(Robot* rp) const; bool energySourceAt(int r, int c) const; void display() const; // Mutators bool addRobot(string name, int r, int c, Dir dir); bool addEnergySource(int r, int c); bool step(); private: Robot* m_robots[MAXROBOTS]; int m_nrobots; EnergySource* m_sources[MAXROBOTS]; int m_nsources; }; // Robot implementation Robot::Robot(string nm, Valley* vp, int r, int c, Dir d): m_name(nm), m_energy(FULL_ENERGY), m_row(r), m_col(c), m_dir(d), m_valley(vp) { // Since the first character of the Robot's name shows up in the display, there had better be a first character. if (nm.size() == 0) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.