There is an eye on the top of the canyon. Why don't we write a guessing number C language ourselves

Posted by madmax on Mon, 17 Jan 2022 19:24:13 +0100

What should you do if you always encounter an eye edge when galloping in the summoner Canyon?
No problem!
Teach you to guess numbers!
Get rid of the trouble of eye edge and find the happy mood when playing small games!

To guess the number, we have to have a random number first. How do we generate it?

int main()
{
	int a=rand();
	printf("%d", a);
	return 0;
}

It looks simple, doesn't it? But careful old fellow will find the problem!

Although I show 41 here, maybe yours is different from mine, you will find that no matter how many times you run the code, the result of the program is the same as before! Amazing! I used a fake rand ()?

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.

The rand function returns 0 to RAND_ A pseudo-random integer in the max range. Use the srand function to seed the pseudo-random number generator before calling Rand.

oh I see. According to msdn's explanation, rand generates a "pseudo-random integer"!

In fact, if you think about it carefully, there is no real random in the computer world! The operation of all functions is based on algorithms, so rand is not random in our understanding!

So, how can we achieve a more reasonable random number in the program?
Of course, we need a lot of random numbers to realize this game!
Then we need to use a magical function - srand
Its prototype is

void srand(usigned int seed)

srand is usually used in conjunction with rand to generate pseudo-random number sequences



Look! According to the change of shaping in srand, it will also lead to the change of rand (), then according to this principle, we can create a "random number generator"
The difficulty is! How can we make the shaping of srand constantly change?
Master Bruce Lee once said, "Be water! My friend!"
Water? Flow? What can flow and grow like water?

Time!

We need the power of time!
We need to use the time function to help srand function be water!

The time function returns the number of seconds elapsed since midnight (00:00:00), January 1, 1970, coordinated universal time, according to the system clock. The return value is stored in the location given by timer. This parameter may be NULL, in which case the return value is not stored

The time function returns the number of seconds elapsed since midnight (00:00:00) on January 1, 1970 according to the system clock to coordinate universal time. The return value is stored in the position given by the timer. This parameter may be NULL, in which case the return value is not stored.

Look! Time is flowing! The time function can return an integer from 70 years ago, and this integer has been changing! Then, our "random number generator" will be completed!
We know from the concept of msdn that time (NULL) does not store the return value, but we can take it directly and return it to srand!

srand((unsigned)time(NULL));

int main()
{
	srand((unsigned)time(NULL));
	int a=rand();
	printf("%d", a);
	return 0;
}

In this way, each time we run the program, the number generated is different!
The random number problem has been solved! Don't worry, we'll do the game right away!

#include<stdio.h>
#include<stdlib. h> / / call the header file of srand
#include<time. h> / / header file for calling time function
int main()
{
    srand((unsigned)time(NULL));//It can be used once in the main function as a seeder to help generate random numbers
	int p = 1;//First define a variable to control the entry and exit loop
	while (p)//When you enter 0, jump out of the loop and exit the game
	{
		interface();//Function of login interface
		scanf("%d", &p);//Enter whether to enter the game, 1 to enter, 0 to exit
		switch (p)//Enter different entrances according to the input p
		{
		case 1:
			game();//Game function
			break;
		case 0:
			break;//Exit client directly
		default:
			printf("Fool, wrong input, re-enter!\n");//Improve fault tolerance and avoid press errors
			break;
		}
	}
	return 0;
}

The general framework of the main function is ready. Let's make an exquisite landing page again!

void interface()
{
	printf("♥🌂🌂🌂🌂🌂🌂🌂♥\n");
	printf("********************\n");
	printf("********************\n");
	printf("1 Enter the game, 0 exit the game\n");
	printf("********************\n");
	printf("********************\n");
}

Hey, hey, isn't it good?
Simple, generous and simple! Protect user privacy!
next! Is the top priority! The function ontology of the game!

void game()
{
	int t = rand()%100+1;//Generate the function, and control the number between 1-100 for the game experience
	int q = 0;//Define the number to enter
	printf("Please guess a number!\n");
	while (1)//Unimpeded entry into the cycle, you can play multiple times
	{
		scanf("%d", &q);
		if (q > t) {
			printf("hero! It's too big!\n");
		}
		else if (q < t) {
			printf("hero! It's too small!\n");
		}
		else {
			printf("brother! That's right!\n");
			break;//After you guess right, help jump out of the loop and return to the main page
		}
	}
}

In this way, the simple number guessing game is completed!
I hope you have a good time~~

Topics: C Back-end