-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgames_using_c.c
107 lines (95 loc) · 2.16 KB
/
games_using_c.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//********************Guess The Number**************************//
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int number, guess, nguesses=1;
srand(time(0));
number = rand()%100 + 1; // Generates a random number between 1 and 100
// printf("The number is %d\n", number);
// Keep running the loop until the number is guessed
do{
printf("Guess the number between 1 to 100\n");
scanf("%d", &guess);
if(guess>number){
printf("Lower number please!\n");
}
else if(guess<number){
printf("Higher number please!\n");
}
else{
printf("You guessed it in %d attempts\n", nguesses);
}
nguesses++;
}while(guess!=number);
return 0;
}
//*********************Rock Paper Scissors*********************//
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int snakeWaterGun(char you, char comp){
// returns 1 if you win, -1 if you lose and 0 if draw
// Condition for draw
// Cases covered:
// ss
// gg
// ww
if(you == comp){
return 0;
}
// Non-draw conditions
// Cases covered:
// sg
// gs
// sw
// ws
// gw
// wg
if(you=='s' && comp=='g'){
return -1;
}
else if(you=='g' && comp=='s'){
return 1;
}
if(you=='s' && comp=='w'){
return 1;
}
else if(you=='w' && comp=='s'){
return -1;
}
if(you=='g' && comp=='w'){
return -1;
}
else if(you=='w' && comp=='g'){
return 1;
}
}
int main(){
char you, comp;
srand(time(0));
int number = rand()%100 + 1;
if(number<33){
comp = 's';
}
else if(number>33 && number<66){
comp='w';
}
else{
comp='g';
}
printf("Enter 's' for snake, 'w' for water and 'g' for gun\n");
scanf("%c", &you);
int result = snakeWaterGun(you, comp);
if(result ==0){
printf("Game draw!\n");
}
else if(result==1){
printf("You win!\n");
}
else{
printf("You Lose!\n");
}
printf("You chose %c and computer chose %c. ", you, comp);
return 0;
}