小游戏2
wyb666999
·
·
科技·工程
* 三国群英录 · 真实战斗版
* 版本:1.3
*
* 编译指令:g++ -std=c++11 -o sanguo sanguo.cpp
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <limits>
using namespace std;
// ============ 武将结构体 ============
struct General {
string name;
int leadership;
int force;
int intelligence;
int politics;
int loyalty;
int hp;
int max_hp;
int attack;
int defense;
bool is_alive;
string skill;
General() {
name = "";
leadership = 50;
force = 50;
intelligence = 50;
politics = 50;
loyalty = 80;
hp = 100;
max_hp = 100;
attack = 10;
defense = 5;
is_alive = true;
skill = "无";
}
General(string n, int l, int f, int i, int p, string s = "无") {
name = n;
leadership = l;
force = f;
intelligence = i;
politics = p;
loyalty = 80;
hp = 100;
max_hp = 100;
attack = 10 + f / 10;
defense = 5 + l / 20;
is_alive = true;
skill = s;
}
};
// ============ 城池结构体 ============
struct City {
string name;
int population;
int food;
int gold;
int army;
int defense_level;
int commerce_level;
int agriculture_level;
vector<General> generals;
string owner;
City() {
name = "";
population = 10000;
food = 1000;
gold = 500;
army = 500;
defense_level = 1;
commerce_level = 1;
agriculture_level = 1;
owner = "";
}
City(string n, string o) {
name = n;
population = 10000 + rand() % 10000;
food = 1000 + rand() % 1000;
gold = 500 + rand() % 500;
army = 500 + rand() % 500;
defense_level = 1;
commerce_level = 1;
agriculture_level = 1;
owner = o;
}
};
// ============ 势力结构体 ============
struct Faction {
string name;
string ruler;
vector<General> generals;
vector<City> cities;
int total_army;
int total_population;
bool is_alive;
Faction() {
name = "";
ruler = "";
total_army = 0;
total_population = 0;
is_alive = true;
}
};
// ============ 游戏状态 ============
struct Game {
vector<Faction> factions;
vector<General> all_generals;
int current_faction;
int turn;
int year;
bool game_over;
Game() {
current_faction = 0;
turn = 1;
year = 184;
game_over = false;
}
};
// ============ 函数声明 ============
void clearScreen();
void waitForEnter();
void showTitle();
void showMainMenu();
void initGame(Game& game);
void initGenerals(Game& game);
void initFactions(Game& game);
void initCities(Game& game);
void showStatus(const Game& game);
void showFactionInfo(const Game& game, int idx);
void showGeneralInfo(const General& g);
void showCityInfo(const City& c);
void gameLoop(Game& game);
void doRecruit(Game& game);
void doDevelop(Game& game);
void doAttack(Game& game);
void doTrain(Game& game);
void doTrade(Game& game);
void doRest(Game& game);
bool battle(Faction& attacker, Faction& defender, City& target_city);
void generalDuel(Faction& attacker, Faction& defender);
int randomRange(int min, int max);
int getTotalArmy(Faction& f);
// ============ 实现 ============
void clearScreen() {
cout << "\033[2J\033[1;1H";
}
void waitForEnter() {
cout << "\n按 Enter 继续...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
}
int randomRange(int min, int max) {
return rand() % (max - min + 1) + min;
}
int getTotalArmy(Faction& f) {
int total = 0;
for (size_t i = 0; i < f.cities.size(); i++) {
total += f.cities[i].army;
}
return total;
}
// ============ 显示标题 ============
void showTitle() {
clearScreen();
cout << " +===========================================================+" << endl;
cout << " | |" << endl;
cout << " | ██████ 三 国 群 英 录 ██████ |" << endl;
cout << " | |" << endl;
cout << " | \"滚滚长江东逝水,浪花淘尽英雄\" |" << endl;
cout << " | |" << endl;
cout << " | 版本 1.3 |" << endl;
cout << " | |" << endl;
cout << " +===========================================================+" << endl;
cout << endl;
}
// ============ 初始化武将 ============
void initGenerals(Game& game) {
game.all_generals.push_back(General("曹操", 95, 72, 91, 94, "奸雄"));
game.all_generals.push_back(General("夏侯惇", 89, 92, 65, 70, "拔矢啖睛"));
game.all_generals.push_back(General("张辽", 93, 90, 78, 75, "威震逍遥津"));
game.all_generals.push_back(General("许褚", 75, 96, 45, 40, "虎痴"));
game.all_generals.push_back(General("典韦", 70, 98, 35, 30, "古之恶来"));
game.all_generals.push_back(General("司马懿", 96, 63, 97, 93, "狼顾"));
game.all_generals.push_back(General("刘备", 78, 75, 74, 78, "仁德"));
game.all_generals.push_back(General("关羽", 95, 97, 75, 68, "武圣"));
game.all_generals.push_back(General("张飞", 85, 98, 45, 38, "万人敌"));
game.all_generals.push_back(General("诸葛亮", 92, 38, 100, 98, "卧龙"));
game.all_generals.push_back(General("赵云", 91, 96, 76, 65, "常山赵子龙"));
game.all_generals.push_back(General("马超", 88, 97, 54, 48, "锦马超"));
game.all_generals.push_back(General("孙权", 76, 65, 80, 88, "紫髯"));
game.all_generals.push_back(General("周瑜", 92, 71, 96, 86, "美周郎"));
game.all_generals.push_back(General("陆逊", 90, 68, 95, 90, "火攻"));
game.all_generals.push_back(General("甘宁", 86, 94, 56, 50, "锦帆贼"));
game.all_generals.push_back(General("太史慈", 84, 93, 62, 58, "守信"));
game.all_generals.push_back(General("吕蒙", 85, 82, 84, 78, "刮目相看"));
game.all_generals.push_back(General("吕布", 80, 100, 40, 35, "飞将"));
game.all_generals.push_back(General("貂蝉", 40, 25, 85, 70, "闭月"));
game.all_generals.push_back(General("华雄", 70, 92, 35, 30, "关西"));
game.all_generals.push_back(General("颜良", 72, 94, 38, 32, "河北"));
game.all_generals.push_back(General("文丑", 70, 93, 36, 30, "河北"));
game.all_generals.push_back(General("张角", 85, 42, 90, 60, "天公将军"));
}
// ============ 初始化势力 ============
void initFactions(Game& game) {
Faction wei;
wei.name = "魏";
wei.ruler = "曹操";
wei.is_alive = true;
game.factions.push_back(wei);
Faction shu;
shu.name = "蜀";
shu.ruler = "刘备";
shu.is_alive = true;
game.factions.push_back(shu);
Faction wu;
wu.name = "吴";
wu.ruler = "孙权";
wu.is_alive = true;
game.factions.push_back(wu);
Faction other;
other.name = "群雄";
other.ruler = "吕布";
other.is_alive = true;
game.factions.push_back(other);
}
// ============ 初始化城池 ============
void initCities(Game& game) {
City c1("洛阳", "魏");
c1.population = 50000;
c1.food = 5000;
c1.gold = 3000;
c1.army = 3000;
game.factions[0].cities.push_back(c1);
City c2("邺城", "魏");
c2.population = 30000;
c2.food = 3000;
c2.gold = 2000;
c2.army = 2000;
game.factions[0].cities.push_back(c2);
City c3("许昌", "魏");
c3.population = 25000;
c3.food = 2500;
c3.gold = 1500;
c3.army = 1500;
game.factions[0].cities.push_back(c3);
City c4("成都", "蜀");
c4.population = 40000;
c4.food = 4000;
c4.gold = 2500;
c4.army = 2500;
game.factions[1].cities.push_back(c4);
City c5("汉中", "蜀");
c5.population = 20000;
c5.food = 2000;
c5.gold = 1200;
c5.army = 1500;
game.factions[1].cities.push_back(c5);
City c6("建业", "吴");
c6.population = 35000;
c6.food = 3500;
c6.gold = 2800;
c6.army = 2500;
game.factions[2].cities.push_back(c6);
City c7("柴桑", "吴");
c7.population = 20000;
c7.food = 2000;
c7.gold = 1500;
c7.army = 1500;
game.factions[2].cities.push_back(c7);
City c8("长安", "群雄");
c8.population = 30000;
c8.food = 2500;
c8.gold = 2000;
c8.army = 2000;
game.factions[3].cities.push_back(c8);
}
// ============ 初始化游戏 ============
void initGame(Game& game) {
game.turn = 1;
game.year = 184;
game.game_over = false;
srand(time(NULL));
initGenerals(game);
initFactions(game);
initCities(game);
vector<General>& all = game.all_generals;
game.factions[0].generals.push_back(all[0]);
game.factions[0].generals.push_back(all[1]);
game.factions[0].generals.push_back(all[2]);
game.factions[0].generals.push_back(all[3]);
game.factions[0].generals.push_back(all[4]);
game.factions[0].generals.push_back(all[5]);
game.factions[1].generals.push_back(all[6]);
game.factions[1].generals.push_back(all[7]);
game.factions[1].generals.push_back(all[8]);
game.factions[1].generals.push_back(all[9]);
game.factions[1].generals.push_back(all[10]);
game.factions[1].generals.push_back(all[11]);
game.factions[2].generals.push_back(all[12]);
game.factions[2].generals.push_back(all[13]);
game.factions[2].generals.push_back(all[14]);
game.factions[2].generals.push_back(all[15]);
game.factions[2].generals.push_back(all[16]);
game.factions[2].generals.push_back(all[17]);
game.factions[3].generals.push_back(all[18]);
game.factions[3].generals.push_back(all[19]);
game.factions[3].generals.push_back(all[20]);
game.factions[3].generals.push_back(all[21]);
game.factions[3].generals.push_back(all[22]);
game.factions[3].generals.push_back(all[23]);
}
// ============ 显示武将信息 ============
void showGeneralInfo(const General& g) {
cout << " " << g.name << " | ";
cout << "统:" << g.leadership << " 武:" << g.force;
cout << " 智:" << g.intelligence << " 政:" << g.politics;
cout << " 忠:" << g.loyalty << " 技:" << g.skill;
cout << " HP:" << g.hp << "/" << g.max_hp;
if (!g.is_alive) cout << " [已阵亡]";
cout << endl;
}
// ============ 显示城池信息 ============
void showCityInfo(const City& c) {
cout << " " << c.name << " | 人口:" << c.population;
cout << " 粮:" << c.food << " 金:" << c.gold;
cout << " 兵:" << c.army << " 城防:" << c.defense_level;
cout << " 商业:" << c.commerce_level << " 农业:" << c.agriculture_level;
cout << endl;
}
// ============ 显示势力信息 ============
void showFactionInfo(const Game& game, int idx) {
const Faction& f = game.factions[idx];
cout << "+===========================================================+" << endl;
cout << "| " << f.name << "国" << " 君主:" << f.ruler;
cout << " 城池:" << f.cities.size() << "座";
cout << " 武将:" << f.generals.size() << "人" << endl;
int total_army = 0;
int total_pop = 0;
int total_gold = 0;
int total_food = 0;
for (size_t i = 0; i < f.cities.size(); i++) {
total_army += f.cities[i].army;
total_pop += f.cities[i].population;
total_gold += f.cities[i].gold;
total_food += f.cities[i].food;
}
cout << "| 总兵力:" << total_army << " 总人口:" << total_pop << endl;
cout << "| 总金钱:" << total_gold << " 总粮食:" << total_food << endl;
cout << "+-----------------------------------------------------------+" << endl;
cout << "【武将列表】" << endl;
for (size_t i = 0; i < f.generals.size(); i++) {
if (f.generals[i].is_alive) {
showGeneralInfo(f.generals[i]);
}
}
cout << "\n【城池列表】" << endl;
for (size_t i = 0; i < f.cities.size(); i++) {
showCityInfo(f.cities[i]);
}
cout << "+===========================================================+" << endl;
}
// ============ 显示游戏状态 ============
void showStatus(const Game& game) {
clearScreen();
cout << "+===========================================================+" << endl;
cout << "| |" << endl;
cout << "| 第 " << game.turn << " 回合 公元 " << game.year << " 年" << endl;
cout << "| 当前势力: " << game.factions[game.current_faction].name << "国" << endl;
cout << "| 君主: " << game.factions[game.current_faction].ruler << endl;
cout << "| |" << endl;
cout << "+===========================================================+" << endl;
showFactionInfo(game, game.current_faction);
}
// ============ 招募武将 ============
void doRecruit(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【招募武将】" << endl;
cout << "花费: 500金" << endl;
if (f.cities.empty()) {
cout << "没有城池,无法招募!" << endl;
waitForEnter();
return;
}
City& capital = f.cities[0];
if (capital.gold < 500) {
cout << "金钱不足!需要500金。" << endl;
waitForEnter();
return;
}
vector<int> available;
for (size_t i = 0; i < game.all_generals.size(); i++) {
bool already_hired = false;
for (size_t j = 0; j < f.generals.size(); j++) {
if (f.generals[j].name == game.all_generals[i].name) {
already_hired = true;
break;
}
}
if (!already_hired) {
for (size_t k = 0; k < game.factions.size(); k++) {
if (k == (size_t)game.current_faction) continue;
for (size_t j = 0; j < game.factions[k].generals.size(); j++) {
if (game.factions[k].generals[j].name == game.all_generals[i].name) {
already_hired = true;
break;
}
}
if (already_hired) break;
}
}
if (!already_hired) {
available.push_back(i);
}
}
if (available.empty()) {
cout << "没有可招募的武将了。" << endl;
waitForEnter();
return;
}
int idx = available[randomRange(0, available.size() - 1)];
General new_gen = game.all_generals[idx];
new_gen.loyalty = randomRange(50, 80);
capital.gold -= 500;
f.generals.push_back(new_gen);
cout << "成功招募 " << new_gen.name << "!" << endl;
cout << " " << new_gen.name << " 忠诚度: " << new_gen.loyalty << endl;
waitForEnter();
}
// ============ 发展城池 ============
void doDevelop(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【发展城池】" << endl;
if (f.cities.empty()) {
cout << "没有城池!" << endl;
waitForEnter();
return;
}
cout << "选择要发展的城池:" << endl;
for (size_t i = 0; i < f.cities.size(); i++) {
cout << " [" << i+1 << "] " << f.cities[i].name;
cout << " (人口:" << f.cities[i].population << " 金:" << f.cities[i].gold;
cout << " 粮:" << f.cities[i].food << ")" << endl;
}
int choice;
cout << "输入编号 (0返回): ";
cin >> choice;
if (choice <= 0 || choice > (int)f.cities.size()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
City& city = f.cities[choice-1];
cout << "\n选择发展方式:" << endl;
cout << " [1] 发展农业 (消耗200金,粮食产量提升)" << endl;
cout << " [2] 发展商业 (消耗300金,金钱收入提升)" << endl;
cout << " [3] 招募士兵 (消耗100金,兵力+100)" << endl;
cout << " [4] 加固城防 (消耗400金,城防+1)" << endl;
int action;
cout << "输入编号 (0返回): ";
cin >> action;
if (action < 0 || action > 4) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "取消操作。" << endl;
waitForEnter();
return;
}
switch(action) {
case 1: {
if (city.gold < 200) {
cout << "金钱不足!需要200金。" << endl;
} else {
city.gold -= 200;
city.agriculture_level++;
city.food += 200 * city.agriculture_level;
city.population += 500;
cout << "农业发展成功!" << endl;
cout << " 农业等级: " << city.agriculture_level << endl;
cout << " 粮食+" << 200 * city.agriculture_level << endl;
cout << " 人口+500" << endl;
}
break;
}
case 2: {
if (city.gold < 300) {
cout << "金钱不足!需要300金。" << endl;
} else {
city.gold -= 300;
city.commerce_level++;
int income = 100 * city.commerce_level;
city.gold += income;
cout << "商业发展成功!" << endl;
cout << " 商业等级: " << city.commerce_level << endl;
cout << " 立即获得收入: " << income << "金" << endl;
}
break;
}
case 3: {
if (city.gold < 100) {
cout << "金钱不足!需要100金。" << endl;
} else {
city.gold -= 100;
city.army += 100;
cout << "招募士兵成功!兵力+100" << endl;
}
break;
}
case 4: {
if (city.gold < 400) {
cout << "金钱不足!需要400金。" << endl;
} else {
city.gold -= 400;
city.defense_level++;
cout << "城防加固成功!城防等级: " << city.defense_level << endl;
}
break;
}
default:
cout << "取消操作。" << endl;
}
waitForEnter();
}
// ============ 训练士兵 ============
void doTrain(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【训练士兵】" << endl;
cout << "训练士兵需要武将带队。" << endl;
if (f.generals.empty()) {
cout << "没有武将!" << endl;
waitForEnter();
return;
}
cout << "选择带队武将:" << endl;
for (size_t i = 0; i < f.generals.size(); i++) {
if (f.generals[i].is_alive) {
cout << " [" << i+1 << "] " << f.generals[i].name;
cout << " (统率:" << f.generals[i].leadership << ")" << endl;
}
}
int choice;
cout << "输入编号 (0返回): ";
cin >> choice;
if (choice <= 0 || choice > (int)f.generals.size()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
General& gen = f.generals[choice-1];
if (!gen.is_alive) {
cout << "该武将已阵亡!" << endl;
waitForEnter();
return;
}
if (f.cities.empty()) {
cout << "没有城池!" << endl;
waitForEnter();
return;
}
City& city = f.cities[0];
if (city.gold < 200) {
cout << "金钱不足!需要200金。" << endl;
waitForEnter();
return;
}
city.gold -= 200;
int train_effect = gen.leadership / 10 + randomRange(1, 5);
city.army += train_effect * 10;
cout << "训练成功!" << endl;
cout << gen.name << "率领训练,兵力+" << train_effect * 10 << endl;
waitForEnter();
}
// ============ 交易 ============
void doTrade(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【交易】" << endl;
if (f.cities.empty()) {
cout << "没有城池!" << endl;
waitForEnter();
return;
}
City& city = f.cities[0];
cout << "当前资源:粮=" << city.food << " 金=" << city.gold << endl;
cout << " [1] 买粮 (10金换20粮)" << endl;
cout << " [2] 卖粮 (20粮换10金)" << endl;
int choice;
cout << "输入编号 (0返回): ";
cin >> choice;
if (choice < 0 || choice > 2) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
if (choice == 1) {
int amount;
cout << "输入购买数量 (10金/次): ";
cin >> amount;
if (amount <= 0) {
cout << "数量无效。" << endl;
} else {
int cost = amount * 10;
if (city.gold < cost) {
cout << "金钱不足!需要 " << cost << " 金。" << endl;
} else {
city.gold -= cost;
city.food += amount * 20;
cout << "购买成功!粮食+" << amount * 20 << endl;
}
}
} else if (choice == 2) {
int amount;
cout << "输入出售数量 (20粮/次): ";
cin >> amount;
if (amount <= 0) {
cout << "数量无效。" << endl;
} else {
int need = amount * 20;
if (city.food < need) {
cout << "粮食不足!需要 " << need << " 粮。" << endl;
} else {
city.food -= need;
city.gold += amount * 10;
cout << "出售成功!金钱+" << amount * 10 << endl;
}
}
}
waitForEnter();
}
// ============ 休整 ============
void doRest(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【休整】" << endl;
for (size_t i = 0; i < f.generals.size(); i++) {
if (f.generals[i].is_alive && f.generals[i].hp < f.generals[i].max_hp) {
f.generals[i].hp += 20;
if (f.generals[i].hp > f.generals[i].max_hp) {
f.generals[i].hp = f.generals[i].max_hp;
}
}
}
for (size_t i = 0; i < f.cities.size(); i++) {
f.cities[i].food += 100;
f.cities[i].population += 100;
int income = 50 * f.cities[i].commerce_level;
f.cities[i].gold += income;
int food_prod = 50 * f.cities[i].agriculture_level;
f.cities[i].food += food_prod;
}
cout << "休整完成!" << endl;
cout << " - 武将生命恢复20" << endl;
cout << " - 城池粮食+100,人口+100" << endl;
cout << " - 商业和农业产出增加" << endl;
waitForEnter();
}
// ============ 武将单挑 ============
void generalDuel(Faction& attacker, Faction& defender) {
cout << "\n【武将单挑】" << endl;
cout << "========================================" << endl;
vector<General> atk_gens;
vector<General> def_gens;
for (size_t i = 0; i < attacker.generals.size(); i++) {
if (attacker.generals[i].is_alive) {
atk_gens.push_back(attacker.generals[i]);
}
}
for (size_t i = 0; i < defender.generals.size(); i++) {
if (defender.generals[i].is_alive) {
def_gens.push_back(defender.generals[i]);
}
}
if (atk_gens.empty() || def_gens.empty()) {
cout << "一方没有武将,跳过单挑。" << endl;
return;
}
int rounds = min(atk_gens.size(), def_gens.size());
rounds = min(rounds, 5); // 最多打5场
for (int r = 0; r < rounds; r++) {
General& atk = atk_gens[r];
General& def = def_gens[r];
cout << "\n第" << (r+1) << "场: " << atk.name << " VS " << def.name << endl;
cout << " " << atk.name << " HP:" << atk.hp << " 攻:" << atk.attack;
cout << " " << def.name << " HP:" << def.hp << " 攻:" << def.attack << endl;
int atk_hp = atk.hp;
int def_hp = def.hp;
// 单挑循环(最多10回合)
for (int t = 0; t < 10 && atk_hp > 0 && def_hp > 0; t++) {
int atk_damage = atk.attack + randomRange(1, 10);
def_hp -= atk_damage;
cout << " " << atk.name << " 造成 " << atk_damage << " 点伤害";
if (def_hp <= 0) {
cout << " -> " << def.name << " 战败!" << endl;
def.is_alive = false;
break;
}
int def_damage = def.attack + randomRange(1, 10);
atk_hp -= def_damage;
cout << ", " << def.name << " 反击 " << def_damage << " 点";
if (atk_hp <= 0) {
cout << " -> " << atk.name << " 战败!" << endl;
atk.is_alive = false;
break;
}
cout << endl;
}
// 更新HP
if (atk_hp > 0) atk.hp = atk_hp;
else atk.is_alive = false;
if (def_hp > 0) def.hp = def_hp;
else def.is_alive = false;
cout << " 结果: " << atk.name << (atk.is_alive ? " 存活" : " 阵亡");
cout << ", " << def.name << (def.is_alive ? " 存活" : " 阵亡") << endl;
}
}
// ============ 战斗系统(真实版) ============
bool battle(Faction& attacker, Faction& defender, City& target_city) {
cout << "\n【攻城战】" << endl;
cout << "========================================" << endl;
cout << attacker.name << "国 攻打 " << defender.name << "国 " << target_city.name << endl;
cout << "========================================" << endl;
// 获取双方总兵力
int atk_total = getTotalArmy(attacker);
int def_total = target_city.army;
cout << "攻击方总兵力: " << atk_total << endl;
cout << "防守方兵力: " << def_total << endl;
cout << "城防等级: " << target_city.defense_level << endl;
cout << "攻击方武将: " << attacker.generals.size() << "人" << endl;
cout << "防守方武将: " << defender.generals.size() << "人" << endl;
cout << "========================================" << endl;
// 计算基础战力
int atk_power = atk_total * 2;
int def_power = def_total * 2 + target_city.defense_level * 300;
// 武将加成
for (size_t i = 0; i < attacker.generals.size(); i++) {
if (attacker.generals[i].is_alive) {
atk_power += attacker.generals[i].leadership + attacker.generals[i].force;
}
}
for (size_t i = 0; i < defender.generals.size(); i++) {
if (defender.generals[i].is_alive) {
def_power += defender.generals[i].leadership + defender.generals[i].force;
}
}
// 防守方主场优势
def_power += 200;
// 攻击方攻城惩罚
atk_power = atk_power * 80 / 100;
cout << "攻击方战力: " << atk_power << endl;
cout << "防守方战力: " << def_power << endl;
cout << "========================================" << endl;
// 计算胜率
int total_power = atk_power + def_power;
int win_rate = (atk_power * 100) / total_power;
cout << "攻击方胜率: " << win_rate << "%" << endl;
cout << "防守方胜率: " << (100 - win_rate) << "%" << endl;
cout << "========================================" << endl;
// 随机判定
int dice = randomRange(1, 100);
bool victory = (dice <= win_rate);
cout << "掷骰结果: " << dice << endl;
// 计算双方损失(不管胜负,双方都会损失兵力)
int atk_loss_ratio, def_loss_ratio;
if (victory) {
// 攻击方胜利,损失较小
atk_loss_ratio = randomRange(15, 35);
def_loss_ratio = randomRange(50, 80);
cout << "\n" << attacker.name << "国 攻城成功!" << endl;
} else {
// 攻击方失败,损失较大
atk_loss_ratio = randomRange(35, 60);
def_loss_ratio = randomRange(10, 30);
cout << "\n" << attacker.name << "国 攻城失败!" << endl;
}
// 执行兵力损失
cout << "\n【战损统计】" << endl;
cout << "攻击方损失: " << atk_loss_ratio << "%" << endl;
cout << "防守方损失: " << def_loss_ratio << "%" << endl;
// 攻击方所有城池按比例损失兵力
for (size_t i = 0; i < attacker.cities.size(); i++) {
int loss = attacker.cities[i].army * atk_loss_ratio / 100;
attacker.cities[i].army -= loss;
if (attacker.cities[i].army < 0) attacker.cities[i].army = 0;
}
// 防守方目标城池损失兵力
int def_loss = target_city.army * def_loss_ratio / 100;
target_city.army -= def_loss;
if (target_city.army < 0) target_city.army = 0;
cout << "攻击方剩余总兵力: " << getTotalArmy(attacker) << endl;
cout << "防守方剩余兵力: " << target_city.army << endl;
// 武将单挑
generalDuel(attacker, defender);
if (victory) {
// 占领城池
cout << "\n【占领城池】" << endl;
target_city.owner = attacker.name;
// 从防守方移除城池
for (size_t i = 0; i < defender.cities.size(); i++) {
if (defender.cities[i].name == target_city.name) {
defender.cities.erase(defender.cities.begin() + i);
break;
}
}
// 投降的武将
cout << "\n【武将去向】" << endl;
for (size_t i = 0; i < defender.generals.size(); i++) {
if (defender.generals[i].is_alive) {
int chance = randomRange(0, 100);
if (chance < 40) {
cout << defender.generals[i].name << " 投降了!" << endl;
defender.generals[i].loyalty = 30;
attacker.generals.push_back(defender.generals[i]);
} else if (chance < 70) {
cout << defender.generals[i].name << " 逃走了。" << endl;
} else {
cout << defender.generals[i].name << " 被俘虏了。" << endl;
defender.generals[i].loyalty = 20;
attacker.generals.push_back(defender.generals[i]);
}
}
}
// 清除已阵亡武将
vector<General> new_gens;
for (size_t i = 0; i < defender.generals.size(); i++) {
if (defender.generals[i].is_alive) {
new_gens.push_back(defender.generals[i]);
}
}
defender.generals = new_gens;
// 将城池加入攻击方
attacker.cities.push_back(target_city);
cout << "\n成功占领 " << target_city.name << "!" << endl;
return true;
} else {
cout << "\n进攻失败,撤退回城..." << endl;
// 攻击方武将伤亡
for (size_t i = 0; i < attacker.generals.size(); i++) {
if (attacker.generals[i].is_alive && randomRange(0, 100) < 20) {
attacker.generals[i].hp -= 30;
if (attacker.generals[i].hp <= 0) {
attacker.generals[i].is_alive = false;
cout << attacker.generals[i].name << " 阵亡!" << endl;
}
}
}
return false;
}
}
// ============ 攻击 ============
void doAttack(Game& game) {
Faction& f = game.factions[game.current_faction];
cout << "\n【出征】" << endl;
int total_army = getTotalArmy(f);
if (total_army < 500) {
cout << "兵力不足!需要至少500兵力。当前: " << total_army << endl;
waitForEnter();
return;
}
cout << "当前总兵力: " << total_army << endl;
cout << "\n选择攻击目标:" << endl;
vector< pair<int, int> > targets;
for (size_t i = 0; i < game.factions.size(); i++) {
if (i == (size_t)game.current_faction) continue;
if (!game.factions[i].is_alive) continue;
for (size_t j = 0; j < game.factions[i].cities.size(); j++) {
cout << " [" << targets.size()+1 << "] ";
cout << game.factions[i].cities[j].name;
cout << " (" << game.factions[i].name << "国)";
cout << " 兵力:" << game.factions[i].cities[j].army;
cout << " 城防:" << game.factions[i].cities[j].defense_level;
cout << " 武将:" << game.factions[i].generals.size() << "人";
cout << endl;
targets.push_back(make_pair(i, j));
}
}
if (targets.empty()) {
cout << "没有可攻击的目标!" << endl;
waitForEnter();
return;
}
int choice;
cout << "输入编号 (0返回): ";
cin >> choice;
if (choice <= 0 || choice > (int)targets.size()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return;
}
int target_faction = targets[choice-1].first;
int target_city = targets[choice-1].second;
Faction& defender = game.factions[target_faction];
City& city = defender.cities[target_city];
cout << "\n确定要攻打 " << city.name << " 吗? (y/n): ";
char confirm;
cin >> confirm;
if (confirm != 'y' && confirm != 'Y') {
cout << "取消出征。" << endl;
waitForEnter();
return;
}
if (battle(f, defender, city)) {
if (defender.cities.empty()) {
defender.is_alive = false;
cout << "\n" << defender.name << "国灭亡!" << endl;
}
}
waitForEnter();
}
// ============ 游戏主循环 ============
void gameLoop(Game& game) {
while (!game.game_over) {
showStatus(game);
int alive = 0;
for (size_t i = 0; i < game.factions.size(); i++) {
if (game.factions[i].is_alive) alive++;
}
if (alive <= 1) {
cout << "\n+===========================================================+" << endl;
cout << " " << game.factions[game.current_faction].name << "国统一天下!" << endl;
cout << " 用了 " << game.turn << " 回合" << endl;
cout << "+===========================================================+" << endl;
game.game_over = true;
break;
}
cout << "\n【行动选项】" << endl;
cout << " [1] 招募武将" << endl;
cout << " [2] 发展城池" << endl;
cout << " [3] 出征" << endl;
cout << " [4] 训练士兵" << endl;
cout << " [5] 交易" << endl;
cout << " [6] 休整" << endl;
cout << " [7] 查看所有势力" << endl;
cout << " [0] 结束回合" << endl;
int choice;
cout << "\n输入选择: ";
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入!" << endl;
waitForEnter();
continue;
}
switch(choice) {
case 1: doRecruit(game); break;
case 2: doDevelop(game); break;
case 3: doAttack(game); break;
case 4: doTrain(game); break;
case 5: doTrade(game); break;
case 6: doRest(game); break;
case 7: {
for (size_t i = 0; i < game.factions.size(); i++) {
if (game.factions[i].is_alive) {
showFactionInfo(game, i);
}
}
waitForEnter();
break;
}
case 0: {
game.turn++;
game.year++;
// AI发展
for (size_t i = 0; i < game.factions.size(); i++) {
if (i == (size_t)game.current_faction) continue;
if (!game.factions[i].is_alive) continue;
if (!game.factions[i].cities.empty()) {
City& c = game.factions[i].cities[0];
if (c.gold > 400) {
c.gold -= 150;
c.army += 80;
c.food += 100;
c.population += 200;
if (c.defense_level < 5 && randomRange(0, 100) < 30) {
c.defense_level++;
}
}
}
}
cout << "\n进入第 " << game.turn << " 回合" << endl;
waitForEnter();
break;
}
default:
cout << "无效选择!" << endl;
waitForEnter();
}
}
}
// ============ 主菜单 ============
void showMainMenu() {
cout << " +===========================================================+" << endl;
cout << " | |" << endl;
cout << " | 主 菜 单 |" << endl;
cout << " | |" << endl;
cout << " | [1] 开始游戏 |" << endl;
cout << " | [2] 游戏说明 |" << endl;
cout << " | [3] 退出游戏 |" << endl;
cout << " | |" << endl;
cout << " +===========================================================+" << endl;
}
// ============ 主函数 ============
int main() {
srand(time(NULL));
showTitle();
bool exit_game = false;
while (!exit_game) {
showMainMenu();
int choice;
cout << "\n请输入选择: ";
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入!" << endl;
continue;
}
switch(choice) {
case 1: {
Game game;
initGame(game);
cout << "\n选择势力:" << endl;
for (size_t i = 0; i < game.factions.size(); i++) {
cout << " [" << i+1 << "] " << game.factions[i].name << "国 (" << game.factions[i].ruler << ")" << endl;
}
int sel;
cout << "输入编号: ";
cin >> sel;
if (sel >= 1 && sel <= (int)game.factions.size()) {
game.current_faction = sel - 1;
gameLoop(game);
}
break;
}
case 2: {
clearScreen();
cout << " +===========================================================+" << endl;
cout << " | |" << endl;
cout << " | 游戏说明: |" << endl;
cout << " | 1. 选择势力,开始你的统一大业 |" << endl;
cout << " | 2. 招募武将,每个武将都有独特属性 |" << endl;
cout << " | 3. 发展城池,增加人口、粮食、金钱和兵力 |" << endl;
cout << " | 4. 出征攻打其他势力,占领城池 |" << endl;
cout << " | 5. 统一所有势力即为胜利 |" << endl;
cout << " | 6. 武将会在战斗中阵亡,请谨慎使用 |" << endl;
cout << " | 7. 每次战斗双方都会损失兵力! |" << endl;
cout << " | 8. 攻城前查看敌我对比,避免盲目进攻 |" << endl;
cout << " | |" << endl;
cout << " | 战斗系统: |" << endl;
cout << " | - 兵力、武将、城防影响战力 |" << endl;
cout << " | - 防守方有城防和主场加成 |" << endl;
cout << " | - 攻击方有攻城惩罚 |" << endl;
cout << " | - 胜负由战力对比和随机因素决定 |" << endl;
cout << " | - 胜者损失15-35%,败者损失35-60%兵力 |" << endl;
cout << " | - 武将会进行单挑,可能阵亡 |" << endl;
cout << " | |" << endl;
cout << " +===========================================================+" << endl;
waitForEnter();
break;
}
case 3:
cout << "感谢游玩,再见!" << endl;
exit_game = true;
break;
default:
cout << "无效选择!" << endl;
waitForEnter();
}
}
return 0;
}