Advertisement
XOptimistixX

Rock, Paper, Scissors

Aug 21st, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <stdbool.h>
  6.  
  7. int rockPaperScissors();
  8. char playerInput[20], computerOutput[20];
  9. char *inputPossibilities[6]={"", "rock", "paper", "scissors", "q", "Q"};
  10.  
  11. int main()
  12. {
  13.     int playerInputInInt, computerOutputInInt;
  14.     bool game=1;
  15.     printf("Welcome to Rock, Paper, Scissors\n");
  16.     while (game==1)
  17.     {
  18.         printf("Choose \"rock\", \"paper\" or \"scissors\" or \"q\" to exit\n");
  19.         scanf("%s", playerInput);
  20.         if (!strcmp(playerInput, "rock")&&!strcmp(playerInput, "paper")&&
  21.             !strcmp(playerInput, "scissors")&&!strcmp(playerInput, "q")&&
  22.             !strcmp(playerInput, "Q")==false)
  23.         {
  24.             printf("Please input again\n");
  25.         }
  26.         else if (!strcmp(playerInput, inputPossibilities[4])==true)//exit
  27.         {
  28.             game=0;
  29.             break;
  30.         }
  31.         computerOutputInInt=rockPaperScissors();
  32.         if (!strcmp(playerInput, "rock")==true)
  33.             playerInputInInt=1;
  34.         if (!strcmp(playerInput, "paper")==true)
  35.             playerInputInInt=2;
  36.         if (!strcmp(playerInput, "scissors")==true)
  37.             playerInputInInt=3;
  38.         if (playerInputInInt==computerOutputInInt)//tie
  39.         {
  40.             printf("The game was a tie because the computer chose %s\n", computerOutput);
  41.         }
  42.         else if (playerInputInInt==computerOutputInInt+1||computerOutputInInt-2)//win
  43.         {
  44.             printf("You won because the computer chose %s!\n", computerOutput);
  45.         }
  46.         else if (computerOutputInInt==playerInputInInt+1||playerInputInInt-2)//lose
  47.         {
  48.             printf("You lost because the computer chose %s!\n", computerOutput);
  49.         }
  50.  
  51.     }
  52.     return 0;
  53. }
  54.  
  55. int rockPaperScissors()
  56. {
  57.     srand(time(NULL));
  58.     int output=(rand()%3+1);
  59.     strcpy(computerOutput, inputPossibilities[output]);
  60.     return output;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement