C++: Program 5 Rock, Paper, Scissors Using pointers, references, rand and overloaded functions This will be a multi-file program. Your main will be in Driver.cpp, your function prototypes will go in Functions.h, and your function definitions will go in Functions.cpp. You will also create a struct to hold the GameData. It will be defined in the .h file, before the prototypes. struct GameData { int tieCount = 0; int numRounds = 0; int userWins = 0; int compWins = 0; int numGames = 0; }; For this assignment, you are going to program the “Rock, Paper, Scissors” game. The user will play against the computer. A scissor can cut paper, a rock can knock a scissor, and pap
C++: Program 5 Rock, Paper, Scissors
Using pointers, references, rand and overloaded functions
This will be a multi-file program. Your main will be in Driver.cpp, your function prototypes will go in Functions.h, and your function definitions will go in Functions.cpp. You will also create a struct to hold the GameData. It will be defined in the .h file, before the prototypes.
struct GameData
{
int tieCount = 0;
int numRounds = 0;
int userWins = 0;
int compWins = 0;
int numGames = 0;
};
For this assignment, you are going to program the “Rock, Paper, Scissors” game. The user will play against the computer. A scissor can cut paper, a rock can knock a scissor, and paper can wrap a rock. One game of the Rock Paper Scissors will be such that your user will play continuously until the user or computer wins two times. That is, the game will stop when one of the players wins two rounds. The user may play as many games as they like.
- When your program begins, seed the rand with the system time and show your course header and game explanation by calling the WriteHeader and WriteGameRules Then open a do-while loop so the player can play the game as many times as they like. You will also have an inner do while loop for the play loop that runs until the user or the computer has won twice.
- Ask the user to select their choice (rock, paper, scissors) by calling the AskUserChoice function which displays a menu and asks the user to enter a number. Pass in a pointer for the user’s choice.
- Make sure the user’s choice is valid by passing it to the ValidateUserChoice If it is within the menu range, then return true. If not, build an error message to the user including the choice they entered and return it by the string pointer that was passed in and return false.
- If the user’s choice is valid, then the game play will continue by calling the ObtainCompChoice It is passed the user’s choice, a pointer for the computer’s choice, and a reference to the GameData object that contains the counter for ties. The program uses a random number generator to obtain the computer’s selection. Ties do not count toward the game play, so make sure the computer number is not the same as the user number before exiting the function. However, you should keep a tally of the ties for display purposes.
- After obtaining the computer’s choice you will need to evaluate who won by calling the DetermineWinner This function is passed the user choice, computer choice, and references to the GameData object that contains counters for user and computer wins. You will determine the winner based on the game rules, increment the winner’s tally, and build a message to the user that is returned to main. The message reports both the computer’s and user’s object, and who won that round. DetermineObject will be called from this function to get each player’s object name. It will be passed the player’s menu choice and a reference to the object string.
- Report the results of that round to the user by calling the WriteResults that is passed the result string, ties, user wins, and computer wins. Be sure to include number of valid rounds, wins for each player, and number of disallowed ties in the report.
- If the user’s choice is not valid, call the WriteResults that is passed the result error message string. After the message is displayed, the loop will start over.
- When one of the players has won two rounds, report the number of games played and who won the game, then ask if the user would like to play again. Be specific about how the user should answer. If the answer is yes, start the next game. When the user is done playing games, give a good bye message.
The functions you will need are:
Return Type |
Function Name |
Argument List |
Description |
void |
WriteHeader |
( ) |
Display the course header and any additional information to the user. |
void |
WriteGameRules |
( ) |
Display the rules of the game. |
void |
AskUserChoice |
(int *p_uChoice) |
Display a menu and allow the user to select a weapon type |
bool |
ValidateUserChoice |
(int uChoice, string *p_result) |
Verify that the user’s choice is within the menu range. If not, build a message to the user including the number they chose. |
void |
ObtainCompChoice |
(int uChoice, int *p_cChoice, GameData &r_data) |
Obtain the computer’s choice using rand(), but avoid matching the user’s choice. Keep a tally of the times it tries to tie. |
string |
DetermineWinner |
(int uChoice, int cChoice, GameData &r_data) |
Determines the winner based on the game rules, increments the winner’s tally, and builds a string message to the user. |
void |
DetermineObject |
(int choice, string &r_object) |
Determines whether numeric choice is a rock, paper, or scissors and assigns it to a string. |
void |
WriteResults |
(string result, GameData data) |
Display the result string plus valid rounds, user wins, computer wins, and ties. |
void |
WriteResults |
(string result) |
Display the result error message string. |
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images