Question: Need this C code to be rearranged so that the seperate functions are after main and get called from main using the pointer values. As
Need this C code to be rearranged so that the seperate functions are after main and get called from main using the pointer values.
As of now the seperate functions are arranged before main.
#include
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; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
