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

Need help coding this in C. Can\'t use iostream. Must include stdio.h, stdlib.h,

ID: 3674903 • Letter: N

Question

Need help coding this in C. Can't use iostream. Must include stdio.h, stdlib.h, time.h. Thank you to anyone who can help

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 handing 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 <stdio.h>
#include <stdlib.h>
#include <time.h>

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

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

enum 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;
    }
}

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

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

enum Winner get_winner(enum Weapon you, enum 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(enum Winner winner) {
    if (winner == TIE) {
        printf(", it is a tie. " ); return 0;
    } else if (winner == YOU) {
        printf(", you win!! " ); return 1;
    } else { // if (winner == CPU) {
        printf(", the Computer wins. " );
        return 2;
    }
}

void print_rule(enum Weapon a, enum Weapon b) {
    if (a == ROCK) {
        printf("%s crushes %s ",get_weapon_string(a),get_weapon_string(b) );
    } else if (a == SCISSORS) {
        printf("%s cuts %s ",get_weapon_string(a),get_weapon_string(b) );
    } else {
        printf("%s wraps %s ",get_weapon_string(a),get_weapon_string(b) );
    }
}

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

int main() {

    printf("Let's Play a Game of Rock/Paper/Scissors ");
    int tie =0 , you = 0, comp = 0;
    while (1) {
            srand(time(NULL));
        printf(" Enter the R, P, Q, or Q(for quit)) ");
        char weapon_choice;
        scanf(" %c",&weapon_choice);
   
        if(weapon_choice == 'Q' || weapon_choice == 'q') {
        printf("You won %d times, Computer won %d times and it was a tie %d times ", you, comp , tie);
        printf("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' )
         {   printf(" ERROR: Invalid Choice. Try again. ");
             printf(" Enter the R, P, Q, or Q(for quit)) ");
                    scanf(" %c",&weapon_choice);
            if(weapon_choice == 'Q' || weapon_choice == 'q')
             { printf("You won %d times, Computer won %d times and it was a tie %d times ", you, comp , tie);
            printf("Thank You for Playing "); return 0; }
          }

        enum Weapon your_weapon = get_your_weapon(weapon_choice);
        enum Weapon cpu_weapon = get_cpu_weapon();
    
        enum Winner winner = get_winner(your_weapon, cpu_weapon);
    
        printf("You Picked %s", get_weapon_string(your_weapon) );
        printf(", Computer Picked %s ", 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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote