当前位置:网站首页>[C language] number guessing game + shutdown applet
[C language] number guessing game + shutdown applet
2022-07-23 14:33:00 【Brain melon seed Weng Feng】
List of articles
Preface
Today, I write guessing number games and shutdown applet , You can also have fun while studying , Learning and entertainment are the same , Let's experience the happiness of writing games !
1. Guess number games
Let the computer generate 1~100 The random number , Then guess the number , Guess the number must be bigger , Guess a little . Guess big return guess big , Guess small, then return to guess small , Until you guessed right , Guess right and end the game . After playing the number guessing game, we don't feel satisfied. We need to do it again , We're going to use a loop .
(1) generate menu
At the beginning of the game, we have to print the menu , So we use do while Cycle print menu , Pack a simple menu function
void menu()
{
printf("*******************************\n");
printf("******** 1.play *******\n");// choose 1 Enter the game
printf("******** 0.exit *******\n");// choose 0 Quit the game
printf("*******************************\n");
}
(2) Whether to enter the game
Follow the menu prompts , Do we play games , You have to enter a number to judge whether the player enters the game , So let's define a variable input, We use switch and case Statement to select , choice 1 Just enter the game and guess the number ; choice 0 Then quit the game ; If you choose other, you will be prompted with the wrong choice , To choose !
int main()
{
int input = 0;
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 1:
printf(" Guess the number \n");
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , To choose !\n");
break;
}
} while (input);// The circulation condition here is also input, Input 0, It will exit the loop directly
return 0;
}
(3) Guess the number game()
- Generate random number
Players play games , We have to let the computer randomly generate some numbers , Let the players guess , We used rand function , Let's see rand Use of functions :
stay rand The use of functions is mentioned , call rand Before the function, you need to call sand function To set up our random generator ,sand The use of the function is shown in the figure below :
We want random numbers , The computer will generate different random numbers , We need to pass a value here , This value is time , Time changes every minute and every second , Here we introduce a timestamp
Be careful : We don't generate random numbers once, the game only needs to be generated once, so we will srand() Function in our main function
void game()
{
srand((unsigned int)time(NULL));
//1. Generate random number
int ret = rand()%100+1;// Generate 0~100 The random number
}
- Players guess numbers
Let's write a loop , Guess big return guess big , Guess small return guess small , Until you guessed right
void game()
{
int guess = 0;
//1. Generate random number
int ret = rand()%100+1;// Generate 0~100 The random number
//2. Guess the number
while (1)
{
printf(" Please guess the number :>");
scanf("%d", &guess);
if (guess > ret)
{
printf(" Guess the \n");
}
else if (guess < ret)
{
printf(" Guess a little \n");
}
else
{
printf(" congratulations , Guessed it \n");
break;
}
}
}
(4) The whole game complete code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("*******************************\n");
printf("******** 1.play *******\n");// choose 1 Enter the game
printf("******** 0.exit *******\n");// choose 0 Quit the game
printf("*******************************\n");
}
void game()
{
int guess = 0;
//1. Generate random number
int ret = rand()%100+1;// Generate 0~100 The random number
//2. Guess the number
while (1)
{
printf(" Please guess the number :>");
scanf("%d", &guess);
if (guess > ret)
{
printf(" Guess the \n");
}
else if (guess < ret)
{
printf(" Guess a little \n");
}
else
{
printf(" congratulations , Guessed it \n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf(" Please select :>");
scanf("%d", &input);
switch (input)
{
case 1:
game();// Guess the whole logic of numbers
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , To choose !\n");
break;
}
} while (input);
return 0;
}
Play a game !
2. Shut down the applet
Here is a hint that the shutdown applet is purely entertainment , You can also spoof !
Write a shutdown program , Just run , The computer is 1 Shut down in minutes , If input : I am a pig , To cancel shutdown .
We need to call the system command here shutdown -s -t 60 (-t Indicates setting the time to shut down ,60 Express 60 second ), The system cancel shutdown command is shutdown -a
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char input[20] = {
0 };
system("shutdown -s -t 60");
again:
printf(" Please note that , Your computer is 1 Shut down in minutes , If I'm a pig , Just cancel the shutdown \n");
scanf("%s", input);
if (strcmp(input, " I am a pig ") == 0)
{
system("shutdown -a");
printf(" Shutdown cancelled \n");
}
else
{
goto again;
}
return 0;
}
This chapter ends here , If there is anything bad written , Please correct me. .
If you think it's good and helpful to you, please give support to the third company !
Fighting!!!
边栏推荐
- The shell needs to know the commands when running
- npm warn config global `--global`, `--local` are deprecated. use `--location=global` instead.
- 关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
- 右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
- Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)
- Description of test platform and hardware design
- wacom固件更新错误123,数位板驱动更新不了
- 链下数据互操作
- Surrounded Regions
- 在使用 VScode 进行代码格式化后,保存发现代码又变乱了,怎么办?vs去掉格式化
猜你喜欢

ValidationError: Invalid options object. Dev Server has been initialized using an options object th

Towhee 每周模型

Pagehepler lost the pit of the original SQL order by condition
![Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)](/img/f0/9491ccc2a86d95bb30066397fb9fb6.png)
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)

Canvas from getting started to persuading friends to give up (graphic version)

ThreadLocal interview Kills 11 consecutive questions

494. 目标和

子序列 --- 编辑距离

右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法

单调栈!!!
随机推荐
炫酷代码雨动态背景注册页面
websocket通用化封装设计与实现
CPU, memory, disk speed comparison
How can manual testing turn to automated testing? Byte 5 years of automation experience talk about
Sampling and data driven
Wacom firmware update error 123, digital board driver cannot be updated
152. 乘积最大子数组
股票炒股开户风险性大吗,安全吗?
Chapter 2 basic query and sorting
STM32 output sine wave +cubemx configuration +hal Library
优化华为云服务器采用Key登陆
ThreadLocal interview Kills 11 consecutive questions
-bash: ifconfig: command not found
Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
第4章 集合运算
webstrom ERROR in [eslint] ESLint is not a constructor
FFmpeg 2 - ffplay、ffprobe、ffmpeg 命令使用
Towhee weekly model
打家劫舍!
Le shell a besoin de connaître les commandes


