C language beginner level: guessing numbers game

Posted by vargadanis on Sun, 16 Jan 2022 14:45:32 +0100

catalog:

1: Introduction to the number guessing game and expectation of the program

2: Program design idea:

1. General idea

2. Menu function

3. Some functions of the game

4. Main function

5. Code body

III summary

This paper mainly explains the number guessing game written in C language. The purpose is to introduce the specific usage of loops and branches in C language.

<!-- The following is the main body -- >

1: Basic introduction of guessing numbers game & expectation of program

The number guessing game, as the name suggests, is that the system randomly gives a number, and the player guesses the number. If the number guessed by the player is greater than the given number, the system will prompt that the guess is big. If the number guessed by the player is less than the given number, the system will prompt that the guess is small, so as to gradually narrow the guess range until the player guesses correctly.

2. Program expectation: after guessing the number, if the player guesses correctly, he can continue the game or exit the game

2: Programming ideas

1. General idea:

First of all, to realize the game function, there must be a menu to prompt the player how to operate. Secondly, in order to make the code readable, you can take the game part function and the menu part function as two sub functions, and then call them in the main function. Then we can use the loop in order to realize that players can play multiple times.

To sum up, we should design two sub functions: menu function and game function. A main function

2. Menu function:

Menu part function

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

Menu function design is relatively simple, just to prompt players how to operate

3. Some functions of the game

void game()
{
	srand((unsigned int)time(NULL));
	int ret = rand() % 100 + 1;
	while (1)
	{
		printf("Guess the number\n");
		int guess = 0;
		scanf("%d", &guess);
		if (guess > ret)
		{
			printf("Guess big\n");
		}
		else if (guess < ret)
		{
			printf("Guess it's small\n");
		}
		else
		{
			printf("You guessed right\n");
			break;
		}
		
	}
}

① In the game function part, first generate random numbers. The method of generating random numbers is as follows:

C language provides the function of generating random numbers, rand function

The rand function will return a random integer from 0 to RAND-MAX. using this function, you need to include the header file < stdlib h>

However, the random number generated by rand function is the same every time, so only rand function is not enough, so srand() function should be used to increase randomness

 

The latter sentence states that an srand() function should be defined before using the rand function

As shown in the underlined sentence, the srand function is the seed of the generation of a random number.

And you need an unsigned integer. You can generate a random number by entering a random number in parentheses in the srand function. Time is a changing number.

A concept of timestamp is proposed here: timestamp refers to the total number of seconds from 00:00:00 GMT on January 1, 1970 (08:00:00 GMT on January 1, 1970) to the present.

Because the parameters required by srand are unsigned, cast time. Time does not require parameters, so give him a null pointer.

Use srand((unsigned int)time(NULL))

Enter time to return a timestamp

A header file < time. Is required h>

Because the range of random numbers given is too large, it is necessary to limit the range of random numbers

As follows: int ret = rand()%100+1, limit the random number to the range of 0-100 for easy guessing

② : guessing numbers

Use the while loop to gradually narrow the guess range. When the guessed number is equal to the random number, it will prompt "guess right".

4. Main function

int main()
{
	int input = 0;

	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("Input error, please re-enter");
		}
	} while (input);
	return 0;
}

switch statement and do while statement are used in the main function

The main purpose is for players to choose whether to enter the game

5. Code body

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
void menu()
{
	
	printf("***********************************************\n");
	printf("*************************1play*****************\n");
	printf("*************************0exit*****************\n");
	printf("***********************************************\n");
}
void game()
{
	srand((unsigned int)time(NULL));
	int ret = rand() % 100 + 1;
	while (1)
	{
		printf("Guess the number\n");
		int guess = 0;
		scanf("%d", &guess);
		if (guess > ret)
		{
			printf("Guess big\n");
		}
		else if (guess < ret)
		{
			printf("Guess it's small\n");
		}
		else
		{
			printf("You guessed right\n");
			break;
		}
		
	}
}
int main()
{
	int input = 0;

	do
	{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("Input error, please re-enter");
		}
	} while (input);
	return 0;
}

3: Summary

This program is mainly to be familiar with the specific usage of loop and branch, and learn to use timestamp and rand function to generate random numbers.

I hope you will pay more attention and continue to output articles every week

Topics: C