Guess the number game (if you guess wrong, your phone will shut down. It is recommended to ask your roommate to help you play)

Preface
Today I will show you how to write a simple number-guessing game. If you guess n times incorrectly (set by yourself), the computer will shut down. What are you waiting for? Learn it quickly and send it to your roommate to help you play!
Insert image description here
Text:
Generation of random numbers
First we need to learn how to create random numbers. This requires the use of the rand function in the C language. However, the return value of the rand function (0 to 2^31-1) does not seem to be so random, because every The starting point is the same every time, so we need to give it a random starting point. To set the starting point for the rand function, we need to use the srand function. The srand function has a parameter, and the generated starting point will change according to the size of the parameter. Then the question Here we go again, don’t we need a parameter that changes? How to find it? We know that time is constantly changing every moment. Recording time is not only the timing method of 24 hours a day, but also a method of expressing time called timestamp. We can get the current time correspondence by calling the time function. The timestamp is passed as a parameter to the srand function, so that we can get random numbers!
Shutdown penalty.
Regarding how to use code to shut down, you only need to write the following code:
system("shutdown -s -t 60"); where shutdown means shutdown, and 60 means that it will shut down after 60 seconds (you can adjust it by yourself) size), then it is very simple to terminate the timing. Just write this code to terminate the timing, system("shutdown -a");
then the principle has been explained clearly to everyone. Without further ado, let's go directly to the code!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void game()//设置game函数
{
    
    
	int num = rand() % 100 + 1;//使随机数的范围在1-100之间
	int ret = 0;
	int n = 10;//设置有十次机会如果都用完了,还没对只能等待关机了
	while (n--)
	{
    
    
		scanf("%d", &ret);//玩家输入数字
		if (ret < num)//小提示也可以不加折磨人
		{
    
    
			printf("选小了\n");
		}
		else if (ret > num)
		{
    
    
			printf("选大了\n");
		}
		else
		{
    
    
			system("shutdown -a");//猜对了阻止关机
			printf("选对了!!!你是天才!!!!!!!!!!!");
		}
	}
	if (n == 0)//失败了,败者就要任凭摆布[doge]
		printf("自尽吧!阿乌拉(指电脑)\n");
}
int main()
{
    
    
	int a = 0;
	printf("                       猜数字游戏\n                       play => 1\n                       exit => 0\n");//打印菜单
	scanf("%d", &a);//选择是否开始游戏
	if (a == 1)
	{
    
    
		system("shutdown -s -t 60");60秒倒计时不选出正确答案就会关机
		printf("开始游戏\n你现在有60s的时间输入1-100的数字如果没有输对正确数字将会受到惩罚!\n");
		srand((unsigned int)time(NULL));//设置随机数起点
		game();//进入游戏

	}
	else if (a == 0)//选择退出游戏
	{
    
    
		printf("退出游戏");
	}
	else//乱输入,重新输
	{
    
    
		printf("出错,请重试。");
	}
	return 0;
}

This is the mini game shared in this issue. Friends who see here must have been unable to resist the urge to play games with their roommates, but don’t worry, it’s not too late to give the blogger a follow, like, and save it before leaving. ! The blogger will continue to update more interesting knowledge sharing, see you in the next issue!

Guess you like

Origin blog.csdn.net/Tokai___Teio/article/details/135060121