跳到正文

休闲·娱乐

策略游戏-帝国建设者

koukou约 32 分钟
原文

这是 2025年10月17日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月18日
篇幅
约 32 分钟

版本

  1. v2当前
  2. v1

有待更新qwq

C++
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;

void Color(int a) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}

class EmpireBuilder {
private:
    struct City {
        string name;
        int population;
        int economy;
        int military;
        int technology;
        int happiness;
        int player; // 0:玩家, 1:AI
    };
    
    struct Resource {
        int gold;
        int food;
        int research;
        int influence;
    };
    
    vector<City> cities;
    Resource playerResource, aiResource;
    int turn;
    vector<string> gameLog;
    
public:
    EmpireBuilder() {
        // 初始化城市
        cities.push_back({"首都", 10000, 30, 20, 10, 80, 0});
        cities.push_back({"边境城", 5000, 20, 30, 5, 70, 0});
        cities.push_back({"工业城", 8000, 40, 10, 15, 60, 1});
        cities.push_back({"科技城", 6000, 25, 15, 30, 75, 1});
        
        // 初始化资源
        playerResource = {100, 200, 50, 30};
        aiResource = {100, 200, 50, 30};
        
        turn = 1;
    }
    
    void DisplayGameState() {
        system("cls");
        
        Color(11);
        cout << "═══════════════════════════════════════════════════" << endl;
        cout << "                 帝国建设者 - 回合 " << turn << endl;
        cout << "═══════════════════════════════════════════════════" << endl;
        
        // 显示玩家资源
        Color(14);
        cout << "?? 你的资源: ";
        Color(10);
        cout << "黄金:" << playerResource.gold << " ";
        Color(6);
        cout << "粮食:" << playerResource.food << " ";
        Color(3);
        cout << "科研:" << playerResource.research << " ";
        Color(13);
        cout << "影响力:" << playerResource.influence << endl;
        
        Color(12);
        cout << "?? AI资源:   ";
        Color(10);
        cout << "黄金:" << aiResource.gold << " ";
        Color(6);
        cout << "粮食:" << aiResource.food << " ";
        Color(3);
        cout << "科研:" << aiResource.research << " ";
        Color(13);
        cout << "影响力:" << aiResource.influence << endl << endl;
        
        // 显示城市信息
        DisplayCities();
        
        // 显示游戏日志
        DisplayGameLog();
        
        // 显示操作选项
        DisplayActions();
    }
    
    void DisplayCities() {
        Color(11);
        cout << "???  城市状态:" << endl;
        cout << "───────────────────────────────────────────────────" << endl;
        
        for(int i = 0; i < cities.size(); i++) {
            City& city = cities[i];
            
            if(city.player == 0) {
                Color(10); // 玩家城市绿色
                cout << "你";
            } else {
                Color(12); // AI城市红色
                cout << "AI";
            }
            
            cout << " - " << city.name;
            cout << " ??" << city.population;
            cout << " ??" << city.economy;
            cout << " ??" << city.military;
            cout << " ??" << city.technology;
            cout << " ??" << city.happiness;
            cout << endl;
        }
        cout << endl;
    }
    
    void DisplayGameLog() {
        Color(11);
        cout << "?? 游戏日志:" << endl;
        cout << "───────────────────────────────────────────────────" << endl;
        
        for(int i = max(0, (int)gameLog.size() - 5); i < gameLog.size(); i++) {
            Color(7);
            cout << "? " << gameLog[i] << endl;
        }
        cout << endl;
    }
    
