Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description: Write a program that implements a Rock/Paper/Scissors game that all

ID: 3673577 • Letter: D

Question

Description: Write a program that implements a Rock/Paper/Scissors game that allows the user to play the computer (also known as roshambo, possibly originating in 19th century Japan). The program should prompt the user to enter R, P, or S for Rock, Paper, or Scissors, then it should randomly generate the computer's choice and determine who won and display the result. The program should allow the user to continue playing until the user decides to stop, displaying the score (number of times that the computer won, the number of times that the player won and the number of ties.). In this game, Rock beats (breaks) Scissors, Scissors beats (cuts) Paper, and Paper beats (covers) Rock. The program should do error handling on the input values and require the Player to enter R, P, S or Q (for quit.) The program should accept both upper and lower case letters. The program output should look exactly like the examples below, including spacing and error handling.

Explanation / Answer

////////////////////////////// c++ code //////////////////////////////////////

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

enum Winner {
    CPU = 1,
    YOU,
    TIE
};

enum Weapon {
    ROCK = 1,
    PAPER,
    SCISSORS,
    BAD_WEAPON
};

Weapon get_your_weapon(char ch) {
    switch(ch) {
        case 'r':
        case 'R':
            return ROCK;
        case 'p':
        case 'P':
            return PAPER;
        case 's':
        case 'S':
            return SCISSORS;
        default:
            return BAD_WEAPON;
    }
}

Weapon get_cpu_weapon() {
    switch(rand() % 3) {
        case 0:
            return ROCK;
        case 1:
            return PAPER;
        case 2:
            return SCISSORS;
    }
}

string get_weapon_string(Weapon weapon) {
    switch(weapon) {
        case ROCK:
            return "Rock";
        case PAPER:
            return "Paper";
        case SCISSORS:
            return "Scissors";
        case BAD_WEAPON:
            return "Bad Weapon";
    }
}

Winner get_winner(Weapon you, Weapon cpu) {
    if (you == cpu) {
        return TIE;
    } else if (you == ROCK) {
        if (cpu == PAPER) {
            return CPU;
        } else {
            return YOU;
        }
    } else if (you == PAPER) {
        if (cpu == SCISSORS) {
            return CPU;
        } else {
            return YOU;
        }
    } else if (you == SCISSORS) {
        if (cpu == ROCK) {
            return CPU;
        } else {
            return YOU;
        }
    } else {
        return CPU;
    }
}

int print_winner(Winner winner) {
    if (winner == TIE) {
        cout << ", it is a tie. "; return 0;
    } else if (winner == YOU) {
        cout << ", you win! "; return 1;
    } else { // if (winner == CPU) {
        cout << ", the Computer wins. "; return 2;
    }
}

void print_rule(Weapon a, Weapon b) {
    if (a == ROCK) {
        cout << get_weapon_string(a) << " crushes " << get_weapon_string(b) << " ";
    } else if (a == SCISSORS) {
        cout << get_weapon_string(a) << " cuts " << get_weapon_string(b) << " ";
    } else {
        cout << get_weapon_string(a) << " wraps " << get_weapon_string(b) << " ";
    }
}

void print_rules(Winner winner, Weapon you, Weapon cpu) {
    if (winner == TIE) {
        cout << "we picked the same thing" << " ";
    } else if (winner == YOU) {
        print_rule(you, cpu);
    } else { // winner == CPU
        print_rule(cpu, you);
    }
}

int main() {

    cout << "Let's Play a Game of Rock/Paper/Scissors ";
    int tie =0 , you = 0, comp = 0;
    while (1) {
            srand(time(NULL));
        cout << " Enter the R, P, Q, or Q(for quit)) ";
        char weapon_choice;
        cin >> weapon_choice;
     
        if(weapon_choice == 'Q' || weapon_choice == 'q') {
        cout << "You won "<< you << " times, Computer won "<< comp << " times and it was a tie " << tie <<" times ";
        cout << "Thank You for Playing"; return 0; }

        while(weapon_choice != 'R' && weapon_choice != 'r' && weapon_choice != 'P' && weapon_choice != 'p' && weapon_choice != 'S' && weapon_choice != 's' )
         {   cout <<" ERROR: Invalid Choice. Try again. ";
            cout << " Enter the R, P, Q, or Q(for quit)) ";
            cin >> weapon_choice;
            if(weapon_choice == 'Q' || weapon_choice == 'q')
             { cout << "You won "<< you << " times, Computer won "<< comp << " times and it was a tie " << tie <<" times ";
            cout << "Thank You for Playing"; return 0; }
          }

        Weapon your_weapon = get_your_weapon(weapon_choice);
        Weapon cpu_weapon = get_cpu_weapon();
      
        Winner winner = get_winner(your_weapon, cpu_weapon);
      
        cout << "You Picked " + get_weapon_string(your_weapon);
        cout << ", Computer Picked " + get_weapon_string(cpu_weapon) << " ";
      
        print_rules(winner, your_weapon, cpu_weapon);
        int t = print_winner(winner);
        if(t==0) tie ++;
        else if(t==1) you++;
        else if(t==2) comp++;
      
     
    }
  
    return 0;
}