当前位置:网站首页>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 !
边栏推荐
- How to solve the code error when storing array data into the database
- C语言猜数字游戏
- Shutdown procedure after 60
- BGP experiment the next day
- Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
- Introduction to JSON usage scenarios and precautions
- Spring recruitment of Internet enterprises: Kwai meituan has expanded the most, and the annual salary of technical posts is up to nearly 400000
- 阿里云polkit pkexec 本地提权漏洞
- 初识P4语言
- Wechat applet pull-down loading more waterfall flow loading
猜你喜欢

Lei Jun wrote a blog when he was a programmer. It's awesome

pip 安装第三方库

Jetpack's livedata extension mediatorlivedata

【leetcode】34. Find the first and last positions of elements in a sorted array

Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)

Suggestions on settlement solution of u standard contract position explosion

Typescript practice for SAP ui5

Document declaration and character encoding

Which is better, industrial intelligent gateway or edge computing gateway? How to choose the right one?

Force buckle 540 A single element in an ordered array
随机推荐
Use a mask to restrict the input of the qlineedit control
MySQL error: expression 1 of select list is not in group by claim and contains nonaggre
【毕业季·进击的技术er】年少有梦,何惧彷徨
Microsoft Research Institute's new book "Fundamentals of data science", 479 Pages pdf
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
BGP experiment the next day
Websites that it people often visit
Spring moves are coming. Watch the gods fight
go 包的使用
Play with concurrency: draw a thread state transition diagram
office_ Delete the last page of word (the seemingly blank page)
藍湖的安裝及使用
powershell_ View PowerShell function source code (environment variable / alias) / take function as parameter
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
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
Okcc why is cloud call center better than traditional call center?
Lei Jun wrote a blog when he was a programmer. It's awesome
Go language naming specification
Pytorch-Yolov5從0運行Bug解决:
Wechat applet JWT login issue token