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.
return tolower(result); //Make it easier, converting uppercase letter to lower case.
} 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!