当前位置:网站首页>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 !
边栏推荐
- 【leetcode】74. Search 2D matrix
- 10 minutes to understand CMS garbage collector in JVM
- Playing with concurrency: what are the ways of communication between threads?
- Sword finger offer II 006 Sort the sum of two numbers in the array
- go 语言命名规范
- The difference between vectorresize and reverse.
- Three years of experience in Android development interview (I regret that I didn't get n+1, Android bottom development tutorial
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- How to solve the problem that objects cannot be deleted in Editor Mode
- Hand tear - sort
猜你喜欢

QT designer plug-in implementation of QT plug-in

Target free or target specific: a simple and effective zero sample position detection comparative learning method

Play with concurrency: what's the use of interruptedexception?

Common sense of cloud server security settings

How much is the tuition fee of SCM training class? How long is the study time?

Typescript practice for SAP ui5

Installation and use of blue lake

《动手学深度学习》(二)-- 多层感知机

What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?

Li Kou interview question 02.08 Loop detection
随机推荐
Installation and use of blue lake
Installation et utilisation du lac bleu
go 包的使用
A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent
[personnel density detection] matlab simulation of personnel density detection based on morphological processing and GRNN network
Is it safe to open an account with first venture securities? I like to open an account. How can I open it?
【c语言】基础篇学习笔记
Okcc why is cloud call center better than traditional call center?
How much is the tuition fee of SCM training class? How long is the study time?
go 函数
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)
千亿市场规模医疗美容行业的水究竟有多浑?
"No war on the Western Front" we just began to love life, but we had to shoot at everything
阿里云polkit pkexec 本地提权漏洞
文档声明与字符编码
66.qt quick-qml自定义日历组件(支持竖屏和横屏)
Playing with concurrency: what are the ways of communication between threads?
[ibdfe] matlab simulation of frequency domain equalization based on ibdfe
[JS -- map string]
Force buckle 540 A single element in an ordered array