-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rock_paper_scissors game
96 lines (78 loc) · 1.66 KB
/
Rock_paper_scissors game
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
91
92
93
94
95
96
/* *author: Jai Keshav Sharma
*description: this is the C code for ROCK PAPER & SCISSORS game in which user plays against the computer
*28/01/2024
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int game(char you, char computer)
{
if (you == computer)
{
return -1;
}
if (you == 'r' && computer == 'p')
{
return 0;
}
else if (you == 'p' && computer == 'r')
{
return 1;
}
if (you == 'r' && computer == 's')
{
return 1;
}
else if (you == 's' && computer == 'r')
{
return 0;
}
if (you == 'r' && computer == 's')
{
return 1;
}
if (you == 'p' && computer == 's')
{
return 0;
}
else if (you == 's' && computer == 'p')
{
return 1;
}
}
int main()
{
int n, result;
char you, computer;
srand(time(NULL));
n = rand() % 100;
if (n < 33)
{
computer = 'r';
}
else if (n < 66 && n > 33)
{
computer = 'p';
}
else
{
computer = 's';
}
printf("\n\n\t\t\t\tWelcome to the Rock, Paper and Scissors game\n\t\t\t\t");
printf("\n\n\t\t\tEnter 'r' for 'rock', 'p' for 'paper' and 's' for 'scissors\n\n\t\t\t\t");
scanf("%c", &you);
result = game(computer, you);
if (result == -1)
{
printf("\n\t\t\t\t\t\t\tGame DRAW!\n");
}
else if (result == 1)
{
printf("\n\t\t\t\t\t WOW!, You hav won the game.\n");
}
else{
printf("\n\t\t\t\t\tOh!, you have lost the game.\n");
}
printf("\n\t\t\t\t\tyou chose: %c and computer chose: %c \n",you, computer);
return 0;
}