当前位置:网站首页>C language implements a simple number guessing game
C language implements a simple number guessing game
2022-08-05 02:19:00 【[email protected]】
前言
猜数字游戏,Program randomly generated Numbers,玩家输入数字,Program prompts the player to guess the number compared with the generated random number is input big,还是猜小了.直到猜中,游戏结束.
一、大致思路
1.产生随机数,At the same time pay attention to the random number should be controlled within a certain range of,If the number in the difficult to guess,影响游戏体验.
2.判断逻辑,Because of big probability not a guess they should adopt the way of circulation to determine whether guess digital link.
3.Design a simple menu,Hint whether players into the game.So should be to play at least once in deciding whether to start the game,可以采用do while循环来实现
二、具体实现
1.Using the simple print function design game menu:
void meau()
{
printf("******************\n");
printf("*****1 play*******\n");
printf("*****0 exit*******\n");
}
About will begin to start the game is set0Quit the game is set1It has a clear benefits,Previously analysis into the game to takedo while 循环的方式,If the input select Numbers 1或0Set loop condition,A natural transition to enter1Enter the game until finishing the exit,输入0跳出循环,Just meet the set to exit the game,The general logic have.Because after entering cycle again want to face choice,So circulation on insideswitchStatement to select logic.Based on the above thinking has the following code
int main()
{
int input = 0;
do
{
meau();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
while (input);
return 0;
}
Due to the guessing game subject digital logic more,可以将其封装成gameFunction to design it.Real first first about the game is to generate a random number.可以利用randFunction to realize the generation of random Numbers.But by the input data test found a problem,The generated random number every time don't random
所以为了解决这问题,我们利用srand初始化随机数生成器,In order to make each initialization value is different,Can use timestamp to generate different random number.Because the computer time is change at any time.If the random number generated ingame函数中的,When we enter the game random number every time interval is very short, they could be the same,So random Numbers can be generated in themain函数中因为mainEvery time call only once,srandOne call can be used.
int main()
{
srand((unsigned int)time(NULL));//设置随机数生成器
.............................
...............................
..............................
}
With random Numbers will only judge logic,Set a variable to receive a random number,Then they guess number compared with the variable.If big tip player input digital big,反之亦然.If equal prompt players guessed it.Due to guess the number is not a big probability of so loop is used to implement,When prevailed in addition to remind players guess also should jump out of the loop.In order to avoid too much random number,Will generate a random number is limited to100的范围内.Will generate random Numbers of100Take more to add1即可.
代码如下:
/rand生成随机数 0~32767
//返回一个0~32767;
void game()
{
system("cls");//清理屏幕
printf("Congratulations to play\n");
//1.生成随机数
// 2.猜数字
//srand初始化随机数生成器
//时间戳
int ret = rand()%100+1;//0~100的数字
while (1)
{
printf("请猜数字\n");
int guess = 0;
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else
{
printf("恭喜猜对了\n");
break;
}
}
}
总结
1.源码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
void meau()
{
printf("******************\n");
printf("*****1 play*******\n");
printf("*****0 exit*******\n");
}
//rand生成随机数 0~32767
//返回一个0~32767;
void game()
{
system("cls");//清理屏幕
printf("Congratulations to play\n");
//1.生成随机数
// 2.猜数字
//srand初始化随机数生成器
//时间戳
int ret = rand()%100+1;//0~100的数字
while (1)
{
printf("请猜数字\n");
int guess = 0;
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else
{
printf("恭喜猜对了\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//设置随机数生成器
int input = 0;
do
{
meau();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
}
while (input);
return 0;
}
My ability is limited code implementation is simple.This is me to guess the Numbers game simple introduction,如有错误,欢迎指出!
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/217/202208050212240726.html
边栏推荐
- 【OpenCV 图像处理2】:OpenCV 基础知识
- MySQL learning
- Fragment visibility judgment
- The difference between a process in user mode and kernel mode [exclusive analysis]
- 线性表的查找
- 迁移学习——Joint Geometrical and Statistical Alignment for Visual Domain Adaptation
- The 2022 EdgeX China Challenge will be grandly opened on August 3
- PHP技能评测
- 树表的查找
- Jincang database KingbaseES V8 GIS data migration solution (3. Data migration based on ArcGIS platform to KES)
猜你喜欢

Xunrui cms website cannot be displayed normally after relocation and server change

Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中

.Net C# 控制台 使用 Win32 API 创建一个窗口

C语言实现简单猜数字游戏

【C语言】详解栈和队列(定义、销毁、数据的操作)
![[Endnote] Word inserts a custom form of Endnote document format](/img/70/e8a6f15b80e9c53db369fd715e51df.png)
[Endnote] Word inserts a custom form of Endnote document format

ExcelPatternTool: Excel表格-数据库互导工具

多线程(2)

js中try...catch和finally的用法

DAY23: Command Execution & Code Execution Vulnerability
随机推荐
HOG feature study notes
《.NET物联网从零开始》系列
第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
leetcode-对称二叉树
“嘀哩哩,等灯等灯”,工厂安全生产的提示音
在这个超连接的世界里,你的数据安全吗
短域名绕过及xss相关知识
重新审视分布式系统:永远不会有完美的一致性方案……
KingbaseES V8 GIS data migration solution (2. Introduction to the capabilities of Kingbase GIS)
source program in assembly language
RAID磁盘阵列
How to simply implement the quantization and compression of the model based on the OpenVINO POT tool
如何看待自己的羞愧感
“配置”是把双刃剑,带你了解各种配置方法
Understand the recommendation system in one article: Recall 06: Two-tower model - model structure, training method, the recall model is a late fusion feature, and the sorting model is an early fusion
ARM Mailbox
ExcelPatternTool: Excel table-database mutual import tool
LPQ(局部相位量化)学习笔记
力扣-相同的树
01 【前言 基础使用 核心概念】