Hello all,
This is my first time posting here but this project is due tonight and I've been stuck on this problem for the past 2 hours. I am new to C.
The assignment is based on one from 2 weeks ago, and basically we just have to simplify the code more instead of using if else's constantly we need to use a switch statement and typedef enum. It is okay to use the if elses in the cases to finish the little game.
When I run the program it takes 2 inputs for player 1 and player 2. I then run playGame which turns player 1's input into a choice. it uses that choice to run a switch statement and complete's it with the if elses inside, but unfortunately whenever I run it I keep getting "It's a draw!" regardless of what I input. I don't understand why it keeps only going to "It's a draw!" If anyone has some insight it would be greatly appreciated
Code:
#include <stdio.h>
typedef enum { ROCK = 1, PAPER, SCISSORS } Choice;
Choice choice;
char play1;
char play2;
void playGame(char play1, char play2);
int main(void)
{
printf("Player 1 please choose <r>ock, <p>aper, or <s>cissors:");
scanf("%c", &play1);
getchar();
printf("Player 2 please choose <r>ock, <p>aper, or <s>cissors:");
scanf("%c", &play2);
playGame(play1, play2);
return 0;
}
void playGame(char play1, char play2)
{
if (play1 == 'r' || 'R')
{
choice = ROCK;
}
else if (play1 == 'p' || 'P')
{
choice = PAPER;
}
else if (play1 == 's' || 'S')
{
choice = SCISSORS;
}
switch (choice)
{
case ROCK:
if (play2 == 'r' || 'R')
{
printf("It's a draw!\n");
}
else if (play2 == 'p' || 'P')
{
printf("Player 2 wins! Paper covers Rock!\n");
}
else if (play2 == 's' || 'S')
{
printf("Player 1 wins! Rock breaks Scissors!\n");
}
break;
case PAPER:
if (play2 == 'r' || 'R')
{
printf("Player 1 wins! Paper covers Rock!\n");
}
else if (play2 == 'p' || 'P')
{
printf("It's a draw!\n");
}
else if (play2 == 's' || 'S')
{
printf("Player 2 wins! Scissors cut Paper!\n");
}
break;
case SCISSORS:
if (play2 == 'r' || 'R')
{
printf("Player 2 wins! Rock breaks Scissors!\n");
}
else if (play2 == 'p' || 'P')
{
printf("Player 1 wins! Scissors cut Paper!\n");
}
else if (play2 == 's' || 'S')
{
printf("It's a draw!\n");
}
break;
default:
{
break;
}
}
}