2. 猜数字

这是一个简单的猜数字游戏,玩家根据提示在不同难度等级下猜测一个随机数。游戏提供从萌新到最难的九个难度级别,每个级别随机数的范围不同,玩家有5次机会猜出正确数字。若猜错,生命值会减少,生命值耗尽则游戏结束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类型:半转载(转载后进行增减)

转载于:???(不好意思忘记了)

理解难度:3

代码:

#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
void Start();
void GetResults();
int i,j,life,maxrand;
char c;
void Start()
{
	system("color fa");
	int qw;
	i=0;
	j=0;
	life=0;
	maxrand=6;
	
	
	cout<<setw(65)<<"选择难度\n"; // the user has to select a difficutly level
	cout<<setw(73)<<"0 : 萌新专用 (范围0-5)\n";
	cout<<setw(71)<<"1 : 简单 (范围0-15)\n";
	cout<<setw(71)<<"2 : 中等 (范围0-30)\n";
	cout<<setw(71)<<"3 : 困难 (范围0-50)\n";
	cout<<setw(71)<<"4 : 炼狱 (范围0-80)\n";
	cout<<setw(71)<<"5 : 终极 (范围0-100)\n";
	cout<<setw(73)<<"6 : 无人能赢 (范围0-200)\n";
	cout<<setw(76)<<"7 : 无法用语言形容 (范围0-500)\n";
	cout<<setw(74)<<"8 : 惩罚用的 (范围0-1000)\n";
	cout<<setw(73)<<"9 : 最难的 (范围0-5000)\n";
	cout<<setw(87)<<"按1、2、3、4、5、6、7、8、9、0除外的键退出\n\n\n";
	cout<<setw(70)<<"您现在在的地点:选择处\n\n"; 
	c=30;

	cin>>c;                   // read the user's choice
	cout<<"\n";

	switch(c)
	{
		case '0':
			maxrand=5;  // the random number will be between 0 and maxrand
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");
			break;
		case '1':
			maxrand=15;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");
			break;
		case '2':
			maxrand=30;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");
			break;
		case '3':
			maxrand=50;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");
			break;
		case '4':
			maxrand=80; 
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");
			break;
		case '5':
			maxrand=100;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");  
			break;
		case '6':
			maxrand=200; 
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls"); 
			break;
		case '7':
			maxrand=500;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");  
			break;
		case '8':
			maxrand=1000;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");  // the random number will be between 0 and maxrand
			break;
		case '9':
			maxrand=5000;
			system("cls");
			cout<<"加载中…";
			qw=0;
			while(qw<=100000000){
				qw++;
			}
			system("cls");
			cout<<"加载完成!";
			system("cls");  // the random number will be between 0 and maxrand
			break;
		default:
			system("cls");
			cout<<"您现在在的地点:退出处";
			exit(0);
		break;
	}

	life=5;         // number of lifes of the player
	srand((unsigned)time(NULL)); // init Rand() function
	j=rand()%maxrand;  // j get a random value between 0 and maxrand
	
	GetResults();
}

void GetResults()
{
	if(life<=0)
	{ // if player has no more life then he loses
		cout<<"你输了!\n";
		cout<<"正确数字是:"<<j<<"\n";
		Start();
	}

	cout<<"请输入: ";
	cin>>i;
	
	if((i>maxrand)||(i<0))
	{ // if the user number isn't correct, restart
		cout<<"数字输入错误!"<<maxrand;
		GetResults();
	}

	if(i==j)
	{
		cout<<"你赢了!\n\n"; // the user found the secret number
		cout<<"正确数字就是:"<<j<<"\n";
		Start();
	}
	else if(i>j)
	{
		cout<<"太大了\n";
		life=life-1;
		cout<<"剩余机会:"<<life<<"\n\n";
		GetResults();
	}
	else if(i<j)
	{
		cout<<"太小了\n";
		life=life-1;
		cout<<"剩余机会: "<<life<<"\n\n";
		GetResults();
	}
}

int main()
{
	cout<<"** 一个好玩的游戏 **\n";
	cout<<"这个游戏的名字叫猜数字。\n";
	cout<<"你要根据提示猜出数字。\n";
	cout<<"你只有5次机会。\n";
	cout<<"你要根据智商选择你要的等级。\n";
	cout<<"最后一个选项特别难!\n\n";
	Start();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值