Jun 29, 2011 at 2:24pm UTC
Hi,
I have to write a program to play Rock Paper Scissors against the CPU
I have gotten the user input and computer choice to work but thats where it stops at. For some reason it will not read the cout statement to show who has won.
Please help
Thanks, Ben
Here is what I have
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void ShowProgramHeader();
int main()
{
int PlayerChoice;
void ShowProgramHeader();
{
cout <<" Ben Abeln " << endl;
cout <<" June 29th 2011 " << endl;
cout <<" Computer Science 120 " << endl;
cout <<"----------------------" << endl;
}
void Intro();
{
cout <<" Welcome to Rock, Paper, Scissors, Lizard, Spock " << endl;
cout <<" " << endl;
cout <<" Rules of the game : " << endl;
cout <<" Scissors cuts Paper covers Rock crushes Lizard poisons Spock smashes" << endl;
cout <<" Scissors decapitates Lizard eats Paper disproves Spock vaporizes Rock crushes" << endl;
cout <<" Scissors " << endl;
cout <<" " << endl;
cout <<" Please choose your choice: 1 = Rock, 2 = Paper " << endl;
cout <<" 3 = Scissors, 4 = Lizard, 5 = Spock " << endl;
cin >> PlayerChoice;
}
if ( PlayerChoice == 1 )
{
cout <<" Player Chooses: Rock " << endl;
}
else if ( PlayerChoice == 2 )
{
cout <<" Player Chooses: Paper " << endl;
}
else if ( PlayerChoice == 3 )
{
cout <<" Player Chooses: Scissors " << endl;
}
else if ( PlayerChoice == 4 )
{
cout << " Player Chooses: Lizard " << endl;
}
else if ( PlayerChoice == 5 )
{
cout << " Player Chooses: Spock " << endl;
return PlayerChoice;
}
int CompChoice;
char CompChar;
srand(time(0));
CompChoice = rand() % 5 + 1;
if ( CompChoice == 1 )
{
CompChar = '1';
cout << " Computer Chooses: Rock " << endl;
}
else if ( CompChoice == 2 )
{
CompChar = '2';
cout << " Computer Chooses: Paper " << endl;
}
else if ( CompChoice == 3 )
{
CompChar = '3';
cout << " Computer Chooses: Scissors " << endl;
}
else if ( CompChoice == 4 )
{
CompChar = '4';
cout << " Computer Chooses: Lizard " << endl;
}
else if ( CompChoice == 5 )
{
CompChar = '5';
cout << " Computer Chooses: Spock " << endl;
return CompChar;
}
if ( ( PlayerChoice == 1 && CompChar == 1 ) ||
( PlayerChoice == 2 && CompChar == 2 ) ||
( PlayerChoice == 3 && CompChar == 3 ) ||
( PlayerChoice == 4 && CompChar == 4 ) ||
( PlayerChoice == 5 && CompChar == 5 ) )
{
cout << " It's a TIE! " << endl;
}
else if (( PlayerChoice == 3 && CompChar == 2 ) ||
( PlayerChoice == 2 && CompChar == 1 ) ||
( PlayerChoice == 1 && CompChar == 4 ) ||
( PlayerChoice == 4 && CompChar == 5 ) ||
( PlayerChoice == 5 && CompChar == 3 ) ||
( PlayerChoice == 3 && CompChar == 4 ) ||
( PlayerChoice == 4 && CompChar == 2 ) ||
( PlayerChoice == 2 && CompChar == 5 ) ||
( PlayerChoice == 5 && CompChar == 1 ) ||
( PlayerChoice == 1 && CompChar == 3 ))
{
cout << " Congratulations, You have WON! " << endl;
}
else if (( PlayerChoice == 2 && CompChar == 3 ) ||
( PlayerChoice == 1 && CompChar == 2 ) ||
( PlayerChoice == 4 && CompChar == 1 ) ||
( PlayerChoice == 5 && CompChar == 4 ) ||
( PlayerChoice == 3 && CompChar == 5 ) ||
( PlayerChoice == 4 && CompChar == 3 ) ||
( PlayerChoice == 2 && CompChar == 4 ) ||
( PlayerChoice == 5 && CompChar == 2 ) ||
( PlayerChoice == 1 && CompChar == 5 ) ||
( PlayerChoice == 3 && CompChar == 1 ))
{
cout << " Too Bad, You have LOST! " << endl;
}
return 0;
}
Jun 29, 2011 at 2:50pm UTC
Where to start...CompChar == ...
why are you using a char for the computer and an int for the user? You need single quotes for the comparison: CompChar == '1'
or use CompChoice.
Last edited on Jun 29, 2011 at 2:50pm UTC
Jun 29, 2011 at 3:28pm UTC
Thanks for the help, I think I tried to do it one way and then realized there was a better way to do it and eventually it became a mess of both tries.