Question: C++ Only Write a class named Player. The class contains two attributes: name (string) and id (integer) of a Player. You must implement the following

C++ Only

Write a class named Player. The class contains two attributes: name (string) and id (integer) of a Player. You must implement the following functions for the class:

Default Constructor: Assign empty string to name and 0 to id.

Overloaded Constructor: Take a string and int as parameters in the constructor and initialize the object.

Make two getter functions to return name or id of the player and make two setter functions to set the values of name or id of the player.

rollADice(): The function rolls a dice (like in ludo), that is, it randomly generates a number between 1 and 6 (inclusive), and returns it. Include library, and use rand() % 6 + 1, to generate a number between 1 and 6.

Using the class above, make another class named Team. In this class, you will have following private attributes:

An array of N Player objects, where N is a constant. Shall be initialized with 3.

An int variable to store number of current Players in the array. Must be between 0 and N.

An int variable to store current score

A string variable to store name of the team

You must implement following functions for this class as well:

Default Constructor: Assign 0 to number of players and current score and empty string to name.

Overloaded Constructor: Take as parameters, a string for team name, and an int for number of players. Always set current score to 0 when object is created. In this function, you will take input from user the names of every player and save them in the array. The id of the player will be generated incrementally. The first player will have id 1, the next player will have id 2, and so on. When creating a Team object in the main function, pass number of players as N to this parameterized constructor so that this constructor takes N players as input from user.

printTeam(): This function simply prints the name of team, number of players in team, and the information (id, name) of all players using cout.

chooseAndPlay(): This function chooses a random player from the team and prints its id and name. Then, this player rolls the dice (remember the function we created in the Player class), and that dice value is returned from this function as an int.

Add appropriate getters/setters and utility/helper functions needed as per use.

Write a testPlay() function, that creates two teams, prints the teams using the printTeam() function defined above, and then starts a match between the two teams. Call the testPlay() function in main().

The match is played as follow:

Create an infinite while loop which takes user input at the start of every iteration.

The number of rounds is infinite, until user inputs stop. Implement an appropriate condition.

If the user inputs anything other than stop, the next round begins. In each round a random player is picked from each team using the chooseAndPlay() function.

The player who rolls the higher number wins the round, and the respective teams current score is increased by one. In case of a tie, no point is rewarded to any team.

When the user has had enough and inputs stop, the game stops and displays the winning Team (use the team print function again).

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!