#include <windows.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
// 全局颜色句柄
HANDLE hConsole;
// 设置颜色函数
void setColor(int color) {
SetConsoleTextAttribute(hConsole, color);
}
// 清屏函数
void clearScreen() {
system("cls");
}
// 显示标题
void showTitle() {
clearScreen();
setColor(14); // 黄色
cout << "========================================\n";
cout << " 数字侦探大冒险 \n";
cout << "========================================\n\n";
setColor(7); // 白色
}
// 游戏主程序
void numberDetectiveGame() {
srand(time(0));
int secretNumber = rand() % 100 + 1; // 1-100的随机数
int guess;
int attempts = 0;
bool won = false;
showTitle();
// 欢迎弹窗
MessageBox(NULL, "欢迎来到数字侦探大冒险!\n\n你的任务是找到神秘数字!\n它就在1-100之间!",
"欢迎", MB_OK | MB_ICONINFORMATION);
setColor(11); // 浅蓝色
cout << "侦探提示:数字范围是 1-100\n";
cout << "使用线索缩小范围!\n\n";
while(!won) {
setColor(7); // 白色
cout << "请输入你的猜测: ";
cin >> guess;
attempts++;
if(cin.fail()) {
cin.clear();
cin.ignore();
setColor(12); // 红色
cout << "请输入有效数字!\n";
continue;
}
if(guess < 1 || guess > 100) {
setColor(12); // 红色
cout << "数字必须在1-100之间!\n";
continue;
}
if(guess < secretNumber) {
setColor(14); // 黄色
cout << "太小了!继续寻找...\n";
// 线索弹窗(每3次猜测一次)
if(attempts % 3 == 0) {
int low = secretNumber - 10;
int high = secretNumber + 10;
char clue[100];
sprintf(clue, "侦探线索:\n目标数字在 %d 和 %d 之间",
(low < 1 ? 1 : low),
(high > 100 ? 100 : high));
MessageBox(NULL, clue, "侦探线索", MB_OK | MB_ICONQUESTION);
}
}
else if(guess > secretNumber) {
setColor(10); // 绿色
cout << "太大了!缩小范围...\n";
}
else {
// 猜对了!
won = true;
// 清除屏幕,显示胜利画面
clearScreen();
setColor(10); // 绿色
cout << "╔════════════════════════════════════════╗\n";
cout << "║ ★★★ 恭喜! ★★★ ║\n";
cout << "║ ║\n";
cout << "║ 你找到了神秘数字: " << secretNumber << " ║\n";
cout << "║ ║\n";
cout << "║ 总猜测次数: " << attempts << " ║\n";
cout << "║ ║\n";
setColor(14); // 黄色
cout << "║ 你是个优秀的数字侦探! ║\n";
cout << "╚════════════════════════════════════════╝\n\n";
// 根据表现显示不同消息
char victoryMsg[200];
string title;
if(attempts <= 5) {
sprintf(victoryMsg, "太厉害了!\n你只用了 %d 次就找到了数字!\n\n你是天才侦探!", attempts);
title = "天才侦探";
setColor(13); // 紫色
cout << "成就:天才侦探 ★★★★★\n";
}
else if(attempts <= 10) {
sprintf(victoryMsg, "做得好!\n你用了 %d 次找到数字!\n\n你是个出色的侦探!", attempts);
title = "优秀侦探";
setColor(10); // 绿色
cout << "成就:优秀侦探 ★★★★☆\n";
}
else if(attempts <= 15) {
sprintf(victoryMsg, "不错!\n你用了 %d 次找到数字!\n\n继续努力!", attempts);
title = "合格侦探";
setColor(14); // 黄色
cout << "成就:合格侦探 ★★★☆☆\n";
}
else {
sprintf(victoryMsg, "找到了!\n你用了 %d 次找到数字!\n\n下次会更好!", attempts);
title = "实习侦探";
setColor(11); // 浅蓝色
cout << "成就:实习侦探 ★★☆☆☆\n";
}
// 胜利弹窗
MessageBox(NULL, victoryMsg, title.c_str(), MB_OK | MB_ICONINFORMATION);
// 询问是否再玩一次
int playAgain = MessageBox(NULL, "想再玩一次吗?", "再来一次?",
MB_YESNO | MB_ICONQUESTION);
if(playAgain == IDYES) {
numberDetectiveGame(); // 重新开始游戏
}
}
}
}
int main() {
// 初始化
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
setColor(7); // 白色
// 开始游戏
numberDetectiveGame();
// 结束消息
setColor(11);
cout << "\n感谢游玩数字侦探大冒险!\n";
system("pause");
return 0;
}