当前位置:网站首页>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 !
边栏推荐
- What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
- [personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
- Force buckle 540 A single element in an ordered array
- Message mechanism -- message processing
- [wireless image transmission] FPGA based simple wireless image transmission system Verilog development, matlab assisted verification
- powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
- Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
- 微信小程序 - 实现获取手机验证码倒计时 60 秒(手机号+验证码登录功能)
- Pytoch --- use pytoch for image positioning
猜你喜欢

Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000

Use of go package

Www 2022 | rethinking the knowledge map completion of graph convolution network

10 minutes to understand CMS garbage collector in JVM

Suggestions on settlement solution of u standard contract position explosion

Installation and use of blue lake

藍湖的安裝及使用

Jetpack's livedata extension mediatorlivedata

【c语言】基础篇学习笔记

Pytoch --- use pytoch to predict birds
随机推荐
WiFi 5GHz frequency
FAQ | FAQ for building applications for large screen devices
Feature Engineering: summary of common feature transformation methods
Installation et utilisation du lac bleu
[JS -- map string]
JVM知识点
Hand tear - sort
go 包的使用
MySQL advanced SQL statement 2
BGP experiment the next day
Www 2022 | rethinking the knowledge map completion of graph convolution network
Pytorch---使用Pytorch进行图像定位
PIP installation of third-party libraries
Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?
Thinkphp6 limit interface access frequency
C language: examples of logical operation and judgment selection structure
C语言:逻辑运算和判断选择结构例题
Is the product of cancer prevention medical insurance safe?
[C language] basic learning notes
手撕——排序