CREATE A .h file first please! File name: robot101class.h robot101class.cpp test
ID: 3735416 • Letter: C
Question
CREATE A .h file first please! File name: robot101class.h robot101class.cpp test_robot101class.cpp Givens: OP repository. bbs_ROBOT101_class -- class diagram (use to code robot101class.h, and implementation robot101class.cpp) starter-test_robot101class.cpp -- sample test program for testing the ROBOT101class. key-robot101class.o -- correct class implementation (use to test your test program while other group members implement ROBOT101class. To use: you need your robot101class.h file > g++ yourtest.cpp key-robot101class.o -o trc > ./trc Description: Write program to simulate the movement of a robot on a 2-dimensional grid (xMin,yMin) to (xMax, yMax). Simple moves of distance one are supported: L R U D (left). Each attempted move consumes 1 unit of power. The robot is positioned on the grid, then powered on (and given its ID, its power level, the grid dimensions, and its starting position). If the start position is off the grid, the robot issues a message and sets the start position to (0,0). The robot can not leave the grid. For any move that attempts to leave the grid, the robot must report "BAD MOVE to POSITION (xbad,ybad)" but not perform the move. (The attempt DOES consume 1 unit of power.) The robot turns itself off (is no longer active) when the power level falls to zero following a move, or when a shut-down command is processed. All moves are denied when the robot in INACTIVE. The robot controller (a main programi -- PROG7) enables a user to command the robot through keyboard commands: Start (S): 1. read the following from keyboard: - robot ID (a string) - initial power level - grid dimensions (xMin, yMin) (xMax, yMax) - starting position (x, y). 2. - Activate the robot and initialize its with the information in step 1. The robot controller must respond to these commands: WhereIam (W) -- display its current (x,y) grid location. Movement (L, R, U, D) -- each move consumes one unit of power. Power (P) -- display the remaining power level. Terminate(T) --report current position and terminate. Moves (M) -- list the sequence of valid moves. Raw Distance Moved (r) -- display total distance moved. Net Distance Moved (n) -- display net distance traveled from start position to current position.
Explanation / Answer
**********robot101class.h*******
#ifndef ROBOT101CLASS_H
#define ROBOT101CLASS_H
#include<iostream>
#include<vector>
using namespace std;
class robot101class
{
private:
string robotId;
int xMin;
int xMax;
int yMin;
int yMax;
int power;
int rawmoved;
int curX;
int curY;
int iniX ;
int iniY;
int state ;
vector<char> moveList;
public:
void initRobot(string robotId1, int xMin1,int yMin1,int xMax1,int yMax1,int power1,int iniX1,int iniY1);
void whereIam();
void Movement(char move);
void Power();
void Terminate();
void Moves();
void NetDistanceMoved();
void RawDistanceMoved();
void handleCommand(char command);
};
#endif
**********robot101class.cpp**********
#include<iostream>
#include "robot101class.h"
#include<math.h>
using namespace std;
void robot101class::initRobot(string robotId1, int xMin1,int yMin1,int xMax1,int yMax1,int power1,int iniX1,int iniY1)
{
robotId = robotId1;
xMin = xMin1;
yMin = yMin1;
xMax = xMax1;
yMax = yMax1;
iniX = iniX1;
iniY = iniY1;
power = power1;
rawmoved = 0;
if(iniX <= xMax && iniY <= yMax && iniX >= xMin && iniY >= yMin)
{curX =iniX;
curY= iniY;}
else {
cout << "Invalid initial coordinates ";
curX = 0 ; curY = 0; iniX = 0; iniY = 0;
}
state = 1;
};
void robot101class::whereIam()
{
cout << "current position is : " << "(" << curX << "," << curY << ") ";
};
void robot101class::Movement(char move)
{
int status = 0;
if(state == 0 )
{
cout << "Robot is in inactive state " ;
return ;
}
if(move == 'L')
{
if(curX - 1 >= xMin)
{
curX = curX - 1 ;
status = 1;
}
else cout << "bad move to position (" << curX-1 << "," << curY << ") ";
}
else if(move == 'R')
{
if(curX + 1 <= xMax)
{
curX = curX + 1 ;
status = 1;
}
else cout << "bad move to position (" << curX+1 << "," << curY << ") ";
}
else if(move == 'D')
{
if(curY -1 >= yMin)
{
curY = curY - 1 ;
status = 1;
}
else cout << "bad move to position (" << curX << "," << curY-1 << ") ";
}
else if(move == 'U')
{
if(curY +1 <= yMax)
{
curY = curY + 1 ;
status = 1;
}
else cout << "bad move to position (" << curX << "," << curY+1 << ") ";
}
if(status == 1)
{
moveList.push_back(move); power--; rawmoved++;
if(power == 0 ) state =0;
}
}
void robot101class::Power()
{
cout << "current power is : " << power << " ";
}
void robot101class::Terminate()
{ state = 0;
}
void robot101class::Moves()
{
cout << "Moves List : ";
for(int i=0;i<moveList.size();i++) cout << moveList[i] << " ";
cout << " ";
}
void robot101class::NetDistanceMoved()
{
cout << "net distance travelled : " << sqrt(pow(iniX-curX,2)+pow(iniY-curY,2)) << " ";
}
void robot101class::RawDistanceMoved()
{
cout << "raw distance travelled : " << rawmoved << " ";
}
void robot101class::handleCommand(char command)
{
if(command == 'R' || command == 'L' || command == 'U' || command == 'D') Movement(command);
else if(command == 'W') whereIam();
else if(command == 'P' ) Power();
else if(command == 'T') Terminate();
else if(command == 'M') Moves();
else if(command == 'r') RawDistanceMoved();
else if(command == 'n') NetDistanceMoved();
}
*********test.cpp************
#include<iostream>
#include "robot101class.h"
using namespace std;
int main()
{
robot101class robot;
string robotId;
int power;
int xMin,yMin,xMax,yMax;
int iniX,iniY;
cout << "Initiating robot.... ";
cout << "Enter robotId : ";
cin >> robotId;
cout << "power : ";
cin >> power;
cout << "Enter Xmin Ymin Xmax Ymax respectively : ";
cin >> xMin >> yMin >> xMax >> yMax;
cout << "Enter iniX iniY respectively : ";
cin >> iniX >> iniY;
robot.initRobot(robotId, xMin,yMin,xMax,yMax,power,iniX,iniY);
while(true)
{
char command;
cout << " Enter Command W,L,R,U,D,M,P,n,r : ";
cin >> command;
robot.handleCommand(command);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.