Ste1e

Simple C RPS game

Sep 17th, 2025 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main() {
  6.     srand(time(NULL));                              /* initiating random number generator with seed time */
  7.     short choice_computer = 1 + rand() % (3);       /* generating a random number between 1 - 3 */
  8.     short choice_user;
  9.  
  10.     printf("1 = rock; 2 = paper; 3 = scissors\n");
  11.     printf("Choose wisely: ");
  12.  
  13.     scanf("%hi", &choice_user);
  14.    
  15.     printf("\nComputer chose option %hi\n", choice_computer);
  16.     printf("You chose option %hi\n", choice_user);
  17.  
  18.     if(choice_user < 1 || choice_user > 3){         /* checking if user input is valid */
  19.         printf("\nInvalid choice!\n");
  20.         return 1;
  21.     }
  22.    
  23.     if(choice_computer - choice_user == 0){         /* calculating the outcome */
  24.         printf("\nIt's a draw!\n");
  25.     }else if(choice_computer - choice_user == 1 || choice_computer - choice_user == -2){
  26.         printf("\nComputer wins!\n");
  27.     }else{
  28.         printf("\nYou win!\n");
  29.     }
  30.  
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment