Question

I'm trying to write this program in C++ and I'm having trouble initializing the overload function...

I'm trying to write this program in C++ and I'm having trouble initializing the overload function I have to use. Here's the assignment and what I got so far, thanks for the help!

/*2. Implement the game of Rock-Paper-Scissors in C++, where you can play against the computer.
a. Scissors beat paper, paper beats rock, and rock beats scissors. If both you and computer choose the same tool, it is a tie.
b. Your program should have a class game with one variable winCount and public functions to set and access winCount.
c. Create another class tool that inherits from game with one private property type.
This class should have appropriate methods or constructors to set and access the property type (for tool type r, p or s).
d. User will select a tool by entering a type r, p or s for rock, paper or scissors.
e. Computer will select a tool by generating a random number between 1 and 3. Random number 1 means r, 2 means p, and 3 means s.
Hint: To generate a random number between 1 and 3, you will need to include header file cstdlib and run the following statement:
int randomNumber = (rand() % 3) + 1;
f. Your program should produce an output like this:
**Enter e to end*** Enter a tool (r, p, s): p. You chose paper. Computer chose scissors. Computer wins... Your winnings: 0 Computer winnings: 1
**********************************. Enter a tool (r, p, s): s. You chose scissors
Computer chose scissors
It's a tie...
Your winnings: 0 Computer winnings: 1
**********************************
Enter a tool (r, p, s): r
You chose rock
Computer chose paper
Computer wins...
Your winnings: 0 Computer winnings: 2
**********************************
Enter a tool (r, p, s): s
You chose scissors
Computer chose paper
You win!!!
Your winnings: 1 Computer winnings: 2
********************************** */

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int add(int);
int add(int);
int add(int);
class game {
private:
   winCount;
public:
   void setWinCount(int w)
   {
       WinCount = w;
   }
   void getWinCount()
   {
       return winCount;
   }
};
class play : public game {
private:
  
   rock;
   paper;
   scissors;
public:
   void setRock(int r = 1)
   {
       rock = r;
   }
   void getRock()
   {
       return rock;
   }
   void setPaper(int p = 2)
   {
       paper = p;
   }
   void getPaper()
   {
       return paper;
   }
   void setScissors(int s = 3)

   {
       scissors = s;
   }
   void getScissors()
   {
       return scissors;
   }
   void getScore()
   {
       return getWinCount;
   }
};
int add(int getRock())
{
   return getRock();
}
int add(int getPaper())
{
   return getPaper();
}
int add(int getScissors())
{
   return getScissors();
}
int main()
{
   play ga;
   int randomNumber = (rand() % 3) + 1;
   int r;
   int p;
   int s;
   cout << "Let's play rock, paper scissors, enter r for rock, p for paper, and s for scissors." << endl;
   cin >> add(int getRock(), int getPaper(), int getScissors());
   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code is below

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

//return types of get functions should be the type of variable it returns not void
class game {
private:
int winCount;
public:
game()
{
winCount = 0;
}
void setWinCount(int w)
{
winCount = w;
}
int getWinCount()
{
return winCount;
}
};
class play : public game {
private:
  
int rock;
int paper;
int scissors;
public:
//constructor
play()
{
game();
rock = 0;
paper = 0;
scissors = 0;
}
void setRock(int r = 1)
{
rock = r;
}
int getRock()
{
return rock;
}
void setPaper(int p = 2)
{
paper = p;
}
int getPaper()
{
return paper;
}
void setScissors(int s = 3)

{
scissors = s;
}
int getScissors()
{
return scissors;
}
int getScore()
{
return getWinCount();
}
};

int main()
{
char choice,loop_char;   
int randomNumber;
int r,p,s;
int wincountuser =0, wincountcomp =0;
//dowhile loop for checking if user wants to continue

do{
cout << endl<<"Let's play rock, paper scissors, enter r for rock, p for paper, and s for scissors." << endl;
cin>>choice;
//use new objects everytime, otherwise previous values will be stored
play comp, user;
//use static variable for wincount since you didn't I contiued by storing a copy
comp.setWinCount( wincountcomp);
user.setWinCount(wincountuser);
randomNumber = rand() % 3 + 1;
//user choice set
switch(choice)
{
case 'r': user.setRock();
cout<<endl<<"You chose rock"<<endl; break;
case 'p': user.setPaper();
cout<<endl<<"You chose paper"<<endl;
break;
case 's': user.setScissors(); cout<<endl<<"You chose scissors"<<endl;
break;
}
//computer choice
switch(randomNumber)
{
case 1: comp.setRock(); cout<<endl<<"Computer chose rock"<<endl;break;
case 2: comp.setPaper();cout<<endl<<"Computer chose paper"<<endl;break;
case 3: comp.setScissors(); cout<<endl<<"Computer chose scissors"<<endl;break;
}

//possible 9 combinations, 3 for tie, 3 for comp win 3 for you win
if(user.getRock()== 1 && comp.getRock() == 1){
cout << "Its a tie!" << endl;
}
else if(user.getRock() ==1 && comp.getPaper()== 2){
cout << "Computer wins..." << endl;
comp.setWinCount(1+comp.getWinCount());
}
else if(user.getRock() == 1 && comp.getScissors() == 3){
cout << "You win!!!" << endl;
user.setWinCount(1+user.getWinCount());
}
else if(user.getPaper()== 2 && comp.getRock()== 1){
cout << "You win!!!" << endl;
user.setWinCount(1+user.getWinCount());
}
else if(user.getPaper() == 2 && comp.getPaper() == 2){
cout << "Its a tie!" << endl;
}
else if(user.getPaper() == 2 && comp.getScissors() == 3){
cout << "Computer wins..." << endl;
comp.setWinCount(1+comp.getWinCount());
}
else if( user.getScissors()== 3 && comp.getRock() == 1){
cout << "Computer wins.." << endl;
comp.setWinCount(1+comp.getWinCount());
}
else if(user.getScissors() == 3 && comp.getPaper() == 2){
cout <<"You win!!!" << endl;
user.setWinCount(1+user.getWinCount());
}
else if(user.getScissors() == 3 && comp.getScissors() == 3){
cout << "Its a tie!" << endl;
}
  
wincountuser = user.getWinCount(), wincountcomp = comp.getWinCount();
//display values
cout<<endl<<"Your winnings: "<<user.getWinCount();
cout<<endl<<"Computer Winnings"<<comp.getWinCount();
cout<<endl<<"Enter e to end";
cin>>loop_char;
//end only if user enters e
}while(loop_char != 'e');
return 0;
}

Add a comment
Know the answer?
Add Answer to:
I'm trying to write this program in C++ and I'm having trouble initializing the overload function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats...

    C++ Write a program that plays the rock, paper, scissors game. Rock beats scissors; scissors beats paper; paper beats rock. You must use these specific functions in your program. These are the prototypes: char generateP2toss(); int checkThrow(char, char); void printStatistics(int, int, int, int); void finalStatistics(int, int, int, int); void welcome(); Notes on these functions: generateP2toss() will generate the computer's toss (the computer is player2). It returns either an "r", "p", or "s/" checkThrow(char char) will check the throw by passing...

  • Use Dev C++ for program and include all of the following. Im lost. Make sure the...

    Use Dev C++ for program and include all of the following. Im lost. Make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch. Make sure the user knows why he/she has won or lost – print a message like “Paper covers Rock” or “Scissors cuts Paper”. Ask the user how many times he/she wants to...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Write a program in the Codio programming environment that allows you to play the game of...

    Write a program in the Codio programming environment that allows you to play the game of Rock / Paper / Scissors against the computer. Within the Codio starting project you will find starter code as well as tests to run at each stage. There are three stages to the program, as illustrated below. You must pass the tests at each stage before continuing in to the next stage.  We may rerun all tests within Codio before grading your program. Please see...

  • It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user...

    It's writing a simple rock paper scissors game strictly following the instructions. INSTRUCTIONS: If the user selects 'p': 1. First the program should call a function named getComputerChoice to get the computer's choice in the game. The getComputerChoice function should generate a random number between 1 and 3. If the random number is 1 the computer has chosen Rock, if the random number is 2 the user has chosen Paper, and if the random number is 3 the computer has...

  • This is a C++ question need to complete project 2. attached the completed simple version of...

    This is a C++ question need to complete project 2. attached the completed simple version of the program below but need the updated version which is listed in the project 2 outline Project 2 Rock, Paper, Scissors Updated Take the solution to Lab 3 and make sure the user enters a letter of R, P, or S and the computer generates a number between 0-2 and changes that number to a letter of R, P or S, using a switch....

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the...

    Rock, Paper, Scissors - Write a MATLAB script file to play Rock, Paper, Scissors with the computer. x=rand; if x< 1/3 then it is Rock, if l/3<= x < 2/3 it is Paper else it is Scissors end if This is how the game should look like on the screen: Enter "r" for Rock, "p" for Paper, or "s" for Scissors", or enter "q" to Quit the game r leftarrow say, this is what you entered Computer choice: Scissors RESULT:...

  • Python Language: Please see program listed below of a game called: Rock, Paper, Scissors. Please add...

    Python Language: Please see program listed below of a game called: Rock, Paper, Scissors. Please add an exception, for example if the user inputs something else other than rock, paper or scissors, the exception will print message: "Enter only rock, paper, or scissors". Also add option to keep playing the game, for example: print: "Would you like to play again? Enter Y or N". If the user says Y, the game will repeat, if N, the game will end and...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT