In: Computer Science
i am looking to add more detail into my program the runs a game of rock paper scissors
i want to include a function or way to ask the user to input their name so that it could be shown in the results at the end of a round/game.
after a round something like "Mike wins!"
i also want to output the round number when the result of for each round is outputed
Round 1: Mike Wins!
Round 2: Computer wins!
#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;
}
#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;
string PlayerName;
vector <string> winner;
do
{
winner.clear();
userChoice = menu();
if (userChoice == 1)
{
cout<<"Enter your Name? ";
cin>>PlayerName;
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("Round" +to_string(c+1) +": Computer WIN!");
}
else if (isPlayerWinner(playerChoice, computerChoice))
{
winner.push_back("Round" +to_string(c+1)+": "+PlayerName+"
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;
}