    void DisplayActions() {
        Color(11);
        cout << "?? 可执行操作:" << endl;
        cout << "───────────────────────────────────────────────────" << endl;
        
        Color(14);
        cout << "1. 发展经济 (消耗50黄金,提升城市经济)" << endl;
        cout << "2. 军事扩张 (消耗30黄金,提升城市军事)" << endl;
        cout << "3. 科技研发 (消耗40科研,提升城市科技)" << endl;
        cout << "4. 民生建设 (消耗20黄金,提升幸福度)" << endl;
        cout << "5. 外交行动 (消耗25影响力,可能获得城市)" << endl;
        cout << "6. 间谍活动 (消耗15黄金,降低AI城市幸福度)" << endl;
        cout << "7. 结束回合" << endl;
        cout << "8. 退出游戏" << endl << endl;
        
        Color(10);
        cout << "请选择操作 (1-8): ";
    }
    
    void AddLog(string message) {
        gameLog.push_back("回合 " + to_string(turn) + ": " + message);
    }
    
    void PlayerTurn() {
        bool turnEnded = false;
        
        while(!turnEnded) {
            DisplayGameState();
            
            char choice = _getch();
            
            switch(choice) {
                case '1':
                    DevelopEconomy();
                    break;
                case '2':
                    MilitaryExpansion();
                    break;
                case '3':
                    ResearchTechnology();
                    break;
                case '4':
                    BuildWelfare();
                    break;
                case '5':
                    DiplomaticAction();
                    break;
                case '6':
                    SpyAction();
                    break;
                case '7':
                    turnEnded = true;
                    AddLog("玩家结束回合");
                    break;
                case '8':
                    exit(0);
                default:
                    break;
            }
        }
    }
    
