Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- enum RPSChoice
- {
- ROCK = 1,
- PAPER = 2,
- SCISSORS = 3
- };
- // Convert an user input character to a choice ID
- int characterToChoice(char c)
- {
- c = tolower(c);
- if (c == 'r')
- return ROCK;
- else if (c == 's')
- return SCISSORS;
- else if (c == 'p')
- return PAPER;
- else
- return 0; // Not a proper choice!
- }
- // Decide who is the winner
- int winner(int user1, int user2) {
- if (user1 == user2) {
- return 0; // tie
- }
- else if ((user1 == ROCK && user2 == PAPER) ||
- (user1 == PAPER && user2 == SCISSORS) ||
- (user1 == SCISSORS && user2 == ROCK)) {
- return 2; // user2 is the winner
- }
- else {
- return 1; // user1 is the winner
- }
- }
- // Print someone's decision at the game
- void printDecision(string who, int choice) {
- cout << who; // no matter what, we will tell who took a decision
- if (choice == ROCK)
- cout << " chose ROCK!" << endl;
- else if (choice == PAPER)
- cout << " chose PAPER!" << endl;
- else if (choice == SCISSORS)
- cout << " chose SCISSORS!" << endl;
- }
- void playRPS (char x) {
- int user1 = characterToChoice(x);
- int user2 = rand() % 3 + 1;
- printDecision("You", user1);
- printDecision("The computer", user2);
- int result = winner(user1, user2);
- if (result == 0)
- cout << "It's a TIE!" << endl;
- else if (result == 1)
- cout << "You WON!" << endl;
- else
- cout << "You LOSE!" << endl;
- }
- int main() {
- char letter;
- cout << "Welcome to COP3014 ROCK PAPER SCISSORS!\n\n";
- cout << "Please select: " << endl
- << "Rock(r), Paper(p), or Scissors(s)? " << endl
- << "Or enter q to quit --> ";
- bool correctInput = false;
- do {
- cin >> letter;
- if (characterToChoice(letter) != 0) {
- playRPS(letter);
- correctInput = true;
- }
- else {
- cout << "Please enter r, p, or s" << endl;
- }
- } while (!correctInput);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement