Thread: Rock Paper Scissors keeps messing up

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    5

    Rock Paper Scissors keeps messing up

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    This is probably wrong:
    Code:
    if (play1 == 'r' || 'R')
    {
        choice = ROCK;
    }
    What it means is "if the value of play1 is equal to 'r', or if the value of 'R' is non-zero, then assign the value of ROCK to choice". Since the value of 'R' is non-zero, the above code is equivalent to:
    Code:
    if (1)
    {
        choice = ROCK;
    }
    which is equivalent to:
    Code:
    choice = ROCK;
    which means that you wasted all your effort writing the if statement. What you probably wanted is "if the value of play1 is equal to 'r', or if the value of play1 is equal to 'R', then assign the value of ROCK to choice", i.e.,
    Code:
    if (play1 == 'r' || play1 == 'R')
    {
        choice = ROCK;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    Thank you so much! I'm not running on much sleep, so I never would have seen that. Also explains the spelling errors everywhere when I tried to explain the program. It runs perfectly now, thanks!

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    Instead of comparing the input to both upper and lower case, convert the input to one of them.

    You could use a string to keep track of what beats what. It might be a slight overkill (or not) for rock-paper-scissors, but it works well when extended to the full rock-paper-scissors-lizard-spock.

    Assuming opponent is already set to R, S, or P (or L or K) :
    Code:
    // rock-paper-scissors
    char a[] = "RsSpPr";  // R beats s, S beats p, P beats r
    int c = toupper(getchar());
    char *p = strchr(a, c); // find uppercase match
    if (!p) ; // error
    
    if (opponent == c)
        ;  // tie
    else if (opponent == toupper(p[1]))
        ;  // you win
    else
        ;  // you lose
    
    
    
    // rock-paper-scissors-lizard-spock (K is spock)
    char a[] = "KsrSplPrkRlsLpk"
    int c = toupper(getchar());
    char *p = strchr(a, c);
    if (!p) ; // error
    
    if (opponent == c)
        ;  // tie
    else if (opponent == toupper(p[1]) || opponent == toupper(p[2]))
        ;  // you win
    else
        ;  // you lose

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,660
    > If anyone has some insight it would be greatly appreciated
    Well working on a problem (and asking questions) sooner than "this project is due tonight" would be a start. Consider this an effective lesson on how not to do time management.

    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    5
    I had started it the day before and emailed my professor about my problem, but unfortunately he doesn't respond very quickly. The next day I had been trying to work on it but I couldn't figure it out, hence why I came here.

    While time management has not always been my strong suit, in this case it was not an issue (We only have 2 days to do our projects since it's a sped up summer course anyways). I will keep your advice in mind though, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rock, Scissors, paper
    By myyke_myyke in forum C Programming
    Replies: 6
    Last Post: 08-25-2011, 01:26 PM
  2. paper rock scissors
    By Amyaayaa in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 10:59 AM
  3. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  4. Paper Scissors Rock v0.1
    By Kirdra in forum Game Programming
    Replies: 2
    Last Post: 09-14-2002, 11:32 AM

Tags for this Thread