Question

In: Computer Science

i am looking to add more detail into my program the runs a game of rock...

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;
}

Solutions

Expert Solution

#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;
}


Related Solutions

If I am looking to maximize my investment as an investor and the company I am...
If I am looking to maximize my investment as an investor and the company I am looking at has a low or unstable payout schedule, what would be the point of investing?
What is the "control" in a marketing plan? I am needing to add it into my...
What is the "control" in a marketing plan? I am needing to add it into my plan and I am lost at what this is.
I am convinced that my friend is cheating in our board game. I think that their...
I am convinced that my friend is cheating in our board game. I think that their die is not a normal six sided die because it rolls six far too often. Let ? be the proportion of rolls that are sixes. I plan on recording their result on their next 80 turns. (I will keep track of the number of sixes.) Draw the sampling distribution.
I am playing a game of Dungeon and Dragons with my family. My son’s character must...
I am playing a game of Dungeon and Dragons with my family. My son’s character must roll a critical hit, (19 or 20) on a 20 sided die. Without a critical hit, he cannot do enough damage to the troll. The troll will regenerate and kill me on the next turn. In addition to getting a high roll for hit chance, my son’s character must also get a high roll for damage. His weapon is a great sword and does...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I am looking to verify my answers by placing the proper items in the balance sheet...
I am looking to verify my answers by placing the proper items in the balance sheet Client Financial Information Assets & Liabilities Annual Income & Expenses Joint Money Market Account $     5,500 Jane's Gross - Part Time Wages $   15,000 Boat $   25,000 Gifts to Family $     2,500 Credit Card B (For Clothes) $     4,000 Homeowner's Insurance $     1,250 Mortgage Balance $   80,000 Auto insurance/registration $     1,100 Cash on Hand $     1,500 Income Taxes $   25,000 House $ 120,000 John's...
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
This is a java program I am trying to figure out how to add a couple...
This is a java program I am trying to figure out how to add a couple methods to a program I am working on. I need to be able to: 1. Remove elements from a binary tree 2. Print them in breadth first order Any help would appreciated. //start BinaryTree class /** * * @author Reilly * @param <E> */ public class BinaryTree { TreeNode root = null; TreeNode current, parent; private int size = 0, counter = 0; public...
Java Counter Program I can't get my program to add the number of times a number...
Java Counter Program I can't get my program to add the number of times a number was landed on for my if statements for No12 through No2. My Code: import java.util.Scanner; import java.util.Random;    import java.lang.*;       public class Dice    {               public static void main(String[] args)        {            Scanner in = new Scanner(System.in);            int Continue = 1;            //randomnum = new Random();           ...
Hi, (C programming) I am looking to write a program that will encrypt a text file...
Hi, (C programming) I am looking to write a program that will encrypt a text file automatically once program has opened, and have the option to decrypt it in a menu in my program. I have multiple text files that I need to encrypt, and would like if the program could encrypt all of them at once. I would also only like to decrypt the a text file once the name has been entered into a scanf function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT