数字炸弹儿~
2026-08-18 13:43:40
发布于:广东
#include <windows.h>
#include <bits/stdc++.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
#include <thread>
#include <chrono>
using namespace std;
// 全局变量用于存储游戏状态
int bombNumber;
int currentMin;
int currentMax;
// 初始化游戏范围
void initGameRange() {
currentMin = 1;
currentMax = 100;
srand((unsigned int)time(NULL));
bombNumber = rand() % 100 + 1;
}
// 显示主菜单
void showMenu() {
cout << "========================================" << endl;
cout << " 经典版:数字炸弹 - 多模式 " << endl;
cout << "========================================" << endl;
cout << "请选择游戏模式:" << endl;
cout << "1. 单人挑战模式 (Self-Practice)" << endl;
cout << "2. 人机对战模式 (Vs Computer)" << endl;
cout << "3. 多人轮流模式 (Multiplayer)" << endl;
cout << "0. 退出游戏" << endl;
cout << "========================================" << endl;
cout << "请输入选项: ";
}
// 检查输入是否合法并返回猜测值,非法则重试
int getValidInput(int minR, int maxR) {
int guess;
while (true) {
cout << "请输入 " << minR << " 到 " << maxR << " 之间的数字: ";
if (cin >> guess) {
if (guess >= minR && guess <= maxR) {
return guess;
} else {
cout << "输入错误!数字必须在 " << minR << " 到 " << maxR << " 之间。" << endl;
}
} else {
cin.clear();
cin.ignore(10000, '\n');
cout << "输入无效,请输入整数!" << endl;
}
}
}
// 单人模式逻辑
void playSinglePlayer() {
initGameRange();
cout << "\n--- 单人挑战模式 ---" << endl;
cout << "炸弹已埋设,请尝试找出它!" << endl;
while (true) {
int guess = getValidInput(currentMin, currentMax);
if (guess == bombNumber) {
cout << "\nBOOM! 你猜中了炸弹数字: " << bombNumber << endl;
cout << "游戏结束!" << endl;
break;
} else if (guess > bombNumber) {
cout << "大了!范围缩小为: [" << currentMin << ", " << guess << "]" << endl;
currentMax = guess;
} else {
cout << "小了!范围缩小为: [" << guess << ", " << currentMax << "]" << endl;
currentMin = guess;
}
}
}
// 电脑生成猜测值(简单策略:随机在当前范围内猜测)
int computerGuess(int minR, int maxR) {
// 确保范围有效
if (minR >= maxR) return minR;
return rand() % (maxR - minR + 1) + minR;
}
// 人机对战模式逻辑
void playVsComputer() {
initGameRange();
cout << "\n--- 人机对战模式 ---" << endl;
cout << "你将与电脑轮流猜测,谁踩中炸弹谁输!" << endl;
bool isPlayerTurn = true; // true为玩家,false为电脑
while (true) {
cout << "\n当前范围: [" << currentMin << ", " << currentMax << "]" << endl;
int guess;
if (isPlayerTurn) {
cout << "[玩家回合] ";
guess = getValidInput(currentMin, currentMax);
} else {
cout << "[电脑回合] 思考中..." << endl;
// 模拟思考延迟
#ifdef _WIN32
Sleep(100);
#else
sleep(1);
#endif
guess = computerGuess(currentMin, currentMax);
cout << "电脑猜测的数字是: " << guess << endl;
}
if (guess == bombNumber) {
cout << "\nBOOM! 炸弹数字是: " << bombNumber << endl;
if (isPlayerTurn) {
cout << "很遗憾,你踩中了炸弹!电脑获胜。" << endl;
} else {
cout << "电脑踩中了炸弹!你获胜了!" << endl;
}
break;
} else if (guess > bombNumber) {
cout << (isPlayerTurn ? "你猜大了!" : "电脑猜大了!") << " 范围缩小为: [" << currentMin << ", " << guess << "]" << endl;
currentMax = guess;
} else {
cout << (isPlayerTurn ? "你猜小了!" : "电脑猜小了!") << " 范围缩小为: [" << guess << ", " << currentMax << "]" << endl;
currentMin = guess;
}
// 切换回合
isPlayerTurn = !isPlayerTurn;
}
}
// 多人模式逻辑
void playMultiPlayer() {
initGameRange();
cout << "\n--- 多人轮流模式 ---" << endl;
cout << "请输入玩家人数 (2-10): ";
int playerCount;
while (!(cin >> playerCount) || playerCount < 2 || playerCount > 10) {
cin.clear();
cin.ignore(10000, '\n');
cout << "输入无效,请输入2到10之间的整数: ";
}
vector<string> players(playerCount);
for (int i = 0; i < playerCount; ++i) {
cout << "请输入第 " << (i + 1) << " 位玩家的姓名: ";
cin >> players[i];
}
cout << "\n游戏开始!炸弹已埋设。" << endl;
int currentPlayerIndex = 0;
while (true) {
string& playerName = players[currentPlayerIndex];
cout << "\n当前范围: [" << currentMin << ", " << currentMax << "]" << endl;
cout << "[" << playerName << "] 的回合: ";
int guess = getValidInput(currentMin, currentMax);
if (guess == bombNumber) {
cout << "\nBOOM! 炸弹数字是: " << bombNumber << endl;
cout << playerName << " 踩中了炸弹!游戏结束!" << endl;
// 显示排名(除了输家以外的都是赢家,这里简单处理)
cout << "其他玩家幸存。" << endl;
break;
} else if (guess > bombNumber) {
cout << playerName << " 猜大了!范围缩小为: [" << currentMin << ", " << guess << "]" << endl;
currentMax = guess;
} else {
cout << playerName << " 猜小了!范围缩小为: [" << guess << ", " << currentMax << "]" << endl;
currentMin = guess;
}
// 切换到下一位玩家
currentPlayerIndex = (currentPlayerIndex + 1) % playerCount;
}
}
int main() {
int choice;
do {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
showMenu();
if (!(cin >> choice)) {
cin.clear();
cin.ignore(10000, '\n');
continue;
}
switch (choice) {
case 1:
playSinglePlayer();
break;
case 2:
playVsComputer();
break;
case 3:
playMultiPlayer();
break;
case 0:
cout << "感谢游玩,再见!" << endl;
return 0;
default:
cout << "无效选项,请重新选择。" << endl;
break;
}
cout << "\n按回车键返回主菜单...";
cin.ignore(); // 清除缓冲区残留
cin.get(); // 等待回车
} while (true);
return 0;
}
温馨提示:由于本作者使用的DEV.C++是老版本的,所以在新版本中如果出现报错,敬请谅解
这里空空如也




















有帮助,赞一个