HI All,

I am having a little difficulties here.

Here is my requirements:

Play the game of paper-rock-scissors. In main ask the user how many hands he/she wants to play. This must be an odd number, so you must validate this input to make sure the number is odd!

After each play, write a message stating who won the play like this:

Human won the play: paper beats rock

Computer won the play: scissors beats paper
-----------------------------------------------------

After each hand ( a hand is when one player wins the majority of the odd number.) For example if a round is 5 plays, the hand ends when either the computer or the human wins 3 plays. Print a message stating whether the computer won the hand or the human won the hand and print the current record like this:

Human: 3 wins Computer: 2 wins

``````#include <cstdlib>
#include <string>
#include <iostream>
#include <ctime>

using namespace std;

char readAndValidateUserMove() //Function to read in 1,2, & 3. which stands for rock, paper, & scissor.
{
char result; //Declare variable for function
do {
cin >> result; // Get input from user
if (result == '1' || result == '2' || result == '3') //If user chooses 1, 2, or 3 it will show.
} while (true); // If user gets the correct input, this will stay with the return statement.
}

char generateCompMove() //Declare variable for generating random number
{
return 48 + rand() % 3 + 1; //Generate number 1-3
}

int CompareMoves(char humanMove, char computersMove) //These variables will compare both moves from human and computer.
{

if (humanMove == computersMove) //This will determine both player's move.
return '2';
else if (humanMove == '1')
{
if (computersMove == '2')
return '1';
else
return 0;
}
else if (humanMove == '2')
{
if (computersMove == '1')
return 0;
else
return '1';
}
else if (humanMove == '3')
{
if (computersMove == '1')
return '1';
else
return 0;
}
return 0;
}

//Main function
int main(int argc, char** argv) //Argument count and argument values. This variable contains the number of arguments
// passes into the application. Will allow user to use the command line parameters passed to this application.
//This give the number and value of the user command lines arugments.
{
srand((unsigned) time(0)); //If you don't have this, the rand will not return the same results. This will make the rand return the same results.

//Declare variables
char human, computer, choice;
int result;

do //Do while loop so this will be repeated if the user wants to continue playing.
{

//Output friendly welcome message
cout << "\n******************************************************************" <<endl; //Stars just to seperate whole output if the user wish to play more.
cout << "\nWelcome to Carol's arcade center of C++! " << endl;
cout << "Are you ready to get this game started?!" << endl << endl;

//This is the output for the rules
cout << "There are three simple well-known rules: ";
cout << "\n\t\t\t\t\tPaper beats Rock";
cout << "\n\t\t\t\t\tRock beats Scissors";
cout << "\n\t\t\t\t\tScissors beats Paper" <<endl;

//Keycodes for user to enter number for the type of item.
cout << "\nHere are some keycodes before playing: ";
cout << "\n\t\t --------------";
cout << "\n\t\t|'1' = Rock    |";
cout << "\n\t\t|'2' = Paper   |";
cout << "\n\t\t|'3' = Scissor |";
cout << "\n\t\t --------------"<<endl;

int numberOfGames; //Declare another variable to see how many times user wants to play.

cout << "\nPlease enter how many times you would like to play: ";
cin >> numberOfGames;

for ( int i = 0; i < numberOfGames; i++) //This will determine how many times the user enters to play, this will loop that number the user enters.
{

cout << "--------------------------------------------------------------- ";
cout << "\n[Please enter one of the following numbers for your pick]: ";

human = readAndValidateUserMove(); //This function will get user to input between 1-3 and stores to the human.

cout << endl << "Computer = ";
computer = generateCompMove(); //This will generate the computer's pick.

if (computer == '1') //If computer chooses rock
cout << "Rock.";
else if (computer == '2') //If computer chooses paper
cout << "Paper.";
else
cout << "Scissors."; //OR computer chooses scissors

cout << endl << endl; //new space lines

//This will determine the overall game win scores for both players.
int totalComputersWins = 0;
int totalHumanWins = 0;
int totalTies = 0;

result = CompareMoves(human, computer); //Compare each move by the human and computer.

if (result == 0) //let the result begin at 0
{
cout << "You are a winner! " <<endl; //If human wins, this will be the message.
totalHumanWins++; //Adds 1 to the human's total of wins.
}
else if (result == '1')
{
cout << "You are a loser. " << endl; //If human loses, this will be the message.
totalComputersWins++; //Since the human lost, the computer will get the score.
}
else
{
cout << "Its a Tie. " <<endl; //If no one wins, this will be the output message.
totalTies++;
}
}

cout << endl; //new spaceline.

//Output for overall game scores.
cout << "\nGame Scores: ";
cout << "\n-----------";
cout << "\n\tHuman: " ;//<< totalPlayerWins; //Human's win score
cout << "\n\tComputer: " ;//<< totalCompWins << endl;//Computer's win score

//Asking if the user wants to play again after the rounds the user finished.
cout << "\nPlay Again? --- (Y/N): "; //Selecting Y or N to continue or quit.
cin >> choice;
} while(tolower(choice) == 'y'); //To covert uppercase to lowercase.
//If the user picks Y, the do while loop will contine again.

if(choice == 'n')
{

cout << "\n{Thank you for playing, hope you enjoy!" << endl << "\tGoodbye!}"<<endl<<endl;
}

return 0;
}``````

