跳到正文

剪贴板

剪贴板 w33y4j88

原文
剪贴板信息

剪贴板信息

收录
更新

数字炸弹!

C++
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int guess,low,high;
int random(int small,int big){ //随机数
	srand(time(0));
	return small+rand()%big;
}
void game(){ //游戏主程序
	guess=0;
	cout<<"Range:";
	cin>>low>>high;
	int bomb=random(low,high);
	while (guess!=bomb){
		cout<<"Guess:";
		cin>>guess;
		if (guess>high||guess<low) //范围外
			break;
		if (guess<bomb){ //比炸弹小
			low=guess+1;
			cout<<low<<'~'<<high;
		}
		else if (guess>bomb){ //比炸弹大
			high=guess-1;
			cout<<low<<'~'<<high;
		}
		cout<<'\n';
	}
	cout<<"Boom!\n";
}
void rules(){ //介绍游戏规则
	cout<<"游戏规则:输入猜测数字的范围。接着,生成一个随机数作为炸弹。根据猜测的数字调整范围,并输出当前的猜测范围。如果猜中或不在范围内,炸弹爆炸。一般多人游玩。\n";
}
int main(){
	rules();
	while (1){ //游戏过程
		char start;
		cout<<"Start?(y/n)";
		cin>>start;
		if (start=='y'){ //开始游戏
			system("cls");
			game();
		}
		else if (start=='n') //结束游戏
			break;
		else{ //输入错误
			cout<<"Please enter y or n.\n";
			continue;
		}
	}
	return 0;
}
//printf("h");