当前位置:网站首页>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 )!.
边栏推荐
- leetcode:918. 环形子数组的最大和【逆向思维 + 最大子数组和】
- [machine learning] model and cost function
- nacos无法修改配置文件Mysql8.0的解决方法
- ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
- 剑指 Offer II 032. 有效的变位词
- Golang keyboard input statement scanln scanf code example
- leetcode - 384. 打乱数组
- Sword finger offer 04 Find in 2D array
- 指针,它那些不得不说的题目
- Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream
猜你喜欢

leetcode - 384. 打乱数组

Sword finger offer day 1 stack and queue (simple)

爱可可AI前沿推介(6.25)

Golang keyboard input statement scanln scanf code example

剑指Offer 第 2 天链表(简单)

New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and

Fedora 35 deploys DNS master-slave and separation resolution -- the way to build a dream

Maui的学习之路(二)--设置

Which Chinese virtual human is better? Sullivan, IDC: Xiaobing Baidu Shangtang ranks in the first echelon

关于猜数字游戏的实现
随机推荐
leetcode - 384. 打乱数组
Sword finger offer day 3 string (simple)
Online service emergency research methodology
三行代码简单修改jar包的项目代码
初始c语言时的一些知识
15 basic SEO skills to improve ranking
Optimization of lazyagg query rewriting in parsing data warehouse
AGCO AI frontier promotion (6.25)
Drago Education - typescript learning
Native JS --- infinite scrolling
Openstack learning notes (II)
Back test of quantitative trading - tqzfuturerenkowavestrategy
重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
康威定律,作为架构师还不会灵活运用?
. NET in China - What's New in . NET
CUDA error: unspecified launch failure
À propos du stockage des données en mémoire
JVM参数解释
初始c语言的知识2.0
Golang keyboard input statement scanln scanf code example