农场游戏(0.5.0)

游戏包含5x5的农场地图,玩家可以种植10种不同作物(如小麦、玉米等),每种作物有各自的生长周期和价格。游戏提供4种道具(肥料、除草剂等)来辅助经营,并随机发生自然灾害(虫害、干旱等)。主要功能包括种植收获作物、道具商店、查看状态等。游戏采用面向对象设计,使用vector和map等STL容器存储数据,通过随机数模拟灾害事件,玩家目标是通过合理经营赚取更多资金。

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;

struct Crop {
    string name;
    int growthDays;
    int buyPrice;
    int sellPrice;
    int plantedDay;
    bool isHarvestable;
    bool isGrowing;
};

struct Tool {
    string name;
    string description;
    int price;
    int effectValue;
};

class FarmGame {
private:
    vector<vector<Crop>> farm;
    map<string, int> inventory;
    int money;
    int day;
    const int SIZE = 5;
    mt19937 rng;
    
    const map<string, Crop> CROPS = {
        {"小麦", {"小麦", 3, 10, 20, 0, false, false}},
        {"玉米", {"玉米", 5, 15, 30, 0, false, false}},
        {"胡萝卜", {"胡萝卜", 7, 20, 45, 0, false, false}},
        {"土豆", {"土豆", 4, 12, 25, 0, false, false}},
        {"番茄", {"番茄", 6, 18, 35, 0, false, false}},
        {"草莓", {"草莓", 8, 25, 50, 0, false, false}},
        {"西瓜", {"西瓜", 10, 30, 70, 0, false, false}},
        {"南瓜", {"南瓜", 9, 28, 60, 0, false, false}},
        {"水稻", {"水稻", 5, 15, 32, 0, false, false}},
        {"向日葵", {"向日葵", 7, 22, 45, 0, false, false}}
    };

    const map<string, Tool> TOOLS = {
        {"肥料", {"肥料", "加速作物生长1天", 50, 1}},
        {"除草剂", {"除草剂", "立即收获指定作物", 80, 0}},
        {"温室", {"温室", "延长作物生长时间", 150, 2}},
        {"保险", {"保险", "减少自然灾害损失", 100, 0}}
    };

    void displayMenu() {
        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 << "8. 使用道具" << endl;
        cout << "9. 退出游戏" << endl;
        cout << "选择操作(1-9): ";
    }

    void showStatus() {
        cout << "\n=== 农场状态 ===" << endl;
        cout << "当前资金: " << money << "元" << endl;
        cout << "当前天数: " << day << endl;
        cout << "道具库存: ";
        for(const auto& item : inventory) {
            cout << item.first << "x" << item.second << " ";
        }
        cout << endl;
    }

    void showFarm() {
        cout << "\n=== 农场地图 ===" << endl;
        for(int i = 0; i < SIZE; i++) {
            for(int j = 0; j < SIZE; j++) {
                if(farm[i][j].isGrowing) {
                    cout << farm[i][j].name << "(" 
                         << (farm[i][j].isHarvestable ? "可收获" : "生长中") << ") ";
                } else {
                    cout << "[空地] ";
                }
            }
            cout << endl;
        }
    }

    void showCropList() {
        cout << "\n=== 可用作物 ===" << endl;
        for(const auto& crop : CROPS) {
            cout << crop.first << " - 生长周期:" << crop.second.growthDays 
                 << "天, 成本:" << crop.second.buyPrice 
                 << "元, 售价:" << crop.second.sellPrice << "元" << endl;
        }
    }

    void showToolShop() {
        cout << "\n=== 道具商店 ===" << endl;
        for(const auto& tool : TOOLS) {
            cout << tool.first << " - " << tool.second.description 
                 << " 价格:" << tool.second.price << "元" << endl;
        }
    }

    void plant() {
        int x, y;
        string name;
        cout << "输入坐标(x y)和作物名: ";
        cin >> x >> y >> name;

        if(x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            cout << "无效坐标!" << endl;
            return;
        }

        if(farm[x][y].isGrowing) {
            cout << "该位置已有作物!" << endl;
            return;
        }

        auto it = CROPS.find(name);
        if(it == CROPS.end()) {
            cout << "没有这种作物!" << endl;
            return;
        }

        if(money < it->second.buyPrice) {
            cout << "资金不足!" << endl;
            return;
        }

        farm[x][y] = it->second;
        farm[x][y].plantedDay = day;
        farm[x][y].isGrowing = true;
        money -= it->second.buyPrice;
        cout << "种植成功!" << endl;
    }

