当前位置:网站首页>C language practice - number guessing game
C language practice - number guessing game
2022-07-02 04:16:00 【Mr maple leaf】
Catalog
1. Guess the rules of the number game
2. Guess the realization of numbers
(1) Guess the subject of the number
(2) Guess the real subject of the game
1. Guess the rules of the number game
Guessing numbers, as the name suggests, is to let the system generate a random number , Then the player guesses the random number , If you guess big, you will give a hint that you guess big , Guess small will prompt guess small , Until the player guesses right .
2. Guess the realization of numbers
(1) Guess the subject of the number
1) First , Let's create a game menu first , choice 1 Enter the game (1.PLAY), choice 0 Quit the game (0.EXIT)
2) Then use the loop statement , Build the game framework , Enable players to play multiple games .
3) Finally, the specific idea of designing the game . Make the system generate a random number , Let the player enter a number , Then compare the two numbers , Give hints , Until the player guesses right .
4) Set the range of random numbers as 1~100 Between
The code is as follows
#include<stdio.h>
void menu()
{
printf("******************************\n");
printf("********* Press 1:PLAY *********\n");
printf("********* Press 0:EXIT *********\n");
printf("******************************\n");
}
int main()
{
int intput = 0;
do
{
menu();
printf(" Please select :\n");
scanf("%d", &intput);
switch (intput)
{
case 0:
printf(" Quit the game \n");
break;
case 1:
printf(" Play a game \n");
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (intput);
return 0;
}
(2) Guess the real subject of the game
case 1:
printf(" Play a game \n");
break;
Guess the main body of the game has been completed , But the real subject has not yet begun , Guessing numbers is not the only game “ Play a game ” These three words are so simple , We write a function to really realize the process of guessing numbers .
case 1:
game();
break;
1) Generate random number
Okay , Here we need to know how to generate random numbers .
C In language rand() Function can generate random numbers , Use it to introduce the header file , The range of generating random numbers is 0 to RAND_MAX(0 ~ 32767)
But use it like this rand The function is wrong , Cause it to generate the same random number every time
Let's take a look at the circle in the figure below , In the use of rand Function must be called before srand function
Let's see srand function , An integer parameter must be entered in the frame of the figure and it is random , Only in this way can we meet our needs , At this time, the time stamp will be used .
2) Time stamp
What is? Time stamp ? In a word, it can meet the demand of guessing number games (≖_≖ )
No, you can learn by yourself ( ﹡ˆoˆ﹡ )
C There is one in language time function , It can return a timestamp , We can use it directly .
time The parameters in the parentheses of the function are filled directly NULL , We just need it to generate and return a random number ;
time The function is simple to use , I don't want to explain more , Explain another article ...( Feel for yourself )
It is worth noting that random numbers with a certain range are generated
So how do we generate a range of numbers ?
It's simple , stay rand() Take the remainder later , If %100, The value is 0-99 了 , Then subtract a certain value , It's a definite range .
For example, we want to take 1-100, So you need rand()%100+1.
3)game() The function code is as follows :
void game()
{
srand((unsigned int)time(NULL));
int ret = rand() % 100 + 1;//%100 The remainder of is 0-99, Then add 1, The scope is 1-100
int guess = 0;// Guess the number
while (1)
{
printf(" Please guess the number :\n");
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;
}
}
}
3. Master code
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("******************************\n");
printf("********* Press 1:PLAY *********\n");
printf("********* Press 0:EXIT *********\n");
printf("******************************\n");
}
void game()
{
srand((unsigned int)time(NULL));
int ret = rand() % 100 + 1;//%100 The remainder of is 0-99, Then add 1, The scope is 1-100
int guess = 0;// Guess the number
while (1)
{
printf(" Please guess the number :\n");
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 intput = 0;
do
{
menu();
printf(" Please select :\n");
scanf("%d", &intput);
switch (intput)
{
case 0:
printf(" Quit the game \n");
break;
case 1:
game();
break;
default:
printf(" Wrong choice , Please reselect \n");
break;
}
} while (intput);
return 0;
}
4. Last
This concludes the article ! I hope this article can help you !
边栏推荐
- 5g era is coming in an all-round way, talking about the past and present life of mobile communication
- Pytorch---使用Pytorch进行图像定位
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- How to solve the code error when storing array data into the database
- Shutdown procedure after 60
- Is the product of cancer prevention medical insurance safe?
- WiFi 5GHz frequency
- Wechat applet JWT login issue token
- Handling of inconsistency between cursor and hinttext position in shutter textfield
- [ibdfe] matlab simulation of frequency domain equalization based on ibdfe
猜你喜欢
First acquaintance with P4 language
[C language] Dynamic Planning --- from entry to standing up
Installation et utilisation du lac bleu
Recently, the weather has been extremely hot, so collect the weather data of Beijing, Shanghai, Guangzhou and Shenzhen last year, and make a visual map
蓝湖的安装及使用
Introduction to vmware workstation and vSphere
Federal learning: dividing non IID samples according to Dirichlet distribution
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
PIP installation of third-party libraries
【c语言】基础篇学习笔记
随机推荐
60后关机程序
go 包的使用
Jetpack's livedata extension mediatorlivedata
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Déchirure à la main - tri
C语言:逻辑运算和判断选择结构例题
LCM of Spreadtrum platform rotates 180 °
QT designer plug-in implementation of QT plug-in
Suggestions on settlement solution of u standard contract position explosion
微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
How much is the tuition fee of SCM training class? How long is the study time?
IDEA xml中sql没提示,且方言设置没用。
How to solve the code error when storing array data into the database
The original author is out! Faker. JS has been controlled by the community..
Today's plan: February 15, 2022
【c语言】动态规划---入门到起立
PIP installation of third-party libraries
Homework of the 16th week
Learn more about materialapp and common attribute parsing in fluent
Hand tear - sort