Skip to content

Commit d052002

Browse files
committed
Add 2 Games
Added the following games: -Guess the number. -Snake Water Gun
1 parent 7fa6476 commit d052002

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

games_using_c.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//********************Guess The Number**************************//
2+
#include<stdio.h>
3+
#include<stdlib.h>
4+
#include<time.h>
5+
6+
int main(){
7+
int number, guess, nguesses=1;
8+
srand(time(0));
9+
number = rand()%100 + 1; // Generates a random number between 1 and 100
10+
// printf("The number is %d\n", number);
11+
// Keep running the loop until the number is guessed
12+
do{
13+
printf("Guess the number between 1 to 100\n");
14+
scanf("%d", &guess);
15+
if(guess>number){
16+
printf("Lower number please!\n");
17+
}
18+
else if(guess<number){
19+
printf("Higher number please!\n");
20+
}
21+
else{
22+
printf("You guessed it in %d attempts\n", nguesses);
23+
}
24+
nguesses++;
25+
}while(guess!=number);
26+
27+
return 0;
28+
}
29+
30+
//*********************Rock Paper Scissors*********************//
31+
#include<stdio.h>
32+
#include<stdlib.h>
33+
#include<time.h>
34+
35+
int snakeWaterGun(char you, char comp){
36+
// returns 1 if you win, -1 if you lose and 0 if draw
37+
// Condition for draw
38+
// Cases covered:
39+
// ss
40+
// gg
41+
// ww
42+
if(you == comp){
43+
return 0;
44+
}
45+
46+
// Non-draw conditions
47+
// Cases covered:
48+
// sg
49+
// gs
50+
// sw
51+
// ws
52+
// gw
53+
// wg
54+
55+
56+
if(you=='s' && comp=='g'){
57+
return -1;
58+
}
59+
else if(you=='g' && comp=='s'){
60+
return 1;
61+
}
62+
63+
if(you=='s' && comp=='w'){
64+
return 1;
65+
}
66+
else if(you=='w' && comp=='s'){
67+
return -1;
68+
}
69+
70+
if(you=='g' && comp=='w'){
71+
return -1;
72+
}
73+
else if(you=='w' && comp=='g'){
74+
return 1;
75+
}
76+
77+
}
78+
int main(){
79+
char you, comp;
80+
srand(time(0));
81+
int number = rand()%100 + 1;
82+
83+
if(number<33){
84+
comp = 's';
85+
}
86+
else if(number>33 && number<66){
87+
comp='w';
88+
}
89+
else{
90+
comp='g';
91+
}
92+
93+
printf("Enter 's' for snake, 'w' for water and 'g' for gun\n");
94+
scanf("%c", &you);
95+
int result = snakeWaterGun(you, comp);
96+
if(result ==0){
97+
printf("Game draw!\n");
98+
}
99+
else if(result==1){
100+
printf("You win!\n");
101+
}
102+
else{
103+
printf("You Lose!\n");
104+
}
105+
printf("You chose %c and computer chose %c. ", you, comp);
106+
return 0;
107+
}

hw9final.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//use for and if statements 2 each, one for input and one for output.
1313
main() {
1414
int number[SIZE];// size is [50]
15-
int i;
15+
int i;// should be in []along with variable int.
1616

1717
for (i = 0; i < SIZE; i++) //remember to put value = 0 with the integer [i] in for loop statement.
1818
{

0 commit comments

Comments
 (0)