    void harvest() {
        int x, y;
        cout << "输入坐标(x y): ";
        cin >> x >> y;

        if(x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            cout << "无效坐标!" << endl;
            return;
        }

        if(!farm[x][y].isGrowing) {
            cout << "该位置没有作物!" << endl;
            return;
        }

        if(!farm[x][y].isHarvestable) {
            cout << "作物还未成熟!" << endl;
            return;
        }

        money += farm[x][y].sellPrice;
        cout << "收获" << farm[x][y].name << "获得" << farm[x][y].sellPrice << "元!" << endl;
        farm[x][y] = {"", 0, 0, 0, 0, false, false};
    }

    void checkDisasters() {
        uniform_int_distribution<int> disasterChance(1, 100);
        uniform_int_distribution<int> disasterType(1, 3);
        
        if(disasterChance(rng) <= 15) { // 15%概率发生灾害
            int type = disasterType(rng);
            int loss = 0;
            string disasterName;
            
            switch(type) {
                case 1:
                    disasterName = "虫害";
                    loss = money * 0.1; // 损失10%资金
                    break;
                case 2:
                    disasterName = "干旱";
                    for(auto& row : farm) {
                        for(auto& crop : row) {
                            if(crop.isGrowing) {
                                loss += crop.buyPrice * 0.5; // 作物价值减半
                            }
                        }
                    }
                    break;
                case 3:
                    disasterName = "市场波动";
                    loss = 30 + (rand() % 70); // 30-100随机损失
                    break;
            }
            
            if(inventory["保险"] > 0) {
                loss /= 2; // 保险减半损失
                inventory["保险"]--;
                cout << "【保险生效】";
            }
            
            money -= loss;
            cout << "?? 发生" << disasterName << "!损失" << loss << "元" << endl;
        }
    }

    void nextDay() {
        day++;
        cout << "第" << day << "天开始了..." << endl;
        
        checkDisasters();
        
        for(int i = 0; i < SIZE; i++) {
            for(int j = 0; j < SIZE; j++) {
                if(farm[i][j].isGrowing && !farm[i][j].isHarvestable) {
                    int growth = day - farm[i][j].plantedDay;
                    if(growth >= farm[i][j].growthDays) {
                        farm[i][j].isHarvestable = true;
                        cout << farm[i][j].name << "(" << i << "," << j << ")已成熟!" << endl;
                    }
                }
            }
        }
    }

    void buyTool() {
        string name;
        cout << "输入要购买的道具名: ";
        cin >> name;

        auto it = TOOLS.find(name);
        if(it == TOOLS.end()) {
            cout << "没有这种道具!" << endl;
            return;
        }

        if(money < it->second.price) {
            cout << "资金不足!" << endl;
            return;
        }

        inventory[name]++;
        money -= it->second.price;
        cout << "购买成功!" << endl;
    }

    void useTool() {
        int x, y;
        string toolName;
        cout << "输入坐标(x y)和道具名: ";
        cin >> x >> y >> toolName;

        if(x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
            cout << "无效坐标!" << endl;
            return;
        }

        if(!farm[x][y].isGrowing) {
            cout << "该位置没有作物!" << endl;
            return;
        }

        if(inventory[toolName] <= 0) {
            cout << "道具不足!" << endl;
            return;
        }

        auto toolIt = TOOLS.find(toolName);
        if(toolIt == TOOLS.end()) {
            cout << "无效道具!" << endl;
            return;
        }

        switch(toolIt->second.effectValue) {
            case 1: // 肥料
                farm[x][y].growthDays--;
                cout << farm[x][y].name << "生长加速!" << endl;
                break;
            case 2: // 温室
                farm[x][y].growthDays += 2;
                cout << farm[x][y].name << "生长周期延长!" << endl;
                break;
            default:
                cout << "道具使用成功!" << endl;
        }

        inventory[toolName]--;
    }

public:
    FarmGame() : money(100), day(1), rng(time(0)) {
        farm.resize(SIZE, vector<Crop>(SIZE, {"", 0, 0, 0, 0, false, false}));
    }

    void run() {
        while(true) {
            displayMenu();
            int choice;
            cin >> choice;
            
            switch(choice) {
                case 1: showStatus(); showFarm(); break;
                case 2: plant(); break;
                case 3: harvest(); break;
                case 4: nextDay(); break;
                case 5: showCropList(); break;
                case 6: showToolShop(); break;
                case 7: buyTool(); break;
                case 8: useTool(); break;
                case 9: return;
                default: cout << "无效输入!" << endl;
            }
        }
    }
};

int main() {
    FarmGame game;
    game.run();
    return 0;
}



 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值