C language game - guess the number

Guess the number game is a simple and fun game where players need to guess a random number according to the prompts.
insert image description here

//游戏菜单
void menu()
{
    
    
	printf("*********************\n");
	printf("*       1.play      *\n");
	printf("*       0.exit      *\n");
	printf("*********************\n");
}

Here is a function written in C that displays a simple game menu with two options: "play" and "exit".

srand((unsigned int)time(NULL));

This is a call to a C language standard library function for generating random numbers. Before calling this function in the program, the header files time.h and stdlib.h must be included.

The purpose of this function is to set a seed for the random number generator to generate different sequences of random numbers. time(NULL) returns the current time in seconds, so it can be used as a seed for more random results.

Specifically, the srand() function needs to pass in an unsigned int type parameter as a seed. You can usually use the time value returned by the time(NULL) function as a seed, because the time value is different each time the program runs, so the generated random numbers are also different. Therefore, this function is usually called once at the beginning of the program to ensure that the generated sequence of random numbers is sufficiently random.

do
	{
    
    
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);

This is a do-while loop structure in C language, used to implement a simple game menu. Here is an explanation of the code:

First, call the menu() function to display the game menu.
Then, use the printf() function to prompt the user for a selection, and use the scanf() function to read the user's selection from the standard input stream and store it in the input variable.
Next, use a switch statement to perform an action based on the user's selection. If the user selects 1, call the game() function to start the game; if the user selects 0, output a message indicating that the game has exited; otherwise, output an error message and prompt the user to select again.
Finally, use the do-while loop structure to repeat the above steps until the user chooses to exit the game (that is, the value of input is 0).
At the end of the loop, the program will exit the game and finish running.

void game()
{
    
    
	int ret = rand() % 100 + 1;
	int num = 0;
	/*printf("%d\n", ret);*/
	while (1)
	{
    
    
		printf("请猜数字:>");
		scanf("%d", &num);
		if (num < ret)
		{
    
    
			printf("猜小了\n");
		}
		else if (num > ret)
		{
    
    
			printf("猜大了\n");
		}
		else
		{
    
    
			printf("猜对了\n");
			break;
		}
	}
}

This is a C language function used to implement a simple number guessing game. Here is an explanation of the code:

First, use the rand() function to generate a random integer between 1 and 100, and store it in the ret variable.
Then, use the while loop structure to implement the core logic of the game. In each round of the loop, use the printf() function to prompt the user to enter a guessed number, and use the scanf() function to read the user's input from the standard input stream and store it in the num variable.
Then, use the if-else structure to judge the relationship between the user's guess and the size of the random number ret, and output the corresponding prompt information. If the guessed number is smaller than the random number, the user is prompted to guess that it is smaller; if the guessed number is larger than the random number, the user is prompted to guess that it is larger; if the guessed number is equal to the random number, a congratulatory message is output and use break statement breaks out of the loop.
Finally, use the game() function to end and return to the main function.
This function implements a simple number guessing game, in which the rand() function is used to generate random numbers, and the loop structure and conditional statements implement the core logic of the game.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
void game()
{
    
    
	int ret = rand() % 100 + 1;
	int num = 0;
	/*printf("%d\n", ret);*/
	while (1)
	{
    
    
		printf("请猜数字:>");
		scanf("%d", &num);
		if (num < ret)
		{
    
    
			printf("猜小了\n");
		}
		else if (num > ret)
		{
    
    
			printf("猜大了\n");
		}
		else
		{
    
    
			printf("猜对了\n");
			break;
		}
	}
}
//游戏菜单
void menu()
{
    
    
	printf("*********************\n");
	printf("*       1.play      *\n");
	printf("*       0.exit      *\n");
	printf("*********************\n");
}
int main()
{
    
    
	srand((unsigned int)time(NULL));
	int input = 0;
	do
	{
    
    
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_51799303/article/details/130760384