C++// ============================================================
// 智斗游戏:猜平均数 x 0.8
// C++98 兼容版(请把本文件另存为 GBK / ANSI 编码!)
// ------------------------------------------------------------
// 功能:
// - 主菜单:观战模式 / 正式比赛 / 退出
// - 每轮前可以修改 AI 的思维层级和名字(名字不能重复)
// - AI 之间有隐藏对话,可能是真话也可能是诱导
// - 每轮结束后可以偷看幕后对话
// - 每轮结束后按回车继续下一轮
// ============================================================
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <limits>
#include <sstream>
using namespace std;
// ---------------------- 工具函数 ----------------------
int randInt(int lo, int hi) {
if (hi <= lo) return lo;
return lo + rand() % (hi - lo + 1);
}
double randDouble(double lo, double hi) {
return lo + (hi - lo) * ((double)rand() / (double)RAND_MAX);
}
long myRound(double x) {
if (x >= 0.0) return (long)(x + 0.5);
return (long)(x - 0.5);
}
string intToStr(int x) {
if (x == 0) return "0";
bool neg = (x < 0);
if (neg) x = -x;
string s;
while (x > 0) {
s = (char)('0' + x % 10) + s;
x /= 10;
}
if (neg) s = "-" + s;
return s;
}
string doubleToStr(double x, int prec) {
ostringstream oss;
oss << fixed << setprecision(prec) << x;
return oss.str();
}
// GBK 下中文按 2 格宽计算
int dispWidth(const string& s) {
int w = 0;
size_t i = 0;
while (i < s.size()) {
unsigned char c = (unsigned char)s[i];
if (c < 0x80) { w += 1; i += 1; }
else { w += 2; i += 2; }
}
return w;
}
string padRight(const string& s, int width) {
int w = dispWidth(s);
if (w >= width) return s;
return s + string(width - w, ' ');
}
int readInt(const string& prompt, int lo, int hi) {
while (true) {
cout << prompt;
cout.flush();
string line;
if (!getline(cin, line)) return lo;
istringstream iss(line);
int v;
if (iss >> v) {
char extra;
if (!(iss >> extra) && v >= lo && v <= hi) return v;
}
cout << " [错误] 输入无效,请输入 " << lo << " ~ " << hi << " 之间的整数。\n";
}
}
string readLine(const string& prompt) {
cout << prompt;
cout.flush();
string line;
if (!getline(cin, line)) return "";
size_t a = 0, b = line.size();
while (a < b && (line[a] == ' ' || line[a] == '\t')) a++;
while (b > a && (line[b-1] == ' ' || line[b-1] == '\t')) b--;
return line.substr(a, b - a);
}
void waitForEnter(const string& tip) {
if (!tip.empty()) {
cout << "\n>>> " << tip;
cout.flush();
}
string line;
getline(cin, line);
}
// ---------------------- 默认名字池 ----------------------
const char* NAME_POOL[] = {
"老王", "小李", "阿强", "张麻子", "赵四",
"钱五", "孙七", "周八", "吴九", "郑十",
"冯大", "陈二", "褚三", "卫四", "蒋五",
"沈六", "韩七", "杨八", "朱九", "秦十"
};
const int NAME_POOL_SIZE = 20;
// ---------------------- 玩家结构体 ----------------------
struct Player {
string name;
bool isAI;
int score;
int choice;
bool disqualified;
bool scoredThisRound;
int level;
double bias;
double noiseAmp;
bool isLiar; // 是不是"骗子"型 AI
Player()
: isAI(false), score(0), choice(-1),
disqualified(false), scoredThisRound(false),
level(1), bias(0.0), noiseAmp(0.0), isLiar(false)
{}
};
// ---------------------- AI 决策 ----------------------
int aiChoose(const Player& p, int round, double lastAvg) {
double expectedAvg = (round == 1) ? 50.0 : lastAvg;
double target = expectedAvg;
for (int i = 0; i < p.level; i++) target *= 0.8;
double guess = target + p.bias + randDouble(-p.noiseAmp, p.noiseAmp);
if (guess < 0.0) guess = 0.0;
if (guess > 100.0) guess = 100.0;
return (int)myRound(guess);
}
// 不带随机抖动的"理想出价",用来生成对话内容
int aiIdealChoice(const Player& p, int round, double lastAvg) {
double expectedAvg = (round == 1) ? 50.0 : lastAvg;
double target = expectedAvg;
for (int i = 0; i < p.level; i++) target *= 0.8;
double guess = target + p.bias;
if (guess < 0.0) guess = 0.0;
if (guess > 100.0) guess = 100.0;
return (int)myRound(guess);
}
// ---------------------- 排序比较器 ----------------------
struct ScoreCmp {
const vector<Player>* players;
ScoreCmp(const vector<Player>* p) : players(p) {}
bool operator()(int a, int b) const {
const vector<Player>& ps = *players;
if (ps[a].score != ps[b].score) return ps[a].score > ps[b].score;
return a < b;
}
};
// ---------------------- 名字唯一性检查 ----------------------
bool nameExists(const vector<Player>& players, const string& name, int exceptIdx) {
for (size_t i = 0; i < players.size(); i++) {
if ((int)i == exceptIdx) continue;
if (players[i].name == name) return true;
}
return false;
}
// ---------------------- 生成 AI 之间的幕后对话 ----------------------
vector<pair<string, string> > generateDialogue(
const vector<Player>& players, double nextAvgGuess, int nextRound)
{
vector<pair<string, string> > lines;
for (size_t i = 0; i < players.size(); i++) {
// 60% 概率说话
if (randInt(1, 100) > 60) continue;
const Player& p = players[i];
int realGuess = aiIdealChoice(p, nextRound, nextAvgGuess);
string text;
if (p.isLiar) {
// 骗子:说假话,诱导别人撞车
int fake;
int mode = randInt(0, 2);
if (mode == 0) {
// 引导大家报"第一层"的数,自己偷偷报更深的
double t = nextAvgGuess * 0.8;
fake = (int)myRound(t);
} else if (mode == 1) {
// 说一个离自己真实出价很远的数
fake = (realGuess + 20) % 100;
} else {
// 故意报一个热门区间
fake = 30 + randInt(0, 20);
}
if (fake < 0) fake = 0;
if (fake > 100) fake = 100;
int style = randInt(0, 3);
if (style == 0)
text = "我觉得下轮报 " + intToStr(fake) + " 附近最稳。";
else if (style == 1)
text = "大家一起报 " + intToStr(fake) + " 吧,这个数我看好。";
else if (style == 2)
text = "听说好几个高手下轮都会报 " + intToStr(fake) + "。";
else
text = "别想太多,直接报 " + intToStr(fake) + " 就完了。";
} else {
// 诚实:说真话(大致会报什么数)
int style = randInt(0, 3);
if (style == 0)
text = "我打算下轮报 " + intToStr(realGuess) + " 左右。";
else if (style == 1)
text = "按我的判断,下轮瞄准 " + intToStr(realGuess) + " 比较合适。";
else if (style == 2)
text = "我下轮估计会出 " + intToStr(realGuess) + " 附近。";
else
text = "我准备瞄着 " + intToStr(realGuess) + " 出手。";
}
lines.push_back(make_pair(players[i].name, text));
}
return lines;
}
// ---------------------- 改名菜单 ----------------------
void renameMenu(vector<Player>& players) {
while (true) {
cout << "\n----------------------------------------\n";
cout << " 修 改 A I 名 字\n";
cout << "----------------------------------------\n";
cout << " 0. 返回\n";
for (size_t i = 0; i < players.size(); i++) {
cout << " " << (i + 1) << ". " << players[i].name;
if (players[i].isAI && players[i].isLiar) cout << " (骗子型)";
cout << "\n";
}
cout << "----------------------------------------\n";
int aiCount = (int)players.size();
int idx = readInt("请选择要改名的编号 (0 返回): ", 0, aiCount);
if (idx == 0) return;
int realIdx = idx - 1;
while (true) {
string newName = readLine("请输入新名字: ");
if (newName.empty()) {
cout << " [错误] 名字不能为空。\n";
continue;
}
if (nameExists(players, newName, realIdx)) {
cout << " [错误] 名字 \"" << newName
<< "\" 已被使用,请换一个。\n";
continue;
}
players[realIdx].name = newName;
cout << ">>> 已改名为 \"" << newName << "\"。\n";
break;
}
}
}
// ---------------------- 战前菜单 ----------------------
void preRoundMenu(vector<Player>& players) {
while (true) {
cout << "\n----------------------------------------\n";
cout << " 战前准备菜单(可以临时调整 AI)\n";
cout << "----------------------------------------\n";
cout << " 1. 查看所有 AI 的思维层级和性格\n";
cout << " 2. 修改某一个 AI 的思维层级\n";
cout << " 3. 把所有 AI 的思维层级统一改成同一个值\n";
cout << " 4. 修改 AI 名字(不能重复)\n";
cout << " 5. 不调整了,开始本轮出价\n";
cout << "----------------------------------------\n";
int menuChoice = readInt("请选择 (1-5): ", 1, 5);
if (menuChoice == 5) {
cout << "\n>>> 好,开始本轮出价。\n";
return;
}
if (menuChoice == 1) {
cout << "\n--- 当前所有 AI 的思维模式 ---\n";
cout << "----------------------------------------------------------------\n";
cout << padRight("名字", 14) << padRight("层级", 8)
<< padRight("偏移", 10) << padRight("抖动", 12) << "性格\n";
cout << "----------------------------------------------------------------\n";
for (size_t i = 1; i < players.size(); i++) {
cout << padRight(players[i].name, 14)
<< padRight(intToStr(players[i].level), 8)
<< padRight(doubleToStr(players[i].bias, 1), 10)
<< padRight("正负 " + doubleToStr(players[i].noiseAmp, 1), 12)
<< (players[i].isLiar ? "骗子(会说假话)" : "诚实(会说真话)")
<< "\n";
}
cout << "----------------------------------------------------------------\n";
continue;
}
if (menuChoice == 2) {
int aiCount = (int)players.size() - 1;
int idx = readInt("请输入要修改的 AI 编号 (1 ~ " +
intToStr(aiCount) + "): ", 1, aiCount);
int lv = readInt("请输入新的思维层级 (1 ~ 5): ", 1, 5);
players[idx].level = lv;
cout << ">>> 已把 " << players[idx].name
<< " 的思维层级改为 " << lv << "。\n";
continue;
}
if (menuChoice == 3) {
int lv = readInt("请输入所有 AI 的新思维层级 (1 ~ 5): ", 1, 5);
for (size_t i = 1; i < players.size(); i++) {
players[i].level = lv;
}
cout << ">>> 已把所有 AI 的思维层级统一设为 " << lv << "。\n";
continue;
}
if (menuChoice == 4) {
// 只允许改 AI(跳过 players[0] 玩家自己)
vector<Player> aisOnly;
for (size_t i = 1; i < players.size(); i++) aisOnly.push_back(players[i]);
renameMenu(aisOnly);
for (size_t i = 1; i < players.size(); i++) players[i] = aisOnly[i - 1];
continue;
}
}
}
// ---------------------- 判分 + 战报 + 偷听对话 ----------------------
double judgeAndReport(vector<Player>& players,
int round, int totalRounds,
const string& modeTag,
bool waitAfter,
bool allowPeek)
{
double sum = 0.0;
for (size_t i = 0; i < players.size(); i++) sum += players[i].choice;
double avg = sum / (double)players.size();
double target = avg * 0.8;
map<int, int> cnt;
for (size_t i = 0; i < players.size(); i++) cnt[players[i].choice]++;
for (size_t i = 0; i < players.size(); i++) {
players[i].disqualified = (cnt[players[i].choice] >= 2);
players[i].scoredThisRound = false;
}
bool anyValid = false;
for (size_t i = 0; i < players.size(); i++) {
if (!players[i].disqualified) { anyValid = true; break; }
}
vector<int> candidates;
if (anyValid) {
for (size_t i = 0; i < players.size(); i++) {
if (!players[i].disqualified) candidates.push_back((int)i);
}
} else {
for (size_t i = 0; i < players.size(); i++) {
candidates.push_back((int)i);
}
}
double bestDist = numeric_limits<double>::max();
for (size_t k = 0; k < candidates.size(); k++) {
int idx = candidates[k];
double d = fabs((double)players[idx].choice - target);
if (d < bestDist) bestDist = d;
}
for (size_t k = 0; k < candidates.size(); k++) {
int idx = candidates[k];
double d = fabs((double)players[idx].choice - target);
if (d <= bestDist + 1e-9) {
players[idx].score++;
players[idx].scoredThisRound = true;
}
}
// ---- 战报 ----
cout << "\n--- [" << modeTag << "] 第 " << round << " 轮 出价一览 ---\n";
cout << "------------------------------------------------\n";
cout << padRight("玩家", 14) << padRight("出价", 8) << "本轮状态\n";
cout << "------------------------------------------------\n";
for (size_t i = 0; i < players.size(); i++) {
const Player& p = players[i];
string status;
if (p.scoredThisRound) status = "[赢] 最接近目标, 得 1 分";
else if (p.disqualified) status = "[撞] 同数字撞车, 取消资格";
else status = "[陪] 陪跑";
cout << padRight(p.name, 14)
<< padRight(intToStr(p.choice), 8)
<< status << "\n";
}
cout << "------------------------------------------------\n";
cout << "本轮报数总和 = " << doubleToStr(sum, 2) << "\n";
cout << "本轮参赛人数 = " << (int)players.size() << "\n";
cout << "本轮平均数 A = " << doubleToStr(avg, 2) << "\n";
cout << "目标值 T = 0.8A = " << doubleToStr(target, 2) << "\n";
{
vector<string> dqList;
for (size_t i = 0; i < players.size(); i++) {
if (players[i].disqualified) {
dqList.push_back(players[i].name + "(" +
intToStr(players[i].choice) + ")");
}
}
if (!dqList.empty()) {
cout << "被取消资格的人 = ";
for (size_t i = 0; i < dqList.size(); i++) {
if (i) cout << ", ";
cout << dqList[i];
}
cout << "\n";
}
}
if (!anyValid) {
cout << ">>> 注意: 本轮所有人都撞车了!\n";
cout << ">>> 因此改为在全体中重新比较, 选离目标值最近的人。\n";
}
{
cout << "本轮 +1 分的人 = ";
bool any = false;
for (size_t i = 0; i < players.size(); i++) {
if (players[i].scoredThisRound) {
cout << players[i].name << " ";
any = true;
}
}
if (!any) cout << "(无)";
cout << "\n";
}
// ---- 当前积分榜 ----
cout << "\n--- [" << modeTag << "] 当前积分榜 ---\n";
vector<int> order;
for (size_t i = 0; i < players.size(); i++) order.push_back((int)i);
ScoreCmp cmp(&players);
sort(order.begin(), order.end(), cmp);
for (size_t k = 0; k < order.size(); k++) {
int idx = order[k];
cout << " #" << (int)(k + 1) << " "
<< padRight(players[idx].name, 14)
<< " " << players[idx].score << " 分\n";
}
// ---- 偷看对话 ----
if (allowPeek && round < totalRounds) {
cout << "\n(你隐约听到一些窃窃私语...)\n";
cout << " 1. 偷偷去听 (查看 AI 之间的幕后对话)\n";
cout << " 2. 不理会, 直接进入下一轮\n";
int c = readInt("请选择 (1-2): ", 1, 2);
if (c == 1) {
vector<pair<string, string> > lines =
generateDialogue(players, avg, round + 1);
cout << "\n--- 幕后窃窃私语(真假难辨)---\n";
cout << "------------------------------------------------\n";
if (lines.empty()) {
cout << "(这一轮大家都沉默着,没人开口)\n";
} else {
for (size_t i = 0; i < lines.size(); i++) {
cout << " " << padRight(lines[i].first, 14)
<< "说: " << lines[i].second << "\n";
}
}
cout << "------------------------------------------------\n";
cout << "(提示: 有人可能在说真话, 有人可能在故意误导。\n";
cout << " 至于他们下轮到底报几, 你自己判断。)\n";
}
}
if (waitAfter) {
if (round < totalRounds) {
waitForEnter("按回车继续...");
} else {
waitForEnter("这是最后一轮, 按回车查看最终排名...");
}
}
return avg;
}
// ---------------------- 观战模式 ----------------------
void runSpectatorMode() {
cout << "\n";
cout << "==============================================================\n";
cout << " 观 战 模 式\n";
cout << "==============================================================\n";
cout << " 你将以旁观者身份, 观看 AI 之间的智斗。\n";
cout << " 每轮结束后可以偷看 AI 之间的幕后对话。\n";
cout << " 每轮结束后按回车继续下一轮。\n\n";
int aiCount = readInt("请输入参战 AI 数量 (2 ~ 20): ", 2, 20);
int rounds = readInt("请输入观看轮数 (1 ~ 50): ", 1, 50);
vector<Player> ais;
for (int i = 1; i <= aiCount; i++) {
Player ai;
ai.isAI = true;
if (i <= NAME_POOL_SIZE) ai.name = NAME_POOL[i - 1];
else ai.name = "AI-" + intToStr(i);
ai.level = randInt(1, 3);
ai.bias = randDouble(-8.0, 8.0);
ai.noiseAmp = randDouble(2.0, 6.0);
ai.isLiar = (randInt(1, 100) <= 30); // 30% 是骗子
ais.push_back(ai);
}
cout << "\n--- 参战 AI 一览 ---\n";
cout << "------------------------------------------------\n";
for (size_t i = 0; i < ais.size(); i++) {
string lvlDesc;
if (ais[i].level == 1) lvlDesc = "新手";
else if (ais[i].level == 2) lvlDesc = "老手";
else lvlDesc = "高手";
cout << padRight(ais[i].name, 14)
<< " 层级=" << ais[i].level
<< " 偏移=" << (ais[i].bias >= 0 ? "+" : "")
<< doubleToStr(ais[i].bias, 1)
<< " 抖动=正负" << doubleToStr(ais[i].noiseAmp, 1)
<< " (" << lvlDesc << ")\n";
}
cout << "------------------------------------------------\n";
// 可选改名
{
cout << "\n想给 AI 改名字吗?\n";
cout << " 1. 进入改名菜单(可改多个)\n";
cout << " 2. 直接开始观战\n";
int c = readInt("请选择 (1-2): ", 1, 2);
if (c == 1) renameMenu(ais);
}
double lastAvg = 50.0;
for (int round = 1; round <= rounds; round++) {
cout << "\n";
cout << "==============================================================\n";
cout << " [观战] 第 " << round << " 轮 / 共 "
<< rounds << " 轮\n";
cout << "==============================================================\n";
for (size_t i = 0; i < ais.size(); i++) {
ais[i].choice = aiChoose(ais[i], round, lastAvg);
}
lastAvg = judgeAndReport(ais, round, rounds, "观战", true, true);
}
cout << "\n\n";
cout << "==============================================================\n";
cout << " [观战] 比 赛 结 束\n";
cout << "==============================================================\n";
cout << "\n[最终排名]\n";
cout << "------------------------------------------------\n";
cout << padRight("名次", 8) << padRight("AI 名字", 16) << "得分\n";
cout << "------------------------------------------------\n";
vector<int> order;
for (size_t i = 0; i < ais.size(); i++) order.push_back((int)i);
ScoreCmp cmp(&ais);
sort(order.begin(), order.end(), cmp);
int rank = 1;
for (size_t k = 0; k < order.size(); k++) {
int idx = order[k];
if (k > 0 && ais[order[k]].score != ais[order[k - 1]].score) {
rank = (int)k + 1;
}
cout << padRight("#" + intToStr(rank), 8)
<< padRight(ais[idx].name, 16)
<< ais[idx].score << " 分\n";
}
cout << "------------------------------------------------\n";
waitForEnter("按回车返回主菜单...");
}
// ---------------------- 正式比赛 ----------------------
void runMainGame() {
cout << "\n";
cout << "==============================================================\n";
cout << " 正 式 比 赛\n";
cout << "==============================================================\n";
cout << "[规则说明]\n";
cout << " 1. 每轮每人报一个 0~100 的整数(包括你自己)。\n";
cout << " 2. 目标值 T = 所有人平均数 x 0.8。\n";
cout << " 3. 最接近 T 的人 +1 分。\n";
cout << " 4. 同一数字被 2 人以上报出, 全部取消资格。\n";
cout << " 5. 全体撞车时, 全体重新比较。\n";
cout << " 6. 距离并列时, 并列者都 +1 分。\n";
cout << " 7. 每轮开始前可以调整 AI 的层级和名字。\n";
cout << " 8. 每轮结束后可以偷看 AI 之间的幕后对话。\n\n";
int n = readInt("请输入总人数 n (包含你自己, 2 ~ 20): ", 2, 20);
int m = readInt("请输入比赛轮数 m (1 ~ 50): ", 1, 50);
vector<Player> players;
Player me;
me.name = "你";
me.isAI = false;
players.push_back(me);
cout << "\n[本局对手一览]\n";
cout << "--------------------------------------------------------------\n";
for (int i = 1; i < n; i++) {
Player ai;
ai.isAI = true;
if (i <= NAME_POOL_SIZE) ai.name = NAME_POOL[i - 1];
else ai.name = "AI-" + intToStr(i);
ai.level = randInt(1, 3);
ai.bias = randDouble(-8.0, 8.0);
ai.noiseAmp = randDouble(2.0, 6.0);
ai.isLiar = (randInt(1, 100) <= 30);
players.push_back(ai);
string lvlDesc;
if (ai.level == 1) lvlDesc = "新手";
else if (ai.level == 2) lvlDesc = "老手";
else lvlDesc = "高手";
cout << padRight(ai.name, 14)
<< " 层级=" << ai.level
<< " 偏移=" << (ai.bias >= 0 ? "+" : "")
<< doubleToStr(ai.bias, 1)
<< " 抖动=正负" << doubleToStr(ai.noiseAmp, 1)
<< " (" << lvlDesc << ")\n";
}
cout << "--------------------------------------------------------------\n";
double lastAvg = 50.0;
for (int round = 1; round <= m; round++) {
cout << "\n";
cout << "==============================================================\n";
cout << " [正式] 第 " << round << " 轮 / 共 "
<< m << " 轮\n";
cout << "==============================================================\n";
preRoundMenu(players);
int myChoice = readInt("轮到你出价了 (请输入 0~100 的整数): ", 0, 100);
players[0].choice = myChoice;
for (size_t i = 1; i < players.size(); i++) {
players[i].choice = aiChoose(players[i], round, lastAvg);
}
lastAvg = judgeAndReport(players, round, m, "正式", true, true);
}
cout << "\n\n";
cout << "==============================================================\n";
cout << " 比 赛 结 束\n";
cout << "==============================================================\n";
cout << "\n[最终排名]\n";
cout << "------------------------------------------------\n";
cout << padRight("名次", 8) << padRight("玩家", 16) << "得分\n";
cout << "------------------------------------------------\n";
vector<int> order;
for (size_t i = 0; i < players.size(); i++) order.push_back((int)i);
ScoreCmp cmp(&players);
sort(order.begin(), order.end(), cmp);
int rank = 1;
for (size_t k = 0; k < order.size(); k++) {
int idx = order[k];
if (k > 0 && players[order[k]].score != players[order[k-1]].score) {
rank = (int)k + 1;
}
cout << padRight("#" + intToStr(rank), 8)
<< padRight(players[idx].name, 16)
<< players[idx].score << " 分\n";
}
cout << "------------------------------------------------\n";
{
int myScore = players[0].score;
cout << "\n你的最终得分是 " << myScore << " 分, ";
if (order[0] == 0) {
cout << "恭喜你拿下了第一名!\n";
} else if (players[order[0]].score == myScore) {
cout << "你和别人并列第一!\n";
} else {
cout << "再接再厉, 下次试试报得更反直觉一点?\n";
}
}
waitForEnter("按回车返回主菜单...");
}
// ---------------------- 主程序 ----------------------
int main() {
srand((unsigned)time(0));
cout << "============================================================\n";
cout << " 智斗游戏: 猜平均数 x 0.8\n";
cout << "============================================================\n";
while (true) {
cout << "\n";
cout << "============ 主 菜 单 ============\n";
cout << " 1. 观战模式(看 AI 互斗, 你只当观众)\n";
cout << " 2. 正式比赛(你亲自下场和 AI 过招)\n";
cout << " 3. 退出\n";
cout << "==================================\n";
int choice = readInt("请选择 (1-3): ", 1, 3);
if (choice == 1) {
runSpectatorMode();
} else if (choice == 2) {
runMainGame();
} else {
cout << "\n再见!\n";
break;
}
}
return 0;
}