That is my code. I can not get the last part where they ask how many hands have won. I believe I would have to use a "while" loop but I don't know where to put it at. Please help!

## All 15 Replies

Your compMove does not return 1, 2, or 3. But instead 49, 50, or 51

Would I have to change:

``return 48 + rand() % 3 + 1; //Generate number 1-3``

to

``return rand()%3+1;``

?

First of all, you're not using your characters and integers consistently at all. It's going to cause you a lot of confusion in the future. The character `'1'` is not equal to the number `1` In `readAndValidateUserMove()` , there is really no need to use `tolower()` on the input because there is no such thing as a capital number. The next thing you should do is make it so that function converts the character the human typed into an actual integer. To do this, rewrite your return value as `return result - '0';` After that, get rid of all those silly quotes around all of your numbers, and get rid of that 48 in `generateCompMove()` . The way you're handling those numbers is almost guaranteed to cause problems down the road.

Now, onto what you're asking: You want to log the number of times each player has won, correct? You already have all the information you need, it's being logged in `totalHumanWins` and whatnot. All you need to do is print that information at the end of the program.

EDIT: Yes, that new code you just wrote would be correct, but only if you also correct that other code.

``````//Output for overall game scores.
cout << "\nGame Scores: ";
cout << "\n-----------";
cout << "\n\tHuman: ";
while(totalHumanWins < (numberOfGames/2) + 1 && totalComputersWins < (numberOfGames/2) + 1)
{
totalHumanWins++;
}
cout << totalHumanWins;

cout << "\n\tComputer: " ;//<< totalCompWins << endl;//Computer's win score
while(totalComputersWins < (numberOfGames/2) + 1 && totalComputersWins < (numberOfGames/2) + 1)
{
totalComputersWins++;
}
cout << totalComputersWins;``````

that's what I have so far. The silly thing is, it always show same number for Computer and human.

The reason why I have numberOfGames/2 is because I need to write who ever win the most games (so meaning, the user has to enter an ODD number so it can determine who wins the most games) is gonna win the hand.

I discovered another problem in that code: `totalComputersWins` and `totalHumanWins` are set to zero every time the loop loops. You need to put that at the very beginning of your program or nobody will ever win.

Also, why not just directly compare the two values directly to see who wins?

I discovered another problem in that code: `totalComputersWins` and `totalHumanWins` are set to zero every time the loop loops. You need to put that at the very beginning of your program or nobody will ever win.

Also, why not just directly compare the two values directly to see who wins?

Oh okay, got you! thanks so much.

How would I directly compare the two values?

I moved it to the very top, but it still happen.

Actually, my bad, it should be going inside the do-while loop but outside of the for loop.

And here's how to compare the two values.

``````if(totalComputerWins > totalHumanWins)
{
cout << "The computer has more wins." << endl;
}
else if(totalComputerWins < totalHumanWins)
{
cout << "The human has more wins." << endl;
}
else
{
cout << "Both have the same amount of wins." << endl;
}``````

Also, are you using totalHumanWins for each round that is won or each set of rounds? It looks like you're trying to use the same variable for two different things.

I'm trying to see who wins the most.

For example, if the user enters 7 (they have to enter an odd number) so if the first one that wins 4 out of 3, they are the winner, meaning they have one point.

You get it?

Thanks !

The code I just showed you would compare them. You can't guarantee that there won't be any ties, even with an odd number, though, because of the fact that there can be a tie of the computer and user throw the same hand.

You would need two more variables, `ComputerPoints` and `UserPoints` , and increment them whenever one wins more hands.

Thank you!!! so much. You are a lifesaver. Do you know how to delete this thread?

Do you know how to delete this thread?

Why?

Because I got the help I needed and don't want this thread to be a useless one out there.