"guess the number"

Ideas and steps

To design this number guessing game, we first need the computer to generate a random number, and then compare the number we input with the random number
If the number we input is less than the random number , then the output guess is too small. If the number we input is greater than the random number, the output guess is too large.
If the number we input is equal to the random number, then the output congratulations, you guessed it right.
Step 1: Generate a random number
Step 2: Determine the size of the input number and the random number

code implementation

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//猜数字
//先生成随机数
//然后比较,如果输入值比生成的随机数小,则提示猜小了
//如果输入值比生成的随机数大,则提示猜大了
//如果输入值比生成的随机数相等,则恭喜你,游戏结束了
void menu()
{
    
    
	printf("******************************\n");
	printf("*******    0.exit       ******\n");
	printf("*******    1.play       ******\n");
	printf("******************************\n");

}
void game()
{
    
    
	//生成随机数
	int ret = rand() % 100 + 1;//任何数%100->0-99  %100+1->1-100
	//使用rand函数需要调用srand函数,在srand函数中我们用时间戳参数,时间戳在不停的变化,这样生成的随机值才是随机的。
	// rand函数是一个伪随机数生成函数
	// rand函数生成的随机数的范围是0- RAND_MAX(32767)
	printf("%d", ret);
	//猜数字
	int guees = 0;
	int count = 1;
	while (count)
	{
    
    
		printf("请输入>:");
		scanf("%d", &guees);
		if (guees < ret)
		{
    
    
			printf("猜小了\n");
		}
		else if (guees > ret)
		{
    
    
			printf("猜大了\n");
		}
		else
		{
    
    
			printf("恭喜你,猜对了\n");
			break;
		}
		count++;
		if (count == 4)//设置次数,如果三次还没有猜对,那么游戏就结束了
		{
    
    
			printf("输入次数已用完,游戏结束\n");
			break;
		}
	}
}
int main()
{
    
    
	int input = 0;
	//因为time函数的返回类型是time_t,但是srand函数的参数类型是unsigned int,所以需要强制转换一下
	srand((unsigned int)time(NULL));//用一个时间戳来生成一个随机值
	do
	{
    
    
		menu();
		printf("请选择>:");
		scanf("%d", &input);
		switch(input)
		{
    
    
		case 0:
			printf("游戏结束\n");
			break;
		case 1:
			game();
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

Precautions

rand function, srand function and time function
rand function
rand function is a function that generates pseudo-random numbers
The random number generated by the rand function has a range of 1-RAND_MAX (32767)
Before the rand function generates a random number, the srand function must be used to set the random number generator
The data type returned by the rand function is integer and has no parameters.
The srand function
has no return type, and the parameter type is unsigned int type
You need to call the srand function before using the rand function. Set the random number so that the rand function generates a random number
If the set number is unique, then the random number generated by the rand function is also unique, so we use the timestamp setting< a i=10> time function uses timestamp. The time function in C language can realize this function. The data type returned in this function is time_t. The parameter is a pointer to time_t*. What is a timestamp? Let’s look at the postmark first. Postmark: Postmark is a variety of stamps stamped by the post office on actual mail packages. Each postmark represents each type of letter. Timestamp: A piece of complete, verifiable data that can represent a piece of data that existed before a specific time. It is usually a sequence of characters that uniquely identifies a certain moment in time. To put it bluntly, it is time, the ever-changing time.






Guess you like

Origin blog.csdn.net/HD_13/article/details/132434298