#include<bits/stdc++.h>
#include<chrono>
#include<thread>
#include<windows.h>
using namespace std;
int main(){
system("color 1B");
cout<<"yhxyd2594制作";
Sleep(500);
system("cls");
srand((unsigned int)time(NULL));
bool playAgain=true;
int totalScore=0;
while (playAgain){
long long a1,a2,a,x,i=0,y;
auto seed=chrono::high_resolution_clock::now().time_since_epoch().count();
srand(static_cast<unsigned int>(seed));
cout<<"========== 数字炸弹小游戏 ==========" << endl;
cout<<"游戏规则:猜测数字,猜中即为踩中炸弹!" << endl;
cout<<"=====================================" << endl;
cout<<"选择难度 (1, 2, 3...):";
/*
谁敢100
*/
while(1){
if(cin>>y){
if(y>0)break;
}
cout<<"输入无效,请输入正整数难度: ";
cin.clear();
cin.ignore(10000,'\n');
}
a1=0;
a2=y*100;
if(a2<=1)a2=2;
a=1+rand()%(a2-1);
cout<<"\n游戏开始!目标数字已在 ("<<a1<<" - "<<a2<< ") 之间生成。"<<endl;
auto start_time = chrono::high_resolution_clock::now();
while(true){
cout<<"\n当前可猜范围: ("<<a1<<" - "<<a2<< ")"<<endl;
cout<<"请输入猜测数字: ";
if(!(cin >> x)){
cout<<">>输入无效,请输入数字!"<<endl;
cin.clear();
cin.ignore(10000,'\n');
continue;
}
if(x<=a1||x>=a2){
cout<<">> 输入无效!数字必须在 "<<a1<<" 和 "<<a2<<" 之间 (不含边界)。"<<endl;
continue;
}
i++;
if(x==a) {
auto end_time=chrono::high_resolution_clock::now();
auto duration=chrono::duration_cast<chrono::milliseconds>(end_time - start_time);
double seconds=duration.count()/1000.0;
cout<<"\n 猜对了!你踩中炸弹了!"<<endl;
cout<<"--------------------------------"<<endl;
cout<<"总共猜了: "<<i<<" 次"<<endl;
cout<<"耗时: "<<fixed<<setprecision(2)<<seconds<<"秒"<<endl;
cout << "--------------------------------" << endl;
long long score=(a2/10)/i;
if(score==0)score=1;
if(seconds<10.0){
cout<<">> 获得额外时间奖励!"<<endl;
score+=10;
}
cout<<"本局得分: "<<score<<endl;
totalScore+=score;
break;
}
else if(x<a){
cout<<">> 太小了!"<<endl;
a1=x;
}
else{
cout<<">> 太大了!"<<endl;
a2=x;
}
}
cout<<"====================================="<<endl;
cout<<"当前总分为: "<<totalScore<<endl;
char choice;
cout<<"\n想再来一局吗?(y/n): ";
cin>>choice;
if(choice!='y'&&choice!='Y'){
playAgain=false;
cout<<"感谢游玩!你的最终总分是: "<<totalScore<<endl;
}
this_thread::sleep_for(chrono::milliseconds(500));
}
}
//代码有几个中/英文感叹号?
此话题已在最后回复的 15 天后被自动关闭。不再允许新回复。