Thread: Rock, Paper, Scissors

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Rock, Paper, Scissors

    So I was just think of how to do it and gave it a go. Heres what I got, It works fine, I just want to know if there would be a better way of doing it, a more efficient way so there isn't as many if statements:

    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <time.h>
    #include <stdlib.h>
    
    main()
    {
          
          int iSelection = 0;
          int iRandom = 0;
          srand(time(0));
          
          iRandom = (rand() % 3) + 1;
          
          printf("\tRock, Paper, Scissors!\n");
          printf("\n1\tRock\n");
          printf("2\tPaper\n");
          printf("3\tScissors\n");
          printf("\nPlease enter a selection: ");
          scanf("%d", &iSelection);
          
          if (iSelection < 1 || iSelection > 3) {
             printf("\nPlease enter a valid selection!\n");
          }
          
          if (iSelection == 1 && iRandom == 1) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nIt's a tie!\n");
          }
          
          if (iSelection == 1 && iRandom == 2) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Lose!\n");
          }
          
          if (iSelection == 1 && iRandom == 3) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Win!\n");
          }
          
          if (iSelection == 2 && iRandom == 1) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Win!\n");
          }
          
          if (iSelection == 2 && iRandom == 2) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nIt's a tie!\n");
          }
          
          if (iSelection == 2 && iRandom == 3) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Lose!\n");
          }
          
          if (iSelection == 3 && iRandom == 1) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Lose!\n");
          }
          
          if (iSelection == 3 && iRandom == 2) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Win!\n");
          }
          
          if (iSelection == 3 && iRandom == 3) {
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nIt's a tie!\n");
          }
          
          Sleep(5000);
          
    }

  2. #2
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    1. you should use
    Code:
    int main(int argc, char **argv)
    or at least int main(). Just main(), it will generate an error, smthg like: main function should return a value - when the program is build with some compilers.

    2. You can observe that this:
    Code:
             printf("\nYou selected %d\n", iSelection);
             printf("\nYou opponent selected %d\n", iRandom);
             printf("\nYou Lose!\n");
    is the same for each if statement. You can write a function that will have a switch(iRandom). && call that function in the main function every time you need it.

    In the main function use switch instead of if series. Using switch instead of if series you code will become easier to be read and understood.
    Last edited by ralu.; 02-13-2009 at 06:32 PM.
    I have stopped reading Stephen King novels. Now I just read C code instead.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Factoring out common code, reorganizing your if/then's, and using some ternary operators turns it into this:

    Code:
    static const char *CHOICES[] = {0, "ROCK", "PAPER", "SCISSORS"};
    static const char *WIN_MSG = "You win!";
    static const char *LOSE_MSG = "You lose!";
    static const char *TIE_MSG = "It's a tie!";
    ....
    
    printf("\nYou selected %s\n", CHOICES[iSelection]);
    printf("\nYour opponent selected %s\n", CHOICES[iRandom]);
    printf("\n");
    
    if (iSelection == iRandom)
       printf(TIE_MSG);
    else if (iSelection == 1)
       printf(iRandom == 3 ? WIN_MSG : LOSE_MSG);
    else if (iSelection == 2)
       printf(iRandom == 1 ? WIN_MSG : LOSE_MSG);
    else
       printf(iRandom == 2 ? WIN_MSG : LOSE_MSG);
    
    printf("\n");
    I also modified it to print the name of what you selected, rather than its number. I don't think users like reading, "My 1 beats your 3!!!".

    You could make it even shorter by cheating and cramming things all on the same line:

    Code:
    if (iSelection == iRandom)
       printf(TIE_MSG);
    else switch(iSelection) {
       case 1:  printf(iRandom == 3 ? WIN_MSG : LOSE_MSG); break;
       case 2:  printf(iRandom == 1 ? WIN_MSG : LOSE_MSG); break;
       case 3:  printf(iRandom == 2 ? WIN_MSG : LOSE_MSG); break;
    }
    You could try making it even shorter, but I don't know why you'd bother. I'd try making it more readable. You could replace the hard-coded values 1, 2, 3 with an enumeration perhaps.. {ROCK = 1, PAPER, SCISSORS}.

    Maybe put the input in a loop so that if the user enters an illegal value it asks for another one. (Hint: If you do this, make sure you suck up all the crap in the input buffer. scanf() will leave everything in the input buffer if a formatting error occurs. You could easily turn this into an infinite loop.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  2. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  3. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM
  4. Another rock paper scissor question
    By mattflick in forum C Programming
    Replies: 11
    Last Post: 09-29-2005, 09:41 PM
  5. PRS Help
    By SpudNuts in forum C Programming
    Replies: 10
    Last Post: 08-07-2005, 01:14 PM