forked from sunny7899/c-basic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rock -paper -scissor.c
90 lines (74 loc) · 2.75 KB
/
rock -paper -scissor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printRock() {
printf("┈┈┈┈┈┈┈┈┳╮\n");
printf("┈┈┈╭┻━━╮┃┃╮\n");
printf("┈┈┈┃╲┏━╯┻┫\n");
printf("┈┈┈╰╮╯┊┊┊╭╯\n");
}
void printPaper() {
printf("┏━━━━━━━━━━━━┓\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┃ ┃\n");
printf("┗━━━━━━━━━━━━┛\n");
}
void printScissors() {
printf("┈┈┈┈╭╮╭╮\n");
printf("┈┈┈┈┃┃┃┃\n");
printf("┈┈┈┈┃┃┃┃\n");
printf("┈┈┈┈┃┗┛┣┳╮\n");
printf("┈┈┈╭┻━━╮┃┃╮\n");
printf("┈┈┈┃╲┏━╯┻┫\n");
printf("┈┈┈╰╮╯┊┊┊╭╯\n");
}
int main() {
int playerChoice, computerChoice;
const char *choices[] = {"Rock", "Paper", "Scissors"};
char playAgain;
// Seed the random number generator
srand(time(NULL));
do {
printf("Choose:\n");
printf("0: Rock\n");
printf("1: Paper\n");
printf("2: Scissors\n");
printf("Enter your choice (0-2): ");
scanf("%d", &playerChoice);
if (playerChoice < 0 || playerChoice > 2) {
printf("Invalid choice! Please enter 0, 1, or 2.\n");
continue;
}
// Generate computer's choice
computerChoice = rand() % 3;
// Display player's choice
printf("\nYou chose: %s\n", choices[playerChoice]);
if (playerChoice == 0) printRock();
else if (playerChoice == 1) printPaper();
else if (playerChoice == 2) printScissors();
// Display computer's choice
printf("\nComputer chose: %s\n", choices[computerChoice]);
if (computerChoice == 0) printRock();
else if (computerChoice == 1) printPaper();
else if (computerChoice == 2) printScissors();
// Determine the winner
if (playerChoice == computerChoice) {
printf("\nIt's a tie!\n");
} else if ((playerChoice == 0 && computerChoice == 2) ||
(playerChoice == 1 && computerChoice == 0) ||
(playerChoice == 2 && computerChoice == 1)) {
printf("\nYou win!\n");
} else {
printf("\nYou lose!\n");
}
// Ask if the player wants to play again
printf("\nDo you want to play again? (y for yes, q for quit): ");
scanf(" %c", &playAgain);
} while (playAgain == 'y' || playAgain == 'Y');
printf("Thanks for playing!\n");
return 0;
}