    void DevelopEconomy() {
        if(playerResource.gold < 50) {
            AddLog("黄金不足,无法发展经济");
            return;
        }
        
        playerResource.gold -= 50;
        
        // 随机选择一个玩家城市发展经济
        vector<int> playerCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 0) playerCities.push_back(i);
        }
        
        if(!playerCities.empty()) {
            int cityIndex = playerCities[rand() % playerCities.size()];
            cities[cityIndex].economy += 5 + rand() % 6;
            cities[cityIndex].happiness += 2;
            
            AddLog("在" + cities[cityIndex].name + "发展经济,经济+" + 
                  to_string(cities[cityIndex].economy - (cities[cityIndex].economy - 5 - rand() % 6)));
        }
    }
    
    void MilitaryExpansion() {
        if(playerResource.gold < 30) {
            AddLog("黄金不足,无法进行军事扩张");
            return;
        }
        
        playerResource.gold -= 30;
        
        vector<int> playerCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 0) playerCities.push_back(i);
        }
        
        if(!playerCities.empty()) {
            int cityIndex = playerCities[rand() % playerCities.size()];
            cities[cityIndex].military += 4 + rand() % 4;
            
            AddLog("在" + cities[cityIndex].name + "进行军事扩张,军力+" + 
                  to_string(cities[cityIndex].military - (cities[cityIndex].military - 4 - rand() % 4)));
        }
    }
    
    void ResearchTechnology() {
        if(playerResource.research < 40) {
            AddLog("科研点数不足,无法进行研发");
            return;
        }
        
        playerResource.research -= 40;
        
        vector<int> playerCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 0) playerCities.push_back(i);
        }
        
        if(!playerCities.empty()) {
            int cityIndex = playerCities[rand() % playerCities.size()];
            cities[cityIndex].technology += 3 + rand() % 4;
            
            AddLog("在" + cities[cityIndex].name + "进行科技研发,科技+" + 
                  to_string(cities[cityIndex].technology - (cities[cityIndex].technology - 3 - rand() % 4)));
        }
    }
    
    void BuildWelfare() {
        if(playerResource.gold < 20) {
            AddLog("黄金不足,无法进行民生建设");
            return;
        }
        
        playerResource.gold -= 20;
        
        vector<int> playerCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 0) playerCities.push_back(i);
        }
        
        if(!playerCities.empty()) {
            int cityIndex = playerCities[rand() % playerCities.size()];
            cities[cityIndex].happiness += 8 + rand() % 7;
            
            AddLog("在" + cities[cityIndex].name + "进行民生建设,幸福度+" + 
                  to_string(cities[cityIndex].happiness - (cities[cityIndex].happiness - 8 - rand() % 7)));
        }
    }
    
    void DiplomaticAction() {
        if(playerResource.influence < 25) {
            AddLog("影响力不足,无法进行外交");
            return;
        }
        
        playerResource.influence -= 25;
        
        // 寻找AI城市
        vector<int> aiCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 1) aiCities.push_back(i);
        }
        
        if(!aiCities.empty()) {
            int cityIndex = aiCities[rand() % aiCities.size()];
            
            // 外交成功率基于玩家影响力和城市幸福度
            int successRate = min(80, playerResource.influence + cities[cityIndex].happiness / 2);
            
            if(rand() % 100 < successRate) {
                // 外交成功,获得城市
                cities[cityIndex].player = 0;
                AddLog("?? 外交成功!获得了" + cities[cityIndex].name + "的控制权!");
            } else {
                AddLog("外交失败," + cities[cityIndex].name + "拒绝了你的提议");
            }
        }
    }
    
    void SpyAction() {
        if(playerResource.gold < 15) {
            AddLog("黄金不足,无法进行间谍活动");
            return;
        }
        
        playerResource.gold -= 15;
        
        vector<int> aiCities;
        for(int i=0; i<cities.size(); i++) {
            if(cities[i].player == 1) aiCities.push_back(i);
        }
        
        if(!aiCities.empty()) {
            int cityIndex = aiCities[rand() % aiCities.size()];
            cities[cityIndex].happiness -= 10 + rand() % 11;
            cities[cityIndex].happiness = max(0, cities[cityIndex].happiness);
            
            AddLog("间谍活动成功," + cities[cityIndex].name + "幸福度下降");
        }
    }
    
    void AITurn() {
        AddLog("AI开始行动");
        
        // AI策略:根据当前状况选择行动
        vector<void (EmpireBuilder::*)()> actions = {
            &EmpireBuilder::AIDevelopEconomy,
            &EmpireBuilder::AIMilitaryExpansion,
            &EmpireBuilder::AIResearchTechnology,
            &EmpireBuilder::AIBuildWelfare,
            &EmpireBuilder::AISpyAction
        };
        
        // AI每回合执行2-3个行动
        int actionCount = 2 + rand() % 2;
        for(int i=0; i<actionCount; i++) {
            if(!actions.empty()) {
                int actionIndex = rand() % actions.size();
                (this->*actions[actionIndex])();
            }
        }
    }
    
    void AIDevelopEconomy() {
        if(aiResource.gold >= 50) {
            aiResource.gold -= 50;
            vector<int> aiCities;
            for(int i=0; i<cities.size(); i++) {
                if(cities[i].player == 1) aiCities.push_back(i);
            }
            if(!aiCities.empty()) {
                int cityIndex = aiCities[rand() % aiCities.size()];
                cities[cityIndex].economy += 5 + rand() % 6;
                AddLog("AI发展了" + cities[cityIndex].name + "的经济");
            }
        }
    }
    
    void AIMilitaryExpansion() {
        if(aiResource.gold >= 30) {
            aiResource.gold -= 30;
            vector<int> aiCities;
            for(int i=0; i<cities.size(); i++) {
                if(cities[i].player == 1) aiCities.push_back(i);
            }
            if(!aiCities.empty()) {
                int cityIndex = aiCities[rand() % aiCities.size()];
                cities[cityIndex].military += 4 + rand() % 4;
                AddLog("AI在" + cities[cityIndex].name + "进行了军事扩张");
            }
        }
    }
    
    void AIResearchTechnology() {
        if(aiResource.research >= 40) {
            aiResource.research -= 40;
            vector<int> aiCities;
            for(int i=0; i<cities.size(); i++) {
                if(cities[i].player == 1) aiCities.push_back(i);
            }
            if(!aiCities.empty()) {
                int cityIndex = aiCities[rand() % aiCities.size()];
                cities[cityIndex].technology += 3 + rand() % 4;
                AddLog("AI在" + cities[cityIndex].name + "进行了科技研发");
            }
        }
    }
    
    void AIBuildWelfare() {
        if(aiResource.gold >= 20) {
            aiResource.gold -= 20;
            vector<int> aiCities;
            for(int i=0; i<cities.size(); i++) {
                if(cities[i].player == 1) aiCities.push_back(i);
            }
            if(!aiCities.empty()) {
                int cityIndex = aiCities[rand() % aiCities.size()];
                cities[cityIndex].happiness += 8 + rand() % 7;
                AddLog("AI在" + cities[cityIndex].name + "进行了民生建设");
            }
        }
    }
    
    void AISpyAction() {
        if(aiResource.gold >= 15) {
            aiResource.gold -= 15;
            vector<int> playerCities;
            for(int i=0; i<cities.size(); i++) {
                if(cities[i].player == 0) playerCities.push_back(i);
            }
            if(!playerCities.empty()) {
                int cityIndex = playerCities[rand() % playerCities.size()];
                cities[cityIndex].happiness -= 10 + rand() % 11;
                cities[cityIndex].happiness = max(0, cities[cityIndex].happiness);
                AddLog("AI的间谍活动降低了" + cities[cityIndex].name + "的幸福度");
            }
        }
    }
    
    void UpdateResources() {
        // 每回合根据城市产出资源
        for(auto& city : cities) {
            if(city.player == 0) {
                playerResource.gold += city.economy / 3;
                playerResource.food += city.population / 500;
                playerResource.research += city.technology / 4;
                playerResource.influence += max(0, city.happiness - 50) / 10;
            } else {
                aiResource.gold += city.economy / 3;
                aiResource.food += city.population / 500;
                aiResource.research += city.technology / 4;
                aiResource.influence += max(0, city.happiness - 50) / 10;
            }
        }
    }
    
    bool CheckGameEnd() {
        // 检查是否有玩家失去所有城市
        bool playerHasCities = false, aiHasCities = false;
        for(auto& city : cities) {
            if(city.player == 0) playerHasCities = true;
            else aiHasCities = true;
        }
        
        if(!playerHasCities) {
            DisplayGameState();
            Color(12);
            cout << "?? 游戏结束!你失去了所有城市!" << endl;
            return true;
        }
        if(!aiHasCities) {
            DisplayGameState();
            Color(10);
            cout << "?? 恭喜!你征服了所有城市!" << endl;
            return true;
        }
        if(turn >= 20) {
            DisplayGameState();
            Color(14);
            cout << "? 游戏结束!达到最大回合数!" << endl;
            
            // 计算最终得分
            int playerScore = 0, aiScore = 0;
            for(auto& city : cities) {
                if(city.player == 0) playerScore += city.population + city.economy + city.military + city.technology;
                else aiScore += city.population + city.economy + city.military + city.technology;
            }
            
            if(playerScore > aiScore) {
                Color(10);
                cout << "?? 你获得了胜利!最终比分 " << playerScore << " : " << aiScore << endl;
            } else if(aiScore > playerScore) {
                Color(12);
                cout << "?? AI获得了胜利!最终比分 " << aiScore << " : " << playerScore << endl;
            } else {
                Color(14);
                cout << "?? 平局!比分 " << playerScore << " : " << aiScore << endl;
            }
            return true;
        }
        return false;
    }
    
    void Run() {
        srand((unsigned)time(NULL));
        
        while(true) {
            if(CheckGameEnd()) break;
            
            PlayerTurn();
            UpdateResources();
            AITurn();
            UpdateResources();
            
            turn++;
        }
        
        Color(7);
        cout << endl << "按任意键退出..." << endl;
        _getch();
    }
};

int main() {
    EmpireBuilder game;
    game.Run();
    return 0;
}