小游戏三国群英录

· · 科技·工程

 * 三国群英录 · 文字策略游戏(修复版)
 * 版本:1.1
 * 
 * 编译指令: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);
void doIncome(Game& game);
bool battle(Faction& attacker, Faction& defender, City& target_city);
void battlePhase(Faction& attacker, Faction& defender, vector<General>& atk_gens, vector<General>& def_gens);
int randomRange(int min, int max);

// ============ 实现 ============

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;
}

// ============ 显示标题 ============
void showTitle() {
    clearScreen();
    cout << "    +===========================================================+" << endl;
    cout << "    |                                                           |" << endl;
    cout << "    |              ██████  三  国  群  英  录  ██████           |" << endl;
    cout << "    |                                                           |" << endl;
    cout << "    |          \"滚滚长江东逝水,浪花淘尽英雄\"                   |" << endl;
    cout << "    |                                                           |" << endl;
    cout << "    |                    版本 1.1                                |" << 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;
    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;
                cout << "  后续每回合收入+" << 100 * city.commerce_level << 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 += 10;
            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 << "  - 武将生命恢复" << endl;
    cout << "  - 城池粮食+100,人口+100" << endl;
    cout << "  - 商业和农业产出增加" << endl;
    waitForEnter();
}

// ============ 战斗系统 ============
void battlePhase(Faction& attacker, Faction& defender, 
                  vector<General>& atk_gens, vector<General>& def_gens) {
    cout << "\n【战斗开始!】" << endl;

    while (!atk_gens.empty() && !def_gens.empty()) {
        General& atk = atk_gens[0];
        General& def = def_gens[0];

        cout << atk.name << " VS " << def.name << endl;

        int atk_damage = atk.attack + randomRange(1, 10);
        def.hp -= atk_damage;
        cout << atk.name << " 造成 " << atk_damage << " 点伤害" << endl;

        if (def.hp <= 0) {
            def.is_alive = false;
            cout << def.name << " 阵亡!" << endl;
            def_gens.erase(def_gens.begin());
            if (!def_gens.empty()) continue;
        }

        int def_damage = def.attack + randomRange(1, 10);
        atk.hp -= def_damage;
        cout << def.name << " 反击造成 " << def_damage << " 点伤害" << endl;

        if (atk.hp <= 0) {
            atk.is_alive = false;
            cout << atk.name << " 阵亡!" << endl;
            atk_gens.erase(atk_gens.begin());
            if (!atk_gens.empty()) continue;
        }

        cout << endl;
    }

    if (atk_gens.empty()) {
        cout << "攻击方武将全灭!" << endl;
    } else {
        cout << "防守方武将全灭!" << endl;
    }
}

// ============ 攻城 ============
bool battle(Faction& attacker, Faction& defender, City& target_city) {
    cout << "\n【攻城战】" << endl;
    cout << attacker.name << "国 攻打 " << defender.name << "国 " << target_city.name << 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()) {
        cout << "没有武将出征!" << endl;
        return false;
    }

    int atk_army = 0;
    int def_army = target_city.army;

    for (size_t i = 0; i < attacker.cities.size(); i++) {
        atk_army += attacker.cities[i].army;
    }

    cout << "攻击方兵力: " << atk_army << " 防守方兵力: " << def_army << endl;
    cout << "攻击方武将: " << atk_gens.size() << " 防守方武将: " << def_gens.size() << endl;

    battlePhase(attacker, defender, atk_gens, def_gens);

    if (atk_gens.empty()) {
        cout << "攻城失败!" << endl;
        for (size_t i = 0; i < attacker.cities.size(); i++) {
            attacker.cities[i].army *= 0.7;
        }
        return false;
    } else {
        cout << "攻城成功!" << endl;

        target_city.owner = attacker.name;

        for (size_t i = 0; i < def_gens.size(); i++) {
            if (def_gens[i].is_alive) {
                def_gens[i].loyalty -= 20;
                if (def_gens[i].loyalty < 30) {
                    cout << def_gens[i].name << " 投降了!" << endl;
                    attacker.generals.push_back(def_gens[i]);
                } else {
                    cout << def_gens[i].name << " 逃走了。" << endl;
                }
            }
        }

        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;
            }
        }

        target_city.army *= 0.5;
        attacker.cities.push_back(target_city);

        return true;
    }
}

// ============ 攻击 ============
void doAttack(Game& game) {
    Faction& f = game.factions[game.current_faction];
    cout << "\n【出征】" << endl;

    cout << "选择攻击目标:" << 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 << 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];

    int total_army = 0;
    for (size_t i = 0; i < f.cities.size(); i++) {
        total_army += f.cities[i].army;
    }

    if (total_army < 500) {
        cout << "兵力不足!需要至少500兵力。" << endl;
        waitForEnter();
        return;
    }

    if (battle(f, defender, city)) {
        if (defender.cities.empty()) {
            defender.is_alive = false;
            cout << 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;
                        }
                    }
                }

                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 << "    |                                                           |" << endl;
                cout << "    |  提示:发展商业和农业可以持续增加收入!                  |" << endl;
                cout << "    |                                                           |" << endl;
                cout << "    +===========================================================+" << endl;
                waitForEnter();
                break;
            }
            case 3:
                cout << "感谢游玩,再见!" << endl;
                exit_game = true;
                break;
            default:
                cout << "无效选择!" << endl;
                waitForEnter();
        }
    }

    return 0;
}