Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We\'re going to revisit the robot code from the \'functions\' lab and turn the c

ID: 3920243 • Letter: W

Question

We're going to revisit the robot code from the 'functions' lab and turn the code into a class called 'Robot'. We're then going to make a multiplayer game where players race to build their robots. This will use a vector of robot objects.

You will use the RobotController class to managed both the game, players and Robots.

You need to complete the RobotController and Robot classes based onthe code I provide in a later slide.

Using the rules from the previous robot (repeated on the next slide), make a robot with the following methods to the Robot class

Robot();             // Constructor that initializes the robot to 'empty'
bool addPart();      // Part will be 'Leg' or 'Hand', etc. Gives the Robot that part only
                    // if the rules allow it (This is where the logic resides) return true
                    // if the part was added, false otherwise
int countPart();     // Returns the number of parts of that type added
bool complete();     // Returns true if the robot is complete or false if not
void print();        // Prints the robot part counts in a single neatly formatted line
void setName(string name); // Set the 'name' of the player using that robot
string getName();    // Returns the name of the Robot

Using the rules from the previous robot (repeated on the next slide), make a robot with the following methods to the Robot class

Robot();             // Constructor that initializes the robot to 'empty'
bool addPart();      // Part will be 'Leg' or 'Hand', etc. Gives the Robot that part only
                    // if the rules allow it (This is where the logic resides) return true
                    // if the part was added, false otherwise
int countPart();     // Returns the number of parts of that type added
bool complete();     // Returns true if the robot is complete or false if not
void print();        // Prints the robot part counts in a single neatly formatted line
void setName(string name); // Set the 'name' of the player using that robot
string getName();    // Returns the name of the Robot

If the dice roll is,

you may add this part

if you already have this number of parts

and no more than this amount.

1

Foot

Any. You can always add a Foot

2

2

Leg

A free Foot. If the number of Feet is more than the number of Legs, you can add a Leg

2

3

Torso

2 Legs

1

4

Arm

Torso

2

5

Hand

A free Arm. If the number of Arms is more than the number of Hands, you can add a Hand.

2

6

Head

Torso

1

7

Antenna

Head

1

8

Laser

Hand

1

This class maintains the vector<Robot> and runs the game. If requires the following methods:

void initializeGame(int numPlayers);

void playGame();

These are described in the template code I've provided on a later slide.

If the dice roll is,

you may add this part

if you already have this number of parts

and no more than this amount.

1

Foot

Any. You can always add a Foot

2

2

Leg

A free Foot. If the number of Feet is more than the number of Legs, you can add a Leg

2

3

Torso

2 Legs

1

4

Arm

Torso

2

5

Hand

A free Arm. If the number of Arms is more than the number of Hands, you can add a Hand.

2

6

Head

Torso

1

7

Antenna

Head

1

8

Laser

Hand

1

Explanation / Answer

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <ctime>

using namespace std;

// Class Robot definition
class Robot
{
public:
// Prototype of member functions
Robot();
bool addPart(int);
int countPart(int);
bool complete();
void setName(string name);
string getName();
void print();
private:
// Data member to store data
string name;
int Foot;
int Leg;
int Torso;
int Arm;
int Hand;
int Head;
int Antenna;
int Laser;
};// End of class Robot

// Default constructor definition
Robot::Robot()
{
name = "";
Foot = Leg = Torso = Arm = Hand = Head = Antenna = Laser = 0;
}// End of default constructor

