File tree Expand file tree Collapse file tree 5 files changed +115
-0
lines changed Expand file tree Collapse file tree 5 files changed +115
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <time.h>
4
+
5
+ int main (){
6
+ int number , guess , nguesses = 1 ;
7
+ srand (time (0 ));
8
+ number = rand ()%100 + 1 ; // Generates a random number between 1 and 100
9
+ // printf("The number is %d\n", number);
10
+ // Keep running the loop until the number is guessed
11
+ do {
12
+ printf ("Guess the number between 1 to 100\n" );
13
+ scanf ("%d" , & guess );
14
+ if (guess > number ){
15
+ printf ("Lower number please!\n" );
16
+ }
17
+ else if (guess < number ){
18
+ printf ("Higher number please!\n" );
19
+ }
20
+ else {
21
+ printf ("You guessed it in %d attempts\n" , nguesses );
22
+ }
23
+ nguesses ++ ;
24
+ }while (guess != number );
25
+
26
+ return 0 ;
27
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <time.h>
4
+
5
+ int main (){
6
+ int number ;
7
+ srand (time (0 ));
8
+ number = rand ()%100 + 1 ; // Generates a random number between 1 and 100
9
+ printf ("The number is %d" , number );
10
+ return 0 ;
11
+ }
Original file line number Diff line number Diff line change
1
+ #include <stdio.h>
2
+ #include <stdlib.h>
3
+ #include <time.h>
4
+
5
+ int snakeWaterGun (char you , char comp ){
6
+ // returns 1 if you win, -1 if you lose and 0 if draw
7
+ // Condition for draw
8
+ // Cases covered:
9
+ // ss
10
+ // gg
11
+ // ww
12
+ if (you == comp ){
13
+ return 0 ;
14
+ }
15
+
16
+ // Non-draw conditions
17
+ // Cases covered:
18
+ // sg
19
+ // gs
20
+ // sw
21
+ // ws
22
+ // gw
23
+ // wg
24
+
25
+
26
+ if (you == 's' && comp == 'g' ){
27
+ return -1 ;
28
+ }
29
+ else if (you == 'g' && comp == 's' ){
30
+ return 1 ;
31
+ }
32
+
33
+ if (you == 's' && comp == 'w' ){
34
+ return 1 ;
35
+ }
36
+ else if (you == 'w' && comp == 's' ){
37
+ return -1 ;
38
+ }
39
+
40
+ if (you == 'g' && comp == 'w' ){
41
+ return -1 ;
42
+ }
43
+ else if (you == 'w' && comp == 'g' ){
44
+ return 1 ;
45
+ }
46
+
47
+ }
48
+ int main (){
49
+ char you , comp ;
50
+ srand (time (0 ));
51
+ int number = rand ()%100 + 1 ;
52
+
53
+ if (number < 33 ){
54
+ comp = 's' ;
55
+ }
56
+ else if (number > 33 && number < 66 ){
57
+ comp = 'w' ;
58
+ }
59
+ else {
60
+ comp = 'g' ;
61
+ }
62
+
63
+ printf ("Enter 's' for snake, 'w' for water and 'g' for gun\n" );
64
+ scanf ("%c" , & you );
65
+ int result = snakeWaterGun (you , comp );
66
+ if (result == 0 ){
67
+ printf ("Game draw!\n" );
68
+ }
69
+ else if (result == 1 ){
70
+ printf ("You win!\n" );
71
+ }
72
+ else {
73
+ printf ("You Lose!\n" );
74
+ }
75
+ printf ("You chose %c and computer chose %c. " , you , comp );
76
+ return 0 ;
77
+ }
You can’t perform that action at this time.
0 commit comments