145 lines
3.7 KiB
C++
145 lines
3.7 KiB
C++
//
|
|
// Created by xiaolinquan on 2024/12/21.
|
|
//
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
#include <cmath>
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
using namespace std;
|
|
int randNum;
|
|
|
|
// 设置控制台文本颜色
|
|
void setConsoleColor(int color) {
|
|
#ifdef _WIN32
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
|
|
#else
|
|
cout << "\033[" << color << "m";
|
|
#endif
|
|
}
|
|
|
|
// 恢复控制台文本颜色
|
|
void resetConsoleColor() {
|
|
#ifdef _WIN32
|
|
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
|
|
#else
|
|
cout << "\033[0m";
|
|
#endif
|
|
}
|
|
|
|
// 奖励机制函数
|
|
void calculateReward(int attempts, int &smallSnacks) {
|
|
if (attempts <= 1){
|
|
smallSnacks = 10;
|
|
}else if (attempts <= 2) {
|
|
smallSnacks = 7;
|
|
}else if (attempts <= 4) {
|
|
smallSnacks = 5;
|
|
} else if (attempts <= 7) {
|
|
smallSnacks = 2;
|
|
} else {
|
|
smallSnacks = 1;
|
|
}
|
|
}
|
|
|
|
// 提示机制函数
|
|
void giveHint(int guessNum, int randNum) {
|
|
int difference = abs(guessNum - randNum);
|
|
if (difference == 0) return;
|
|
|
|
// 1. 基础提示
|
|
setConsoleColor(14);
|
|
if (difference <= 5) cout << "非常非常接近了!!" << endl;
|
|
else if (difference <= 10) cout << "接近了!" << endl;
|
|
else if (difference <= 25) cout << "差距适中。" << endl;
|
|
else cout << "!差距很大哦!" << endl;
|
|
|
|
// 2. 随机触发一种额外提示 (抽取 0-5)
|
|
int hintType = rand() % 10;
|
|
|
|
switch (hintType) {
|
|
case 0: // 奇偶性
|
|
if (randNum % 2 == 0) cout << "线索:它是偶数。" << endl;
|
|
else cout << "线索:它是奇数。" << endl;
|
|
break;
|
|
case 1: // 各位数字之和
|
|
cout << "线索:各位数字之和是 " << (randNum/10 + randNum%10) << endl;
|
|
break;
|
|
case 2: // 尾数 (你的原逻辑)
|
|
cout << "线索:最后一位数字是 " << randNum % 10 << endl;
|
|
break;
|
|
case 3: // 倍数
|
|
if (randNum % 3 == 0) cout << "线索:是 3 的倍数。" << endl;
|
|
else if (randNum % 5 == 0) cout << "线索:是 5 的倍数。" << endl;
|
|
else cout << "线索:不是 3 也不是 5 的倍数。" << endl;
|
|
break;
|
|
case 4:
|
|
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
|
break;
|
|
case 5: // 什么都不提示
|
|
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
|
break;
|
|
default:
|
|
cout << "这次没有额外线索,靠直觉吧!" << endl;
|
|
|
|
}
|
|
|
|
resetConsoleColor();
|
|
}
|
|
|
|
int main() {
|
|
|
|
srand((unsigned int)time(nullptr));
|
|
int round = 1;
|
|
|
|
while (true) {
|
|
randNum = rand() % 100 + 1;
|
|
int guessNum = -1;
|
|
int attempts = 0;
|
|
|
|
setConsoleColor(11); // 设置颜色为亮青色
|
|
cout << "开始第" << round << "轮竞猜!" << endl;
|
|
resetConsoleColor();
|
|
|
|
while (guessNum != randNum) {
|
|
cout << "请输入你要猜的数字(范围1-100): ";
|
|
cin >> guessNum;
|
|
attempts++;
|
|
|
|
if (guessNum > randNum) {
|
|
setConsoleColor(12); // 红色
|
|
cout << "你猜得太大了!" << endl;
|
|
resetConsoleColor();
|
|
} else if (guessNum < randNum) {
|
|
setConsoleColor(12); // 红色
|
|
cout << "你猜得太小了!" << endl;
|
|
resetConsoleColor();
|
|
}
|
|
|
|
// 提示玩家
|
|
giveHint(guessNum, randNum);
|
|
|
|
if (guessNum == randNum) {
|
|
setConsoleColor(10); // 绿色
|
|
cout << "你猜中了!!恭喜!" << endl;
|
|
resetConsoleColor();
|
|
|
|
int Snacks = 0;
|
|
calculateReward(attempts,Snacks);
|
|
|
|
setConsoleColor(13); // 紫色
|
|
cout << "你获得了 " << Snacks << " 个零食!" << endl;
|
|
resetConsoleColor();
|
|
}
|
|
}
|
|
|
|
round++;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|