// Function to return true if the type of part can be added as per rules otherwise returns false
bool Robot::addPart(int type)
{
// Initializes add status to false
bool status = false;
// Checks the type of part to be added
switch(type)
{
// For Foot
case 1:
// Calls the function to return number of foots robot current having
// Checks if number of foot is less than two
if(countPart(1) < 2)
{
// Increase the foot counter by one
Foot++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 2:
// Calls the function to return number of leg robot current having
// Checks if number of leg is less than two
if(countPart(2) < 2)
{
// Increase the leg counter by one
Leg++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 3:
// Calls the function to return number of torso robot current having
// Checks if number of torso is less than one
if(countPart(3) < 1)
{
// Increase the torso counter by one
Torso++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 4:
// Calls the function to return number of arm robot current having
// Checks if number of arm is less than tow
if(countPart(4) < 2)
{
// Increase the arm counter by one
Arm++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 5:
// Calls the function to return number of hand robot current having
// Checks if number of hand is less than two
if(countPart(5) < 2)
{
// Increase the hand counter by one
Hand++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 6:
// Calls the function to return number of head robot current having
// Checks if number of head is less than one
if(countPart(6) < 1)
{
// Increase the head counter by one
Head++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 7:
// Calls the function to return number of antenna robot current having
// Checks if number of antenna is less than one
if(countPart(7) < 1)
{
// Increase the antenna counter by one
Antenna++;
// Reset the status to true
status = true;
}// End of if condition
break;
case 8:
// Calls the function to return number of laser robot current having
// Checks if number of laser is less than one
if(countPart(8) < 1)
{
// Increase the laser counter by one
Laser++;
// Reset the status to true
status = true;
}// End of if condition
break;
}// End of switch - case
// Return the status
return status;
}// End of function

// Function to return number of parts of a specific type passed as parameter
int Robot::countPart(int type)
{
// Checks the type and return the number
switch(type)
{
case 1:
return Foot;
break;
case 2:
return Leg;
break;
case 3:
return Torso;
break;
case 4:
return Arm;
break;
case 5:
return Hand;
break;
case 6:
return Head;
break;
case 7:
return Antenna;
break;
case 8:
return Laser;
break;
}// End of switch - case
}// End of function

// Function returns true if game over otherwise returns false
bool Robot::complete()
{
// Initializes game over status to false
bool status = false;
// Checks if all the parts of the robot is full then set the game over status to true
if(countPart(1) == 2 && countPart(2) == 2 && countPart(3) == 1 && countPart(4) == 2 && countPart(5) == 2 && countPart(6) == 1
&& countPart(7) == 1 && countPart(8) == 1)
status = true;
// Returns the status
return status;
}// End of function

// Function to set the name of the robot
void Robot::setName(string name)
{
this->name = name;
}// End of function

// Function to return robot name
string Robot::getName()
{
return name;
}// End of function


// Function to print the robot in a single line of text
void Robot::print()
{
cout << " Robot: Name: "<<name<<" Foot: "<<Foot<<" Leg: "<<Leg<<" Torso: "<<Torso<<" Arm: "<<Arm;
cout<<" Hand: "<<Hand<<" Head: "<<Hand<<" Antenna: "<<Antenna<<" Laser: "<<Laser;
cout << endl;
}// End of function

// Defines a class RobotController as the game manager
class RobotController
{
public:
// Prototype of member function
void initializeGame(int numPlayers);
void playGame();
private:
// Declares a vector to store Robot class objects
vector<Robot> m_Robots;
vector<string> playerName;
};// End of class RobotController

// Function to set of the game for the correct number of players
void RobotController::initializeGame(int numPlayers)
{
// Declares robot class object using default constructor
Robot newRobot;
// To store the name of the robot
string namePlayer, nameRobot;
// Clears the vector or robots (if necessary) and put in one robot per player
m_Robots.clear();

for(int x = 0; x < numPlayers; x++)
{
// Asks for the names of each player, and assign that name to each robot
cout<<" Enter the name of the player: ";
getline(cin, namePlayer);
cout<<" Enter the name of the robot: ";
getline(cin, nameRobot);
newRobot.setName(nameRobot);
m_Robots.push_back(newRobot);
playerName.push_back(namePlayer);
}// End of for loop
}// End of function

// Function to play a single game
void RobotController::playGame()
{
// Initializes game finish status to true
bool finishStatus = true;
// Loops till game finish status is not false
do
{
// Generates random seed val
srand(time(NULL));
// Loops till number of players
for(int x = 0; x < m_Robots.size(); x++)
{
// Generates random number between 1 and 8
int type = rand() % ((8 - 1) + 1) + 1;
// Calls the function to add the robot part if it returns true display the information
if(m_Robots.at(x).addPart(type))
{
// Displays the current type added with player name
cout<<" Player name: "<<playerName[x]<<" Current type: "<<type;
cout<<" Current Robot: ";
m_Robots[x].print();
}// End of if condition

// Calls the function to check game over status.
if(m_Robots.at(x).complete() == true)
{
// Sets the finish status to false
finishStatus = false;
// Calls the function to display the winner
cout<<" Winner of the game is: "<<playerName[x];
m_Robots[x].print();
break;
}// End of if condition
}// End of for loop
// Checks if game over status is false then come out of the loop
if(finishStatus == false)
break;
}while(1);// End of do - while loop
}// End of function

// main function definition
int main()
{
// To store number of player string version
string inputBuffer;
// To store number of player integer version
int numberOfPlayers;
// Creates an object of the class RobotController using default constructor
RobotController gameManager;

// Loops till valid player number entered by the user
do
{
// Accepts number of players
cout << " How many players: ";
getline(cin, inputBuffer);
// Converts the data to integer
numberOfPlayers = atoi( inputBuffer.c_str() );
}
// Checks if the number of player is between 1 and 9
while (numberOfPlayers < 1 || numberOfPlayers > 9); // End of do - while loop


// Loops till user choice is not 'n'
do
{
// Calls the function initialize players
gameManager.initializeGame(numberOfPlayers);
// Calls the function to play the game
gameManager.playGame();
// Accepts user choice to continue game
cout << endl << "Play Again? [y/n]";
getline(cin, inputBuffer);
}while ( tolower(inputBuffer[0]) != 'n' ); // End of do - while loop
return 0;
}// End of main function

Sample Output:

How many players: 3

Enter the name of the player: Pyari

Enter the name of the robot: Robot1

Enter the name of the player: Suresh

Enter the name of the robot: Robot3

Enter the name of the player: Anita

Enter the name of the robot: Robot9

Player name: Pyari Current type: 6
Current Robot:
Robot: Name: Robot1 Foot: 0 Leg: 0 Torso: 0 Arm: 0 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Suresh Current type: 8
Current Robot:
Robot: Name: Robot3 Foot: 0 Leg: 0 Torso: 0 Arm: 0 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Anita Current type: 4
Current Robot:
Robot: Name: Robot9 Foot: 0 Leg: 0 Torso: 0 Arm: 1 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Anita Current type: 4
Current Robot:
Robot: Name: Robot9 Foot: 0 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Pyari Current type: 1
Current Robot:
Robot: Name: Robot1 Foot: 1 Leg: 0 Torso: 0 Arm: 0 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Suresh Current type: 4
Current Robot:
Robot: Name: Robot3 Foot: 0 Leg: 0 Torso: 0 Arm: 1 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Pyari Current type: 1
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 0 Arm: 0 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Suresh Current type: 4
Current Robot:
Robot: Name: Robot3 Foot: 0 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Pyari Current type: 4
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 0 Arm: 1 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Suresh Current type: 1
Current Robot:
Robot: Name: Robot3 Foot: 1 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Pyari Current type: 4
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 0

Player name: Suresh Current type: 1
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Pyari Current type: 8
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 0 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Suresh Current type: 5
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 0 Torso: 0 Arm: 2 Hand: 1 Head: 1 Antenna: 0
Laser: 1

Player name: Suresh Current type: 5
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 0 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 1

Player name: Pyari Current type: 3
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 1 Arm: 2 Hand: 0 Head: 0 Antenna: 0
Laser: 1

Player name: Suresh Current type: 2
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 1 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 1

Player name: Anita Current type: 5
Current Robot:
Robot: Name: Robot9 Foot: 0 Leg: 0 Torso: 0 Arm: 2 Hand: 1 Head: 1 Antenna: 0
Laser: 0

Player name: Suresh Current type: 2
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 2 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 1

Player name: Anita Current type: 5
Current Robot:
Robot: Name: Robot9 Foot: 0 Leg: 0 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 0

Player name: Suresh Current type: 6
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 2 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 1

Player name: Pyari Current type: 5
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 1 Arm: 2 Hand: 1 Head: 1 Antenna: 0
Laser: 1

Player name: Suresh Current type: 7
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 2 Torso: 0 Arm: 2 Hand: 2 Head: 2 Antenna: 1
Laser: 1

Player name: Pyari Current type: 5
Current Robot:
Robot: Name: Robot1 Foot: 2 Leg: 0 Torso: 1 Arm: 2 Hand: 2 Head: 2 Antenna: 0
Laser: 1

Player name: Suresh Current type: 3
Current Robot:
Robot: Name: Robot3 Foot: 2 Leg: 2 Torso: 1 Arm: 2 Hand: 2 Head: 2 Antenna: 1
Laser: 1

Winner of the game is: Suresh
Robot: Name: Robot3 Foot: 2 Leg: 2 Torso: 1 Arm: 2 Hand: 2 Head: 2 Antenna: 1
Laser: 1

Play Again? [y/n]n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote