Question

i have created a program for a game of rock, paper, scissors but i must make...

i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop.

i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program

in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this game.

in Computer vs Computer, the user will only be asked to specify the number of rounds for this game.

in either scenario, runGame must verify that that user attempted to intiate a game with an odd number of rounds. once a valid number of rounds has been established, create a dynamically-allocated array or vector to store the winner of each round. the size of the array/vector will correspond to the number of rounds per game.

Each round must make a call to getOutcome and is considered complete when a winner and loser have been determined. a round is repeated when a draw outcome is returned.

at the end of each round, the winner is printed to the terminal and stored in the dynamically allocated array or vector created in runGame.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int getCpu();
int getComputerChoice();
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int);

int main()
{
int userChoice;
    int playerChoice;
    int computerChoice;
cout << "welcome to rock paper scissors" << endl;
cout <<"Please select a game mode" << endl;
cout << "1. Player vs. Computer" << endl;
cout << "2. Computer vs. Computer" << endl;
cout << "3. Exit" << endl;
cin >> userChoice;

if (userChoice == 1)
{
playerChoice = getPlayerChoice();
computerChoice = getComputerChoice();

if (isTie(playerChoice, computerChoice))
{
cout << "It's a TIE!\n\n";
}

else if (!isPlayerWinner(playerChoice, computerChoice))
{
cout << "Sorry you LOSE.\n\n";
}

else if (isPlayerWinner(playerChoice, computerChoice))
{
cout << "You WIN!\n\n";
}

}

else if (userChoice == 2)
{
playerChoice = getCpu();
computerChoice = getComputerChoice();

if (isTie(playerChoice, computerChoice))
{
cout << "It's a TIE!\n\n";
}

else if (!isPlayerWinner(playerChoice, computerChoice))
{
cout << "Player 2 WIN!\n\n";
}

else if (isPlayerWinner(playerChoice, computerChoice))
{
cout << "Player 1WIN!\n\n";
}

}
else if (userChoice != 2 || userChoice != 1)
{
cout << "Invalid selection. Try again.\n\n";
}

   
return 0;
}

int getComputerChoice()
{

srand(time(NULL));
int randomCompNum = rand() % 3 + 1;

if (randomCompNum == 1)
{
cout << "The computer chose : Rock\n\n";
}

else if (randomCompNum == 2)
{
cout << "The computer chose : Paper\n\n";
}

else if (randomCompNum == 3)
{
cout << "The computer chose : Scissors\n\n";
}

return randomCompNum;
}

int getCpu()
{

srand(time(NULL));
int cpu = rand() % 6;

if (cpu == 1 || cpu == 4)
{
cout << "player 1 chose : Rock\n\n";
}

else if (cpu == 2 || cpu == 5)
{
cout << "player 1 chose : Paper\n\n";
}

else if (cpu == 3 || cpu == 6)
{
cout << "player 1 chose : Scissors\n\n";
}

return cpu;
}

int getPlayerChoice()
{
int myChoice;

cout << "\n\nRock, Paper, or Scissors?\n"
<< "1) Rock\n"
<< "2) Paper\n"
<< "3) Scissors\n"
<< "Please enter your choice : \n";
cin >> myChoice;

if (myChoice == 1)
{
cout << "\nYou chose : Rock\n";
}

else if (myChoice == 2)
{
cout << "\nYou chose : Paper\n";
}

else if (myChoice == 3)
{
cout << "\nYou chose : Scissors\n";
}

return myChoice;

while (myChoice < 1 || myChoice > 3)
{
cout << "Please pick a number between 1 & 3.\n";
cin >> myChoice;
}

}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
if (((myChoice == 1) && (randomCompNum == 3)) || ((myChoice == 3) && (randomCompNum == 2)) ||
((myChoice == 2) && (randomCompNum == 1)))
{
return true;
}

else if (((randomCompNum == 3) && (myChoice == 1)) || ((randomCompNum == 3) && (myChoice == 2)) || ((randomCompNum == 2) && (myChoice == 1)))
{
return false;
}
return 0;
}

bool isTie(int myChoice, int randomCompNum)
{
if (myChoice == randomCompNum)
{
return true;
}

else if (myChoice != randomCompNum)
{
return false;
}
return 0;
}

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

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <vector>
#include <ctime>
using namespace std;

int getCpu();
int getComputerChoice();
int getPlayerChoice();
bool isTie(int, int);
bool isPlayerWinner(int, int);
int menu();
void runGame();

int main()
{
runGame();

return 0;
}

void runGame()
{
int userChoice;
int playerChoice;
int computerChoice;
int rounds;
vector <string> winner;

do
{
winner.clear();
userChoice = menu();
if (userChoice == 1)
{
do
{
cout<<"\n Enter how many rounds to play? ";
cin>>rounds;
if(rounds % 2 == 0)
break;
else
cout<<"\n Enter a even number of rounds. \t Try again!!";
}while(1);

for(int c = 0; c < rounds; c++)
{
playerChoice = getPlayerChoice();
computerChoice = getComputerChoice();

if (isTie(playerChoice, computerChoice))
cout << "It's a TIE!\n\n";

else if (!isPlayerWinner(playerChoice, computerChoice))
{
cout << "Sorry you LOSE.\n\n";
winner.push_back("Computer WIN!");
}

else if (isPlayerWinner(playerChoice, computerChoice))
{
winner.push_back("Player WIN!");
cout << "You WIN!\n\n";
}
}
cout<<"\n ********** Final Result (Player vs. Computer) ********** ";
for(int c = 0; c < winner.size(); c++)
cout<<"\n"<<winner[c];
}

else if (userChoice == 2)
{
do
{
cout<<"\n Enter how many rounds to play? ";
cin>>rounds;
if(rounds % 2 == 0)
break;
else
cout<<"\n Enter a even number of rounds. \t Try again!!";
}while(1);

for(int c = 0; c < rounds; c++)
{
playerChoice = getCpu();
computerChoice = getComputerChoice();

if (isTie(playerChoice, computerChoice))
cout << "It's a TIE!\n\n";

else if (!isPlayerWinner(playerChoice, computerChoice))
{
cout << "Player 2 WIN!\n\n";
winner.push_back("Player 2 WIN!");
}

else if (isPlayerWinner(playerChoice, computerChoice))
{
cout << "Player 1WIN!\n\n";
winner.push_back("Player 1 WIN!");
}
}
cout<<"\n ********** Final Result (Computer vs. Computer) ********** ";
for(int c = 0; c < winner.size(); c++)
cout<<"\n"<<winner[c];
}
else if(userChoice == 3)
exit(0);
else
cout << "Invalid selection. Try again.\n\n";
}while(1);

}
int menu()
{
int userChoice;
cout << "\n\n Welcome to rock paper scissors" << endl;
cout <<"Please select a game mode" << endl;
cout << "1. Player vs. Computer" << endl;
cout << "2. Computer vs. Computer" << endl;
cout << "3. Exit" << endl;
cin >> userChoice;
return userChoice;
}
int getComputerChoice()
{
srand(time(NULL));
int randomCompNum = rand() % 3 + 1;
if (randomCompNum == 1)
{
cout << "The computer chose : Rock\n\n";
}

else if (randomCompNum == 2)
{
cout << "The computer chose : Paper\n\n";
}

else if (randomCompNum == 3)
{
cout << "The computer chose : Scissors\n\n";
}

return randomCompNum;
}

int getCpu()
{
srand(time(NULL));
int cpu = rand() % 6;

if (cpu == 1 || cpu == 4)
{
cout << "player 1 chose : Rock\n\n";
}

else if (cpu == 2 || cpu == 5)
{
cout << "player 1 chose : Paper\n\n";
}

else if (cpu == 3 || cpu == 6)
{
cout << "player 1 chose : Scissors\n\n";
}
return cpu;
}

int getPlayerChoice()
{
int myChoice;
cout<< "\n\nRock, Paper, or Scissors?\n"
<< "1) Rock\n"
<< "2) Paper\n"
<< "3) Scissors\n"
<< "Please enter your choice : \n";
cin >> myChoice;

if (myChoice == 1)
{
cout << "\nYou chose : Rock\n";
}
else if (myChoice == 2)
{
cout << "\nYou chose : Paper\n";
}
else if (myChoice == 3)
{
cout << "\nYou chose : Scissors\n";
}

return myChoice;

while (myChoice < 1 || myChoice > 3)
{
cout << "Please pick a number between 1 & 3.\n";
cin >> myChoice;
}
}

bool isPlayerWinner(int myChoice, int randomCompNum)
{
if (((myChoice == 1) && (randomCompNum == 3)) || ((myChoice == 3) && (randomCompNum == 2)) ||
((myChoice == 2) && (randomCompNum == 1)))
{
return true;
}

else if (((randomCompNum == 3) && (myChoice == 1)) || ((randomCompNum == 3) && (myChoice == 2)) || ((randomCompNum == 2) && (myChoice == 1)))
{
return false;
}
return 0;
}

bool isTie(int myChoice, int randomCompNum)
{
if (myChoice == randomCompNum)
{
return true;
}

else if (myChoice != randomCompNum)
{
return false;
}
return 0;
}

Sample Output:

Welcome to rock paper scissors
Please select a game mode
1. Player vs. Computer
2. Computer vs. Computer
3. Exit
5
Invalid selection. Try again.

Welcome to rock paper scissors
Please select a game mode
1. Player vs. Computer
2. Computer vs. Computer
3. Exit
1

Enter how many rounds to play? 3

Enter a even number of rounds. Try again!!
Enter how many rounds to play? 4


Rock, Paper, or Scissors?
1) Rock
2) Paper
3) Scissors
Please enter your choice :
1

You chose : Rock
The computer chose : Rock

It's a TIE!

Rock, Paper, or Scissors?
1) Rock
2) Paper
3) Scissors
Please enter your choice :
1

You chose : Rock
The computer chose : Paper

Sorry you LOSE.

Rock, Paper, or Scissors?
1) Rock
2) Paper
3) Scissors
Please enter your choice :
1

You chose : Rock
The computer chose : Paper

Sorry you LOSE.

Rock, Paper, or Scissors?
1) Rock
2) Paper
3) Scissors
Please enter your choice :
1

You chose : Rock
The computer chose : Paper

Sorry you LOSE.


********** Final Result (Player vs. Computer) **********
Computer WIN!
Computer WIN!
Computer WIN!

Welcome to rock paper scissors
Please select a game mode
1. Player vs. Computer
2. Computer vs. Computer
3. Exit
2

Enter how many rounds to play? 5

Enter a even number of rounds. Try again!!
Enter how many rounds to play? 6
player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!

player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!

player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!

player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!

player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!

player 1 chose : Paper

The computer chose : Scissors

Player 2 WIN!


********** Final Result (Computer vs. Computer) **********
Player 2 WIN!
Player 2 WIN!
Player 2 WIN!
Player 2 WIN!
Player 2 WIN!
Player 2 WIN!

Welcome to rock paper scissors
Please select a game mode
1. Player vs. Computer
2. Computer vs. Computer
3. Exit
3

Add a comment
Know the answer?
Add Answer to:
i have created a program for a game of rock, paper, scissors but i must make...
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
  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long...

    Create the second part of a Rock, Paper, Scissors game. The game keeps playing as long as the user enters 'Y'. Make this case sensitive; if they enter a lower case y the game will not continue. If the user enters in anything besides upper case Y the game will end. Your text must exactly match the examples below: Example 1 with correct input Let's play Rock, Paper, Scissors Enter 1 for rock, 2 for paper, 3 for scissors 2...

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • (C++) Hey guys we just went over loops and it got me confused, it has me...

    (C++) Hey guys we just went over loops and it got me confused, it has me scrathcing my head for the past two days. I would appreciate any help you guys have to offer. Few places I have a hard time is get input and determine winner(cummulative part). Write a program that lets the user play the Rock, Paper, Scissors game against the computer. The computer first chooses randomly between rock, paper and scissors, but does not display its choice....

  • Objective: Write a program that simulates a game of rock, paper, scissors between a human and...

    Objective: Write a program that simulates a game of rock, paper, scissors between a human and the computer in best 2 out of 3 rounds. Requirements: . The player can enter either "rock", "paper", or "scissors'". o If the player enters anything other than that the computer automatically gets a point . The computer randomly selects one of the gestures o Use the Random type to make this easier o Also make sure you import java.util.Random o You can use...

  • 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...

  • 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...

  • 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...

  • package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame {...

    package rpsgamesimulation; import java.util.Random; import java.util.Scanner; /** * * @author cristy */ public class RPSGame { private String userChoice, computerChoice;    public RPSGame() { userChoice = "rock"; computerChoice = "rock"; }    public String getUserChoice() { return userChoice; }    public String getComputerChoice() { return computerChoice; }    public void setUserChoice(String aUserChoice) { userChoice = aUserChoice; }    public void setComputerChoice(String aComputerChoice) { computerChoice = aComputerChoice; }    public String toString() { return "User Choice: " + userChoice + "...

  • 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
ADVERTISEMENT