Advertisement
Moortiii

Rock / Paper / Scissors [C]

Aug 30th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4.  
  5. int playerChoose()
  6. {
  7.     char player_string;
  8.     int player_choice = 0;
  9.  
  10.     printf("\nMake your choice using one of the letters: 'R' / 'P' / 'S':\n\n");
  11.     scanf(" %c", &player_string);
  12.     player_string = (char)toupper(player_string);
  13.  
  14.     if(player_string == 'R') {
  15.         printf("\nYou chose rock.\n");
  16.         player_choice = 1;
  17.     } else if(player_string == 'P') {
  18.         printf("\nYou chose paper.\n");
  19.         player_choice = 2;
  20.     } else if(player_string == 'S') {
  21.         printf("\nYou chose scissors.\n");
  22.         player_choice = 3;
  23.     } else {
  24.         playerChoose();
  25.     }
  26.  
  27.     return player_choice;
  28. }
  29.  
  30. int computerChoose()
  31. {
  32.     srand(time(NULL));
  33.     int computerChoice = 1 + rand() % 3; /* Return a random number between 1 and 3 */
  34.  
  35.     if(computerChoice == 1) {
  36.         printf("\nThe computer chose rock.\n");
  37.     } else if(computerChoice == 2) {
  38.         printf("\nThe computer chose paper.\n");
  39.     } else if(computerChoice == 3) {
  40.         printf("\nThe computer chose scissors.\n");
  41.     }
  42.  
  43.     return computerChoice;
  44. }
  45.  
  46. int compareChoices(int playerChoice, int computerChoice)
  47. {
  48.     if(playerChoice == 1) { /* User chooses rock */
  49.         if(computerChoice == 1) { /* Computer chooses rock */
  50.             printf("\nIt's a tie!\n");
  51.         } else if(computerChoice == 2) { /* Computer chooses paper */
  52.             printf("\nThe computer wins.\n");
  53.         } else if(computerChoice == 3) { /* Computer chooses scissors */
  54.             printf("\nYou win!\n");
  55.         }
  56.     } else if(playerChoice == 2) { /* Player chooses paper */
  57.         if(computerChoice == 1) { /* Computer chooses rock */
  58.             printf("\nYou win!\n");
  59.         } else if(computerChoice == 2) { /* Computer chooses paper */
  60.             printf("\nIt's a tie.\n");
  61.         } else if(computerChoice == 3) { /* Computer chooses scissors */
  62.             printf("\nThe computer wins.\n");
  63.         }
  64.     } else if(playerChoice == 3) { /* Player chooses scissors */
  65.         if(computerChoice == 1) { /* Computer chooses rock */
  66.             printf("\nThe computer wins.\n");
  67.         } else if(computerChoice == 2) { /* Computer chooses paper */
  68.             printf("\nYou win!\n");
  69.         } else if(computerChoice == 3) { /* Computer chooses scissors */
  70.             printf("\nIt's a tie.\n");
  71.         }
  72.     }
  73. }
  74.  
  75. void playAgain()
  76. {
  77.     char play_again;
  78.     printf("\nDo you want to play again? 'Y' / 'N'\n\n");
  79.     scanf(" %c", &play_again);
  80.     play_again = (char)toupper(play_again);
  81.  
  82.     if(play_again == 'Y') {
  83.         playGame();
  84.     } else if(play_again == 'N') {
  85.         printf("\nThank you for playing.\n");
  86.     } else {
  87.         playAgain();
  88.     }
  89. }
  90.  
  91. void playGame()
  92. {
  93.     int playerChoice = playerChoose();
  94.     int computerChoice = computerChoose();
  95.     compareChoices(playerChoice, computerChoice);
  96.     playAgain();
  97. }
  98.  
  99. int main()
  100. {
  101.     playGame();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement