Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // rock paper sizzors using switch and if
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main() {
- puts("Enter\n1 for rock\n2 for paper\n3 for sizzors\n");
- int userChoice;
- scanf_s("%d", &userChoice);
- // r=1 p=2 s=3
- srand(time(NULL));
- int computerChoice = (1 + rand() % (3 - 1 + 1));
- switch (userChoice) {
- case 1:
- printf("user: rock\n");
- break;
- case 2:
- printf("user: paper\n");
- break;
- case 3:
- printf("user: sizzors\n");
- break;
- default:
- printf("Invalid input\nRestarting the game\n");
- main();
- return 0;
- }
- switch (computerChoice) {
- case 1:
- puts("computer: rock");
- break;
- case 2:
- puts("computer: paper");
- break;
- case 3:
- puts("computer: sizzors");
- break;
- }
- if (userChoice == computerChoice)
- printf("Tie!!");
- else if (userChoice > computerChoice)
- printf("You won!!");
- else if (userChoice < computerChoice)
- printf("Computer won!!");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement