当前位置:网站首页>On the realization of guessing numbers game
On the realization of guessing numbers game
2022-06-25 13:20:00 【LIn_ jt】
On the realization of simple Guessing Number Game
For today's Gobang game , There are several features :
- Players enter numbers to choose whether to play or exit the game , In case of wrong selection, you will be prompted and re-enter
- After entering data through the keyboard , The computer will prompt the player that the data guessed is too large or too small , If you guessed right, print congratulations , Guessed it , Then let the player choose whether to play the game again
- The range of random numbers is 1-100
I don't say much nonsense , Go straight to the code :>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void menu()
{
printf("**********************\n");
printf("****** 1.play *******\n");
printf("****** 0.exit *******\n");
printf("**********************\n");
}
void game()
{
int input2 = 0;
int ret = rand() % 100 + 1;// Generate a random number ;
while (1)
{
printf(" Please guess the number :>");
scanf("%d", &input2);
if (input2 < ret)
{
printf(" Guess a little \n");
}
else if (input2 > ret)
{
printf(" Guess the \n");
}
else
{
printf(" congratulations , Guessed it \n");
break;
}
}
}
int main()
{
int input1 = 0;
srand((unsigned int)time(NULL));// Set the starting point of random number
do
{
menu();// Here menu Is a menu function
printf(" Please enter a number :>");
scanf("%d", &input1);
switch(input1)
{
case 1:
printf(" Guess the number game \n");
game();
break;
case 0:
printf(" Exit procedure \n");
break;
default:
printf(" Input error , Please re-enter \n");
break;
}
} while (input1);
return 0;
}
Code explanation everywhere

About... In the main function srand function , Set a random number starting point for , But to generate a random number , You need to pass him a random number , therefore , We thought of using time as a parameter , And cast to (unsigned int) type , To match srand The grammar of .
menu Menu functions created for ourselves , Used to output and prompt the player what to input

About do-while loop :

Because the game is executed at least once , So with do while Loop to cut in , Among them switch case It is used to judge the input of players to execute the corresponding results
If you choose 1. Then play the game , If you choose 0, Then quit the game , If you choose another number , Then re-enter
Yes game The explanation of functions :

game Function ,rand The function is used to generate a random number , stay while In circulation , The number entered by the player will match rand Compare the random numbers generated by the function , If the input is too small or too large , The computer will prompt , Let the player re-enter , If the player guesses right , And then jump out of the loop , Let the player restart the game or exit the program .
A little change in the procedure

The previous print takes up part of the screen , It doesn't look good , Therefore, the following changes are made to the procedure :



In this way, the print will become more beautiful ( I personally think so )!.
边栏推荐
- Regular match the phone number and replace the fourth to seventh digits of the phone number with****
- QT mouse tracking
- 药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
- Solve the problem that yarn cannot load files in vs Code
- MySQL 学习笔记
- Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
- OpenStack学习笔记-Glance组件深入了解
- C # switch between Chinese and English input methods
- [pit avoidance means "difficult"] actionref current. Reload() does not take effect
- And console Log say goodbye
猜你喜欢
随机推荐
leetcode:剑指 Offer II 091. 粉刷房子【二维dp】
Germany holds global food security Solidarity Conference
KVM 脚本管理 —— 筑梦之路
Sword finger offer II 028 Flatten multi-level bidirectional linked list
Sword finger offer II 032 Effective anagrams
Seven competencies required by architects
Three lines of code to simply modify the project code of the jar package
C # switch between Chinese and English input methods
Sword finger offer day 1 stack and queue (simple)
剑指offer 第 3 天字符串(简单)
leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
Native JS --- infinite scrolling
[turn] starting from the end, analyze in detail how to fill in the college entrance examination volunteer
[pit avoidance means "difficult"] the antd form dynamic form is deleted, and the first line is displayed by default
Accidentally modify or delete the system variable path to restore
揭秘GaussDB(for Redis):全面对比Codis
指针,它那些不得不说的题目
始终保持疫情防控不放松 营造安全稳定的社会环境
[pit avoidance means "difficult"] antd select fuzzy search
剑指 Offer 04. 二维数组中的查找

![[转]以终为始,详细分析高考志愿该怎么填](/img/77/715454c8203d722e246ed70e1fe0d8.png)







