Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main() {
- srand(time(NULL)); /* initiating random number generator with seed time */
- short choice_computer = 1 + rand() % (3); /* generating a random number between 1 - 3 */
- short choice_user;
- printf("1 = rock; 2 = paper; 3 = scissors\n");
- printf("Choose wisely: ");
- scanf("%hi", &choice_user);
- printf("\nComputer chose option %hi\n", choice_computer);
- printf("You chose option %hi\n", choice_user);
- if(choice_user < 1 || choice_user > 3){ /* checking if user input is valid */
- printf("\nInvalid choice!\n");
- return 1;
- }
- if(choice_computer - choice_user == 0){ /* calculating the outcome */
- printf("\nIt's a draw!\n");
- }else if(choice_computer - choice_user == 1 || choice_computer - choice_user == -2){
- printf("\nComputer wins!\n");
- }else{
- printf("\nYou win!\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment