Need this C code to be rearranged so that the seperate functions are after main
ID: 3675637 • Letter: N
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 of now the seperate functions are arranged before main.
#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;
}
Explanation / Answer
The same program can be written as shown in the below code which is also easy to understand than your code.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
enum {
ROCK,
PAPER,
SCISSORS,
MAX
};
static const char key[MAX] = { 'r','p','s' };
static const char * const name[MAX] = { "rock", "paper", "scissors" };
static int beats(int x, int y)
{
if (x == y)
return -1;
if ((x == PAPER && y == ROCK) ||
(x == ROCK && y == SCISSORS) ||
(x == SCISSORS && y == PAPER))
return 1;
return 0;
}
static int choose(void)
{
int c, x, d = 0;
puts("(r)ock, (p)aper, (s)cissors, (q)uit");
do {
c = getchar();
switch (c)
{
case 'r': x = ROCK; d = 1; break;
case 'p': x = PAPER; d = 1; break;
case 's': x = SCISSORS; d = 1; break;
case 'q': case EOF: d = -1; break;
default: break;
}
} while (d == 0);
return (d == -1) ? d : x;
}
int main(void)
{
srand(time(NULL));
while (1)
{
int computer_choice = rand() % MAX;
int user_choice = choose();
if (user_choice == -1)
break;
printf("You choose: %s ", name[user_choice]);
printf("Computer chose: %s ", name[computer_choice]);
puts("");
if (user_choice == computer_choice)
puts("Draw!");
else if (beats(user_choice, computer_choice))
puts("You win!");
else
puts("Computer wins!");
puts("");
}
puts("Bye!");
return 0;
}
please find below your piece of code which has been re-arranged :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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++;
}
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);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.