-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberGuessingGame.c
100 lines (71 loc) · 3.32 KB
/
NumberGuessingGame.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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
// TO STORE THE RANDOM NUMBER GENERATED AND THE NUMBER OF TURNS PLAYER TAKE TO GUESS CORRECT ANSWER...
int randomNum,numOfGuesses=0;
//TO STORE THE GUESSED NUMBER BY PLAYER...
int guessedNumber;
//TO STORE THE HIGHEST SCORE TILL NOW...
int highestScore=100000;
//TO STORE THE DIFFICULTY OF THE GAME...
int difficulty;
//TO STORE THE PLAYER INPUT IF THEY WANT TO PLAY THE GAME OR NOT....
char userInput[3];
printf("TYPE 'yes' IF YOU WANT TO PLAY THIS GAME , ELSE TYPE 'no' TO EXIT :-D \n\n");
scanf("%s",userInput);
if(userInput[0]=='y' || userInput[0]=='Y'){
//PLAYER 1 CAN DECIDE THE RANGE OF THE GAME...
printf("AS A PLAYER 1, YOU ARE GIVEN A PRIVILEGE TO CHOOSE THE DIFFICULTY OF THE GAME... \n");
printf("1. FOR VERY EASY ENTER '10'\n");
printf("2. FOR EASY ENTER '100'\n");
printf("3. FOR MEDIUM ENTER '1000'\n");
printf("4. FOR HARD ENTER '10000'\n");
scanf("%d",&difficulty);
while(userInput[0]=='y' || userInput[0]=='Y'){
//we are Randomely Generating a Number...
srand(time(0));
randomNum=rand()%difficulty +1; //it will produce number between 1 to difficulty level...
//printf("Random number generated is : %d\n",randomNum);
numOfGuesses=0;
do{
printf("Guess the number between 1 to %d :-D\n",difficulty);
scanf("%d",&guessedNumber);
numOfGuesses++;
//now we will return a output to user ...if the guessed number by user is smaller,larger or equal
//to random Number generated by CPU..
if(guessedNumber>randomNum){
printf("Please Enter a Smaller Number\n");
}else if(guessedNumber<randomNum){
printf("Please Enter a Larger Number\n");
}else{
printf("Yayy!! You guessed the correct number in %d attempt\n\n\n",numOfGuesses);
}
}while(guessedNumber!=randomNum);
if(highestScore >= numOfGuesses){
highestScore=numOfGuesses;
}
int choice=1;
while(1){
//to check if any other player also wanted to play game and to want to give competition
//to our player
printf("IF YOU WANT TO PLAY AND GIVE COMPETITION TO OUR CURRENT CHAMPION ,PLEASE TYPE '1'\n");
printf("ELSE IF TYPE '2' TO KNOW THE HIGHEST SCORE TILL NOW...\n");
printf("ELSE TYPE '0' TO EXIT THIS GAME :-D\n\n");
//TO STORE THE CHOICE OF THE NEXT PLAYER...
scanf("%d",&choice);
if(choice==2){
printf("HIGHEST SCORE TILL NOW IS : %d\n\n",highestScore);
}else if(choice==0){
printf("GAME IS SHUTTING DOWN...THANK YOU FOR PLAYING WITH US :-P");
exit(0);
}else if(choice==1){
break;
}
}
}
}else{
printf("YOU SELECTED 'no' OPTION...WE WILL WAIT FOR YOU TO PLAY WITH US SOME OTHER TIME :-D\n" );
}
return 0;
}