Advertisement
valve2

rock paper sizzors using switch and if

Mar 20th, 2023
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | Software | 0 0
  1. // rock paper sizzors using switch and if
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6.  
  7. int main() {
  8.    
  9.     puts("Enter\n1 for rock\n2 for paper\n3 for sizzors\n");
  10.     int userChoice;
  11.     scanf_s("%d", &userChoice);
  12.     // r=1 p=2 s=3
  13.     srand(time(NULL));
  14.     int computerChoice = (1 + rand() %  (3 - 1 + 1));
  15.     switch (userChoice) {
  16.     case 1:
  17.         printf("user: rock\n");
  18.         break;
  19.     case 2:
  20.         printf("user: paper\n");
  21.         break;
  22.     case 3:
  23.         printf("user: sizzors\n");
  24.         break;
  25.     default:
  26.         printf("Invalid input\nRestarting the game\n");
  27.         main();
  28.        
  29.         return 0;
  30.     }
  31.     switch (computerChoice) {
  32.     case 1:
  33.         puts("computer: rock");
  34.         break;
  35.     case 2:
  36.         puts("computer: paper");
  37.         break;
  38.     case 3:
  39.         puts("computer: sizzors");
  40.         break;
  41.     }
  42.  
  43.     if (userChoice == computerChoice)
  44.         printf("Tie!!");
  45.     else if (userChoice > computerChoice)
  46.         printf("You won!!");
  47.     else if (userChoice < computerChoice)
  48.         printf("Computer won!!");
  49.  
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement