当前位置:网站首页>How to use C language to realize [guessing numbers game]
How to use C language to realize [guessing numbers game]
2022-04-23 02:54:00 【Toilet look algorithm】
Thousands of miles to find the mountain, through hundreds of difficulties , Once I see the well, I will die nine times .
Don't suffer from learning , It's about the pain of life .
Catalog
️ Four 、 The process of the game
️ 5、 ... and 、 The code analysis
1. The way to generate random numbers
2. Method of setting random number range
One 、 Preface
After learning the cycle 、 Branch 、 After and function , You can write some simple games to add fun to your programming path . It not only improves the coding ability , You can also learn and play , It's wonderful !
Two 、 The game logic
1. Print selection menu (1.play、0.exit)
2. call rand() Function to generate random numbers
3. Set random number range
4. Guess the number
5. Judge the size of the guess
3、 ... and 、 Mind mapping
Four 、 The process of the game
5、 ... and 、 The code analysis
1. The way to generate random numbers 
Only use rand() Function to generate random numbers is not possible , Because it generates the same random number every time it plays , That is, random numbers are not random , So we need to adopt srand() function , Plant a random seed , To generate random numbers . According to time, it changes all the time , It can be used time() Time function to generate seeds .
here time_t The type of stay C It is defined as a long integer in the language , and srand() The argument type of the function is unsigned int ( Unsigned shaping ), So when you use it, you have to force type conversion , and **time()** function , There is no need to set parameters , So use NULL Instead of .
2. Method of setting random number range
We have let the system randomly generate a random number , Because the range of random numbers generated by the system is too large , It takes time to guess , We first Determine the range of a random number , stay 0–100 Between .rand()%100+1, You can get 0-100 Random number between .rand()%100 Get is 1~99 Random number between , Plus 1 Namely 1~100 The random number between .
6、 ... and 、 Complete code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("**************************\n");
printf("******* 1.play *******\n");
printf("******* 2.exit *******\n");
printf("**************************\n");
}
void game()
{
// Guess the realization of the number game
//1. Generate random number
//rand Function returns a 0-32767 The random value between
// Time - Time stamp
int ret = rand() % 100 + 1;
//%100 The remainder of is 0-99, then +1, The scope is 1-100
//printf("%d\n",ret);
//2. Guess the number
int guess = 0;
while (1)
{
printf(" Please guess the number :");
scanf("%d", &guess);
if (guess < ret)
{
printf(" Guess a little \n");
}
else if (guess > ret)
{
printf(" Guess the \n");
}
else
{
printf(" congratulations , Guessed it \n");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();// Print menu
printf(" Please select :");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf(" Quit the game \n");
break;
default:
printf(" Wrong choice , To choose \n");
break;
}
} while (input);
return 0;
}
版权声明
本文为[Toilet look algorithm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220715148500.html
边栏推荐
- MySQL function syntax
- The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
- The way to conquer C language
- leetcode 烹飪料理
- Intelligent agricultural management model
- Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
- 基于多态的职工管理系统源码与一些理解
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
- 接口请求时间太长,jstack观察锁持有情况
- Looking for a job, writing a resume to an interview, this set of information is enough!
猜你喜欢
Devil cold rice 𞓜 078 devil answers the market in Shanghai and Nanjing; Communication and guidance; Winning the country and killing and screening; The purpose of making money; Change other people's op
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
The way to conquer C language
Log cutting - build a remote log collection server
Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
Kubernetes study notes
Fashion MNIST 数据集分类训练
LeetCode 1450 - 1453
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
随机推荐
ROP Emporium x86_64 7~8题
Learn regular expression options, assertions
How to build an integrated industrial Internet plus hazardous safety production management platform?
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Decision tree principle of machine learning
Winsock programming interface experiment: Ping
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
@Usage and difference between mapper and @ repository
Essential qualities of advanced programmers
First knowledge of C language ~ branch statements
魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
Efficient music format conversion tool Music Converter Pro
Rhcsa day 1 operation
解决win7 中powershell挖矿占用CPU100%
Flink stream processing engine system learning (I)
Rhcsa day 3 operation
Face longitude:
B blocks of the 46th ICPC Asian regional competition (Kunming)
Sonic cloud real machine tutorial
php+mysql对下拉框搜索的内容修改