问题标题: 酷丁编程:1

0
0
陶朴鸣
陶朴鸣
修练者
修练者

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <limits>
#include <cstdlib>
#include <ctime>

using namespace std;

// 建筑基类
class Building {
public:
    string name;
    int cost;
    int income;
    int maintenance;
    int populationEffect;
    int happinessEffect;
    
    Building(string n, int c, int inc, int main, int pop, int hap) 
    : name(n), cost(c), income(inc), maintenance(main), populationEffect(pop), happinessEffect(hap) {}
    
    virtual ~Building() = default;
};

// 具体建筑类型
class House : public Building {
public:
    House() : Building("住宅", 200, 0, 10, 5, 2) {}
};

class Shop : public Building {
public:
    Shop() : Building("商店", 300, 50, 20, 0, 1) {}
};

class Park : public Building {
public:
    Park() : Building("公园", 150, 0, 5, 0, 5) {}
};

class Factory : public Building {
public:
    Factory() : Building("工厂", 500, 100, 30, 0, -3) {}
};

// 游戏**类
class TownManager {
private:
    int money;
    int population;
    int happiness;
    vector<Building*> buildings;
    
public:
    TownManager() : money(10000), population(0), happiness(5) {
        srand(time(nullptr));
    }
    
    ~TownManager() {
        for (auto building : buildings) {
            delete building;
        }
    }
    
    void displayStatus() {
        cout << "\n========== 小镇状态 ==========\n";
        cout << "?? 金币: " << money << endl;
        cout << "?? 人口: " << population << endl;
        cout << "?? 幸福度: " << happiness << "/1000" << endl;
        cout << "?? 建筑数量: " << buildings.size() << endl;
        
        int totalIncome = 0, totalMaintenance = 0;
        for (const auto& b : buildings) {
            totalIncome += b->income;
            totalMaintenance += b->maintenance;
        }
        cout << "?? 总收入: " << totalIncome << " 金币/天" << endl;
        cout << "?? 维护费: " << totalMaintenance << " 金币/天" << endl;
        cout << "?? 净收益: " << (totalIncome - totalMaintenance) << " 金币/天" << endl;
        cout << "============================\n";
    }
    
    void displayBuildings() {
        cout << "\n========== 已建建筑 ==========\n";
        if (buildings.empty()) {
            cout << "还没有建造任何建筑。\n";
        } else {
            for (size_t i = 0; i < buildings.size(); ++i) {
                cout << i + 1 << ". " << buildings[i]->name << endl;
            }
        }
        cout << "============================\n";
    }
    
    void showConstructionMenu() {
        cout << "\n========== 建造菜单 ==========\n";
        cout << "1. ?? 住宅 (200金币) - 增加人口,小幅提升幸福度\n";
        cout << "2. ?? 商店 (300金币) - 产生稳定收入\n";
        cout << "3. ?? 公园 (150金币) - 大幅提升幸福度\n";
        cout << "4. ?? 工厂 (500金币) - 高额收入但降低幸福度\n";
        cout << "0. 返回主菜单\n";
        cout << "请选择: ";
    }
    
    bool constructBuilding(int choice) {
        Building* newBuilding = nullptr;
        
        switch(choice) {
        case 1:
            newBuilding = new House();
            break;
        case 2:
            newBuilding = new Shop();
            break;
        case 3:
            newBuilding = new Park();
            break;
        case 4:
            newBuilding = new Factory();
            break;
        default:
            cout << "无效选择!\n";
            return false;
        }
        
        if (newBuilding) {
            if (money >= newBuilding->cost) {
                money -= newBuilding->cost;
                buildings.push_back(newBuilding);
                population += newBuilding->populationEffect;
                happiness = max(0, min(100, happiness + newBuilding->happinessEffect));
                cout << "? 成功建造了 " << newBuilding->name << "!\n";
                return true;
            } else {
                cout << "? 金币不足! 需要 " << newBuilding->cost << " 金币.\n";
                delete newBuilding;
                return false;
            }
        }
        return false;
    }
    
    void processDailyUpdate() {
        int income = 0, maintenance = 0;
        for (const auto& b : buildings) {
            income += b->income;
            maintenance += b->maintenance;
        }
        
        int netIncome = income - maintenance;
        money += netIncome;
        
        cout << "\n?? 日常结算:\n";
        cout << "   收入: +" << income << " 金币\n";
        cout << "   维护: -" << maintenance << " 金币\n";
        cout << "   净收益: " << (netIncome >= 0 ? "+" : "") << netIncome << " 金币\n";
        cout << "   当前余额: " << money << " 金币\n";
    }
    
    void showMainMenu() {
        cout << "\n========== 小镇经营游戏 ==========\n";
        cout << "1. 查看小镇状态\n";
        cout << "2. 建造建筑\n";
        cout << "3. 查看已建建筑\n";
        cout << "4. 进行一天结算\n";
        cout << "5. 游戏规则说明\n";
        cout << "0. 退出游戏\n";
        cout << "请选择操作: ";
    }
    
    void showGameRules() {
        cout << "\n========== 游戏规则 ==========\n";
        cout << "?? 目标: 发展繁荣的小镇\n";
        cout << "?? 初始资金: 10000金币\n";
        cout << "?? 建造不同类型建筑来发展小镇:\n";
        cout << "   ? 住宅: 增加人口容量和幸福度\n";
        cout << "   ? 商店: 产生稳定收入\n";
        cout << "   ? 公园: 提升居民幸福度\n";
        cout << "   ? 工厂: 高额收入但影响幸福度\n";
        cout << "?? 每天会有收入和维护费用结算\n";
        cout << "?? 幸福度过低会影响小镇发展\n";
        cout << "============================\n";
    }
    
    bool isBankrupt() const {
        return money < 0;
    }
    
    int getMoney() const { return money; }
};

int main() {
    TownManager town;
    int choice;
    
    cout << "?? 欢迎来到小镇经营游戏!" << endl;
    town.showGameRules();
    
    while (true) {
        town.showMainMenu();
        cin >> choice;
        
        // 输入验证
        if (cin.fail()) {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "? 请输入有效数字!\n";
            continue;
        }
        
        switch (choice) {
        case 1:
            town.displayStatus();
            break;
            
            case 2: {
                int buildChoice;
                do {
                    town.showConstructionMenu();
                    cin >> buildChoice;
                    
                    if (cin.fail()) {
                        cin.clear();
                        cin.ignore(numeric_limits<streamsize>::max(), '\n');
                        cout << "? 请输入有效数字!\n";
                        continue;
                    }
                    
                    if (buildChoice >= 1 && buildChoice <= 4) {
                        town.constructBuilding(buildChoice);
                    } else if (buildChoice != 0) {
                        cout << "? 无效选择!\n";
                    }
                } while (buildChoice != 0);
                break;
            }
            
        case 3:
            town.displayBuildings();
            break;
            
        case 4:
            town.processDailyUpdate();
            if (town.isBankrupt()) {
                cout << "\n?? 破产了! 游戏结束...\n";
                cout << "感谢您的游玩!\n";
                return 0;
            }
            break;
            
        case 5:
            town.showGameRules();
            break;
            
        case 0:
            cout << "?? 再见! 感谢游玩小镇经营游戏!\n";
            return 0;
            
        default:
            cout << "? 无效选择,请重新输入!\n";
        }
    }
    
    return 0;
}


0
0
0
0
马弘毅
马弘毅
新手守护
新手守护

666666666666666666666666666666666666666

0
马弘毅
马弘毅
新手守护
新手守护

66666666666666666666666666666666666666

0
我要回答