# C language only uses functions and loops to implement number guessing game

Foreword:

## 2. Guess the numbers

1> If the guess is too small, the program will tell you that the guess is too small. keep guessing

2> The guess is too big, the program will tell you that the guess is too big. keep guessing
3> Guess correctly, the program will tell: Congratulations, you guessed right. Finish

### 3. The game can be played repeatedly

The total code first:

β
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

{
printf("**************************\n");
printf("******    1. play   ******\n");
printf("******    0. exit   ******\n");
printf("**************************\n");
}

void game()            //The basic logic of the game
{
int num = 0;
int ret = rand()%100+1;

while (1)
{
scanf("%d", &num);
if (num < ret)
{
printf("guess too small\n");
}
else if (num > ret)
{
printf("big guess\n");
}
else
{
printf("Congratulations, you guessed it\n");
break;
}
}
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));        //Random number generator for srand library functions
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();            //In the game function is the whole logic of guessing numbers
break;
case 0:
printf("exit the game\n");
break;
default:
printf("Choose wrong, choose again\n");
break;
}
} while (input);

return 0;
}

β

Next, we analyze it piece by piece

## 1. First determine how the subject should be written

#include<stdio.h>

void menu()        //Since the menu does not need a return value, use void
{
printf("**************************\n");
printf("******    1. play   ******\n");
printf("******    0. exit   ******\n");
printf("**************************\n");
}

int main()
{
int input=0;        //initialize the input value;
//The guessing game has to guess once, so use a do while loop
//The judgment condition can be judged by input, because 0 is false in C language, and non-zero is true.
do
{
scanf("%d",&input);
}while(input)
return 0;
}

Let's start printing out the menu again and write it down further.

tips: In the C language, it is best to compile every short paragraph after writing, so as to better find the bugs in the code, rather than finding the bugs when writing the whole thing.

## 2. Write the logic of the game

Immediately after the above, we continue to write the main function, the general idea how to go

#include<stdio.h>
#include<windows.h> //windows library function header file

void menu()        //Since the menu does not need a return value, use void
{
printf("**************************\n");
printf("******    1. play   ******\n");
printf("******    0. exit   ******\n");
printf("**************************\n");
}

int main()
{
int input=0;        //initialize the input value;
//The guessing game has to guess once, so use a do while loop
//The judgment condition can be judged by input, because 0 is false in C language, and non-zero is true.
do
{
scanf("%d",&input);
switch(input)        //Use non-0 and 0 to judge true and false
{
case 1:
game()        //Set a game function without returning a value
break;
case 0:
Sleep(1500)        //Using the Sleep library function is to achieve sleep, the unit is milliseconds, so that the code printing is slower.
system("cls");    // Use the Windows library function, the code means to clear the page
//Makes the screen clear every time the game ends.
break;
defalut:
break;
}
}while(input)
return 0;
}

ok, now we have written the general idea clearly. The next step is to write the game function. How to implement it?

We follow the idea of ββthe main function step by step to write

//Write out the game function (custom function) separately

void game()        //The game comes in from the input value, so there is no need to return a value, so void is used.
{
int num =0;    //As the title requires, we should generate random numbers, and rand can be used in c language
int ret=rand()%100+1    //The return value of rand is accepted by ret
//Since the title requirement is 1-100, the range after %100 is 1-99, and then a 1 is added to satisfy the title condition
//Next, start the main logic of the number guessing game
while(1)        //The game can be played repeatedly, loops can be used;
{
scanf("%d",&num);
if(num>ret)
printf("Guess Dalla\n");
if(num<ret)
printf("Guess Xiaola\n");
else
{
printf("congratulations, guess right\n");
break;    //out of the loop
}
}
}
ok, now we have finished writing the logic of the game~

If we concatenate one and two, compile and run, we will find that the numbers guessed each time are not random and are very similar.

What's going on here?

Further explanation: The computer cannot generate real random numbers, but some random numbers that have been written are stored in the computer, and these numbers are divided into several equal N parts, and add a number to each part. The srand() function gets this number, and rand() gets the numbers in order. When the parameter value of srand() is fixed, the number obtained by rand() is also fixed, so generally the parameter of srand is time(NULL), because the system time is always changing, so the number obtained by rand() is also It is always changing, it is equivalent to a random number. As long as the user or a third party does not set a random seed, by default the random seed is derived from the system clock. If you want to generate a sequence of random numbers in a program, you need to set the random seed at least once before generating the random numbers.

Small tips: the rand function should use a srand to set the random number generator before using it

So we need to use the time function to get the system time, which is a completely random number.

We only need to call it once in the main function, no need to call it multiple times.

int main()
{
int input = 0;
srand((unsigned int)time(NULL));    //With a null pointer, the program does not need parameters to obtain data.
//The time_t type data is converted to (unsigned) type and then passed to the srand function
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("exit the game\n");
break;
default:
printf("Choose wrong, choose again\n");
break;
}
} while (input);

return 0;
}

ok so we're done~. splicing them together is

#include<stdio.h>
#include<windows.h> //windows library function header file
#include<time.h>
#include<stdlib.h>

void menu()        //Since the menu does not need a return value, use void
{
printf("**************************\n");
printf("******    1. play   ******\n");
printf("******    0. exit   ******\n");
printf("**************************\n");
}

void game()
{
int num = 0;
int ret = rand() % 100 + 1;
while (1)
{
printf("Guess the number:");
scanf("%d", &num);
if (num < ret)
{
printf("guess too small\n");
}
else if (num > ret)
{
printf("big guess\n");
}
else
{
printf("Congratulations, you guessed it\n");
Sleep(2000);
system("cls");

break;
}
}
}

int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
scanf("%d", &input);
switch (input)
{
case 1:
game();

break;
case 0:
Sleep(1500);
system("cls");
break;
default:
break;
}
} while (input);
return 0;
}

You're done!~

Posted by lipun4u on Wed, 02 Nov 2022 09:12:31 +1030