当前位置:网站首页>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
边栏推荐
- leetcode 烹飪料理
- Win view port occupation command line
- Chapter IV project cost management of information system project manager summary
- Decision tree principle of machine learning
- JZ35 replication of complex linked list
- Traversée de l'arbre L2 - 006
- The penultimate K nodes in jz22 linked list
- 谷雨
- [wechat applet] set the bottom menu (tabbar) for the applet
- Shell script learning -- practical case
猜你喜欢

Android high-level interview must ask: overall business and project architecture design and reconstruction

解决win7 中powershell挖矿占用CPU100%

Practice of industrial defect detection project (III) -- Based on FPN_ PCB defect detection of tensorflow

First knowledge of C language ~ branch statements

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Innovation and management based on Scrum

Leangoo brain map - shared multi person collaborative mind mapping tool

Intelligent agricultural management model

Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署

Flink stream processing engine system learning (I)
随机推荐
字符串去掉空格问题
Processes and threads
The express project changes the jade template to art template
Rhcsa day 3 operation
leetcode 烹飪料理
Linux Redis——Redis 数据库缓存服务
Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
L2-006 树的遍历(中后序确定二叉树&层序遍历)
The second day of learning rhcsa
The problem of removing spaces from strings
Mosaic Routing: implement / home / news
Intelligent agricultural management model
The interface request takes too long. Jstack observes the lock holding
mysql function函数语法
JZ22 鏈錶中倒數最後k個結點
VirtualBox virtual machine (Oracle VM)
Machine learning (Zhou Zhihua) Chapter 14 probability graph model
OCR识别PDF文件
期中汇总(概论+应用层+运输层)
Encapsulation of ele table

