hey guys
its me again with the rock paper scissors game.
well after spending the whole day on it (ok not really) i was able to write a proper code for the game that would ask user for their input and randomly select one for the computer. now it should print out what each user chose and say who wins the round (ie you chose rock. computer chose paper. you lose. paper wraps rock) and keep a count of who is winning/ties.
its all in function format and i had to declare constants for the rock/paper/scissors/quit option as well as constants to signal who won/tie
here is my complete code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//*********************
//Function Prototypes *
//*********************
int UserChoice();
int ComputerChoice();
int DetermineAndDisplayWinner(int User, int Computer);
void PrintObject(int obj);
void Pause();
const int Rock = 1;
const int Paper = 2;
const int Scissors = 3;
const int Quit = 4;
const int CompWon = 5;
const int UserWon = 6;
const int Tie = 7;
int main()
{
int User;
int Computer;
int obj;
int choice;
char answer = 'y';
while (answer == 'y' || answer == 'Y')
{
choice = UserChoice();
ComputerChoice();
DetermineAndDisplayWinner(User,Computer);
PrintObject(choice);
cout << "Do you want to continue?";
cin >> answer;
}
return 0;
}
//*************************
//Functions *
//*************************
//*******************
//Menu&User Choice *
//*******************
int UserChoice()
{
int choice;
cout << "Select one of the following options" << endl;
cout << "1. Rock" << endl;
cout << "2. Paper" << endl;
cout << "3. Scissors" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
while(choice < 1 || choice > 4)
{
cout << endl;
cout << "Please enter 1,2,3, or 4.";
cin >> choice;
}
cout << endl;
if (choice == 1)
cout << "You picked Rock\n";
else if (choice == 2)
cout << "You picked Paper\n";
else if (choice == 3)
cout << "You picked Scissors\n";
else
cout << "Goodbye!\n";
return choice;
}
//***************************
//Get Computer Choice *
//***************************
int ComputerChoice()
{
int randomNumber;
srand( (unsigned) time (NULL) );
randomNumber = rand() % 3 + 1;
if (randomNumber == 1)
cout << "The computer picked Rock\n";
else if (randomNumber == 2)
cout << "The computer picked Paper\n";
else
cout << "The computer picked Scissors\n";
return randomNumber;
}
//*********************************
//Determine And Display Winner *
//*********************************
int DetermineAndDisplayWinner(int User, int Computer)
{
if (User == Rock && Computer == Rock)
{
cout << "Its a Tie. Try again";
return Tie;
}
if (User == Rock && Computer == Paper)
{
cout << "You lose. Paper wraps Rock.";
return CompWon;
}
if (User == Rock && Computer == Scissors)
{
cout << "You win. Rock Smashes Scissors.";
return UserWon;
}
if (User == Paper && Computer == Scissors)
{
cout << "You lose. Scissors cuts Paper.";
return CompWon;
}
if (User == Paper && Computer == Paper)
{
cout << "It's a tie. Try again.";
return Tie;
}
if (User == Paper && Computer == Rock)
{
cout << "You win. Paper Wraps Rock.";
return CompWon;
}
if (User == Scissors && Computer == Scissors)
{
cout << "It's a tie. Try again.";
return Tie;
}
if (User == Scissors && Computer == Rock)
{
cout << "You lose. Rock smashes Scissors.";
return CompWon;
}
if (User == Scissors && Computer == Paper)
{
cout << "You win. Scissors cuts Paper.";
return UserWon;
}
return -1;
}
//*************************************
//Print Object *
//*************************************
void PrintObject(int obj)
{
switch(obj)
{
case Rock: cout << "Rock";
break;
case Paper: cout << "Paper";
break;
case Scissors: cout << "Scissors";
break;
case Quit: cout << "Quit";
break;
case CompWon: cout << "Computer Wins";
break;
case UserWon: cout << "You Win";
break;
case Tie: cout << "It's a tie";
break;
default: cout << "Invalid. try again";
}
}
//*****************
//Pause *
//*****************
void Pause()
{
cin.ignore(80, '\n');
}
all the function prototypes that u see, MUST BE USED in the program.
the only problem i have is that it wont tell me who wins.