So for a homework assignment I'm supposed to create a simple rock-paper-scissors game.
here is a list of requirements and hints from my teacher:
Requirements:
(1) Your program should provide a simple user interface to tell the user when to choose a shape.
(2) The user will use the keyboard to choose a shape.
(3) The user will use the key ‘R’ (or ‘r’) to choose rock, the key ‘P’ (or ‘p’) to choose paper, and the key ‘S’ (or ‘s’) to choose scissor.
(4) Your program should work no matter the user types an uppercase letter (‘R’, ‘P’, or ‘S’) or a lowercase letter (‘r’, ‘p’, or ‘s’).
(5) If the user types any key that is not one of the above keys, your program should show a message and terminate.
(6) Your program should let the computer randomly choose a shape.
(7) Your program should display what shape the human player chose as well as what shape the AI player chose.
(8) Your program must be able to judge who wins (or if it’s a draw) and display the result.
Hints:
(1) To make sure the computer generates a random number, you need to include these header files:
#include <cstdlib> ← for rand() and srand()
#include <ctime> ← for time()
You also need to add this statement before you begin to use rand() to generate the random number:
srand( static_cast<unsigned int> ( time(0) ) );
(2) Use % operator to constrain the range of the random numbers.
For example, rand() % 3.
Anyway we have to use if statements not switch statements. So my code compiles just fine, but I guess there's something wrong with my logical operators or my rand number? Anyway I've tried a bunch of different things and can't seem to figure it out.
currently my output looks like this:
Rock-Paper-Scissors Game!
Please choose your symbol:
R for rock; P for paper; S for scissor: S
The computer chose Rock.
The match is a draw!
The computer chose Rock.
Victory!
The computer chose Paper.
Victory!
Press any key to continue . . .
Also my else statement doesn't work at the end so like if someone enter 'x' it still comes back with some output like that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
//*******************************************************************************************************
int main()
{
char human;
int ai;
srand(static_cast<unsigned int> (time(0)));
ai = (rand() % 3) + 1;
cout << "Rock-Paper-Scissors Game!" << endl;
cout << "Please choose your symbol:" << endl;
cout << "R for rock; P for paper; S for scissor: ";
cin >> human;
if ((human == 'R' || human == 'r') && (ai == 1))
{
cout << "The computer chose Paper." << endl;
cout << "You lose better luck next time!" << endl;
}
else if ((human == 'R' || human == 'r') && (ai == 3))
{
cout << "The computer chose Rock." << endl;
cout << "The match is a draw!" << endl;
}
else if ((human == 'R' || human == 'r') && (ai == 2))
{
cout << "The computer chose Scissors." << endl;
cout << "Victory!" << endl;
}
else if ((human == 'P' || human == 'p') && (ai == 1))
{
cout << "The computer chose Paper." << endl;
cout << "The match is a draw!" << endl;
}
else if ((human == 'P' || human == 'p') && (ai == 3))
{
cout << "The computer chose Rock." << endl;
cout << "Victory!" << endl;
}
else if ((human == 'P' || human == 'p') && (ai == 2))
{
cout << "The computer chose Scissors." << endl;
cout << "You lose better luck next time!" << endl;
}
else if ((human == 'S' || human == 's') && (ai == 1))
{
cout << "The computer chose Paper." << endl;
cout << "Victory!" << endl;
}
else if ((human == 'S' || human == 's') && (ai == 3))
{
cout << "The computer chose Rock." << endl;
cout << "You lose better luck next time!" << endl;
}
else if ((human == 'S' || human == 's') && (ai == 2))
{
cout << "The computer chose Scissors." << endl;
cout << "The match is a draw!" << endl;
}
else
{
cout << "Invalid input, Game Over." << endl;
}
return 0;
}
//*******************************************************************************************************
|