抖音超火的数字炸弹 c++ 实现

本人第一次写文章,如有错误,请见谅!

大家好,今天给大家带来猜数字游戏C++版游戏原理:初始数据范围1-100,后随机生成一个数,如果猜的数在所给范围中,范围就会不断缩小,如果猜中,就会受到惩罚。

(p.s:如果不想要解析,可以直接跳到最后)

写程序最重要的是什么?脑子?不,是基础程序…

#include<bits/stdc++.h>
using namespace std;
int main()
{

    return 0;
}

先定义几个变量:

int ans=17;//目标数字,猜到就输了
int x,y;//最大范围与最小范围
int n;//输入的数

接下来做while循环:

while(1)//无限循环
{
    cout<<"范围:"<<x<<"~"<<y<<endl;//输出目标范围
	cin>>n;//输入猜的数
	if(n<ans&&n>=x&&n<=y) x=n;//当输入比目标小时,输入的数为最小范围
	if(n>ans&&n>=x&&n<=y) y=n;//当输入比目标大时,输入的数为最大范围
	if(n>y) cout<<"输入的数太大,请重新输入:"<<endl;
	if(n<x) cout<<"输入的数太小,请重新输入:"<<endl;
    //输入太大或太小时,报错
    if(n==ans)
	{
		cout<<"you lost!"<<endl;//猜中就输了
		break;//退出循环
	}
}

那么基本上就是这样了,完整程序如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int ans=17,n=0,x=0,y=100;
    while(1)
	{
		cout<<"范围:"<<x<<"~"<<y<<endl;
		cin>>n;
		if(n<ans&&n>=x&&n<=y) x=n;
		if(n>ans&&n>=x&&n<=y) y=n;
		if(n>y) cout<<"输入的数太大,请重新输入:"<<endl;
		if(n<x) cout<<"输入的数太小,请重新输入:"<<endl;
		if(n==ans)
		{
			cout<<"you lost!"<<endl;
			break;
		}
	}
    return 0;
}

但是!还没完!再在前面加亿点点装饰.....

cout<<"--------------------"<<endl;
cout<<"| Guess The Number |"<<endl;
cout<<"--------------------"<<endl;
cout<<"Game Starts!"<<endl;

加一个惩罚......

void chui1()//锤子的图
{
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
}
void chui2()//锤子的图
{
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"******************"<<endl;
	cout<<"******************"<<endl;
	cout<<"******************"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
}
void cartoon()
{
	for(int q=0;q<100;q++)
	{
		system("cls");//清屏
		cout<<"Goodbye,world!"<<endl;
		chui1();
		_sleep(500);//延迟0.5秒
		system("cls");
		cout<<"Goodbye,world!"<<endl;
		chui2();
		_sleep(500);
	}
}

最后把目标调整为随机数:

srand((int)time(0));
int ans=rand()%100;

最终完整代码在这里:

#include <bits/stdc++.h>
using namespace std;
void chui1()
{
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"*************"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
	cout<<"     ***"<<endl;
}
void chui2()
{
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"******************"<<endl;
	cout<<"******************"<<endl;
	cout<<"******************"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
	cout<<"*******"<<endl;
}
void cartoon()
{
	for(int q=0;q<100;q++)
	{
		system("cls");
		cout<<"Goodbye,world!"<<endl;
		chui1();
		_sleep(500);
		system("cls");
		cout<<"Goodbye,world!"<<endl;
		chui2();
		_sleep(500);
	}
}
int main()
{
	srand((int)time(0));
	int ans=rand()%100;
	int i,n=0,x=0,y=100;
	cout<<"--------------------"<<endl;
	cout<<"| Guess The Number |"<<endl;
	cout<<"--------------------"<<endl;
	cout<<"Game Starts!"<<endl;
	while(1)
	{
		cout<<"范围:"<<x<<"~"<<y<<endl;
		cin>>n;
		if(n<ans&&n>=x&&n<=y) x=n;
		if(n>ans&&n>=x&&n<=y) y=n;
		if(n>y) cout<<"输入的数太大,请重新输入:"<<endl;
		if(n<x) cout<<"输入的数太小,请重新输入:"<<endl;
		if(n==ans)
		{
			cartoon();
			break;
		}
	} 
	return 0;
}

运行结果:

如果我猜中了......

今天的教程到这里就结束了,最后不要忘记 点赞,关注,评论哦!

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星河依旧长明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值