#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime> // For time()
using namespace std;
int main() {
int bomb = rand() % 100 + 1; // 生成1到100之间的随机数
int minVal = 0, maxVal = 101;//0到101,实际只能存储1到100
int guess;
int playerHealth = 1;
cout << "欢迎来到数字炸弹游戏!\n"
<< "炸弹已经设定好,现在开始猜数...\n";
while (playerHealth > 0 && maxVal - minVal > 2) {
cout << "\n请在 " << minVal + 1 << " 和 " << maxVal - 1 << " 之间猜一个数字:";
cin >> guess;
if (guess == bomb) {
--playerHealth;
cout << "很不幸,你猜中了炸弹!";
break;
} else if (guess < bomb && guess > minVal) {
minVal = guess;
cout << "安全,炸弹不在这个值以下。\n";
} else if (guess > bomb && guess < maxVal) {
maxVal = guess;
cout << "安全,炸弹不在这个值以上。\n";
} else {
cout << "无效的猜测,请不要选择范围边界上的值。\n";
continue;
}
if (maxVal - minVal <= 2) {
cout << "范围已经非常小了,只能直接选择剩余的一个数字,所以最后一个数字是炸弹。\n";
}
}
return 0;
}