人生模拟器

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

// 职业类
class Job {
public:
    string name;
    int salary;
    int stress;
    int satisfaction;

    Job(string n, int s, int str, int sat) 
        : name(n), salary(s), stress(str), satisfaction(sat) {}
};

// 事件类
class Event {
public:
    string description;
    int minAge;
    int maxAge;
    bool isPositive;
    int moneyEffect;
    int happinessEffect;
    int healthEffect;

    Event(string desc, int min, int max, bool pos, int money, int happy, int health) 
        : description(desc), minAge(min), maxAge(max), isPositive(pos), 
          moneyEffect(money), happinessEffect(happy), healthEffect(health) {}
};

// 人物类
class Person {
private:
    string name;
    int age;
    int gender; // 0: 男, 1: 女
    int money;
    int health;
    int happiness;
    bool isMarried;
    int children;
    Job* currentJob;
    vector<Event> lifeEvents;

public:
    Person(string n, int g) 
        : name(n), gender(g), age(0), money(0), health(100), happiness(100), 
          isMarried(false), children(0), currentJob(nullptr) {
        srand(time(0));
        generateLifeEvents();
    }

    ~Person() {
        delete currentJob;
    }

    // 生成人生事件
    void generateLifeEvents() {
        // 添加各种人生事件
        lifeEvents.push_back(Event("你在学校赢得了绘画比赛,获得了500元奖金。", 6, 18, true, 500, 10, 0));
        lifeEvents.push_back(Event("你在工作中得到晋升,薪水增加了。", 22, 65, true, 5000, 15, -5));
        lifeEvents.push_back(Event("你生病了,花费了大量金钱治疗。", 1, 100, false, -3000, -10, -20));
        lifeEvents.push_back(Event("你遇到了一生的挚爱,你们结婚了。", 20, 45, true, -10000, 30, 0));
        lifeEvents.push_back(Event("你的第一个孩子出生了。", 22, 45, true, -5000, 25, -10));
        lifeEvents.push_back(Event("你买了一辆新车。", 18, 65, true, -20000, 15, 0));
        lifeEvents.push_back(Event("你投资失败,损失了很多钱。", 18, 65, false, -15000, -20, 0));
        lifeEvents.push_back(Event("你赢得了彩票,获得了50000元。", 18, 65, true, 50000, 30, 0));
        lifeEvents.push_back(Event("你参加了马拉松比赛,身体变得更健康。", 18, 50, true, -500, 10, 15));
        lifeEvents.push_back(Event("你被公司裁员了。", 22, 65, false, -8000, -25, 0));
    }

    // 获取随机事件
    Event getRandomEvent() {
        vector<Event> possibleEvents;
        for (const auto& event : lifeEvents) {
            if (age >= event.minAge && age <= event.maxAge) {
                possibleEvents.push_back(event);
            }
        }

        if (possibleEvents.empty()) {
            return Event("今天过得很平淡。", age, age, true, 0, 0, 0);
        }

        int index = rand() % possibleEvents.size();
        return possibleEvents[index];
    }

    // 选择职业
    void chooseJob() {
        vector<Job> jobs = {
            Job("软件工程师", 12000, 70, 80),
            Job("教师", 6000, 50, 90),
            Job("医生", 15000, 90, 85),
            Job("艺术家", 4000, 30, 95),
            Job("企业家", 20000, 95, 75),
            Job("公务员", 7000, 40, 70),
            Job("运动员", 18000, 80, 90),
            Job("作家", 5000, 45, 90)
        };

        cout << "请选择你的职业:" << endl;
        for (size_t i = 0; i < jobs.size(); i++) {
            cout << i + 1 << ". " << jobs[i].name 
                 << " (月薪: " << jobs[i].salary 
                 << ", 压力: " << jobs[i].stress 
                 << ", 满意度: " << jobs[i].satisfaction << ")" << endl;
        }

        int choice;
        do {
            cout << "输入职业编号 (1-" << jobs.size() << "): ";
            cin >> choice;
        } while (choice < 1 || choice > static_cast<int>(jobs.size()));

        currentJob = new Job(jobs[choice - 1]);
        cout << "你现在是一名" << currentJob->name << "!" << endl;
    }

    // 年龄增长
    void growOlder() {
        age++;
        cout << "\n=== 你现在" << age << "岁了 ===" << endl;

        // 基本属性变化
        if (age > 30) health -= (age - 30) / 5;
        if (age > 50) health -= (age - 50) / 3;
        
        health = max(0, min(100, health));
        happiness = max(0, min(100, happiness));
        
        // 如果有工作,获得薪水并增加压力
        if (currentJob) {
            money += currentJob->salary;
            health -= currentJob->stress / 10;
            happiness += currentJob->satisfaction / 10;
            
            // 随机事件
            if (rand() % 3 == 0) {
                Event event = getRandomEvent();
                cout << event.description << endl;
                money += event.moneyEffect;
                happiness += event.happinessEffect;
                health += event.healthEffect;
                
                // 特殊事件处理
                if (event.description.find("结婚") != string::npos) {
                    isMarried = true;
                } else if (event.description.find("孩子出生") != string::npos) {
                    children++;
                }
            }
        } else if (age >= 22) {
            // 到了工作年龄还没有工作
            cout << "你需要一份工作来维持生计!" << endl;
            chooseJob();
        }
        
        // 显示当前状态
        displayStatus();
        
        // 年龄相关的决策
        if (age >= 18 && age <= 30 && !isMarried && rand() % 5 == 0) {
            cout << "你遇到了一个特别的人,想和TA结婚吗?(1-是, 0-否): ";
            int choice;
            cin >> choice;
            if (choice) {
                cout << "恭喜你结婚了!" << endl;
                isMarried = true;
                money -= 10000; // 结婚花费
                happiness += 30;
            }
        }
        
        if (isMarried && children < 3 && age >= 22 && age <= 45 && rand() % 6 == 0) {
            cout << "你和你的配偶想要一个孩子吗?(1-是, 0-否): ";
            int choice;
            cin >> choice;
            if (choice) {
                cout << "恭喜你,你的孩子出生了!" << endl;
                children++;
                money -= 5000;
                happiness += 25;
                health -= 10;
            }
        }
        
        // 健康检查
        if (health <= 0) {
            cout << "你的健康状况恶化,不幸去世了..." << endl;
            displayLifeSummary();
            exit(0);
        }
        
        // 退休年龄
        if (age >= 65) {
            cout << "你已经到了退休年龄,开始享受晚年生活!" << endl;
            delete currentJob;
            currentJob = nullptr;
            money += 3000; // 退休金
        }
    }

    // 显示当前状态
    void displayStatus() {
        cout << "状态: " << endl;
        cout << "金钱: " << money << " 元" << endl;
        cout << "健康: " << health << "%" << endl;
        cout << "幸福度: " << happiness << "%" << endl;
        if (currentJob) {
            cout << "职业: " << currentJob->name << " (月薪: " << currentJob->salary << ")" << endl;
        } else {
            cout << "职业: 无" << endl;
        }
        cout << "婚姻状况: " << (isMarried ? "已婚" : "单身") << endl;
        cout << "子女: " << children << endl;
    }

    // 显示人生总结
    void displayLifeSummary() {
        cout << "\n=== 人生总结 ===" << endl;
        cout << "姓名: " << name << endl;
        cout << "享年: " << age << "岁" << endl;
        cout << "最终财富: " << money << " 元" << endl;
        cout << "家庭: " << (isMarried ? "已婚," : "单身,") << children << "个孩子" << endl;
        
        if (currentJob) {
            cout << "职业: " << currentJob->name << endl;
        }
        
        // 评分
        int finalScore = (money / 1000) + (health * 2) + (happiness * 3);
        if (isMarried) finalScore += 50;
        finalScore += children * 30;
        
        cout << "人生评分: " << finalScore << "/1000" << endl;
        
        if (finalScore >= 900) {
            cout << "你的人生堪称完美!" << endl;
        } else if (finalScore >= 700) {
            cout << "你的人生非常充实!" << endl;
        } else if (finalScore >= 500) {
            cout << "你的人生过得还不错!" << endl;
        } else if (finalScore >= 300) {
            cout << "你的人生有些坎坷,但也有许多值得回忆的时刻。" << endl;
        } else {
            cout << "你的人生充满了挑战,也许来世会更好。" << endl;
        }
    }
};

int main() {
    cout << "=== 人生模拟器 ===" << endl;
    
    string name;
    cout << "请输入你的名字: ";
    cin >> name;
    
    int gender;
    do {
        cout << "请选择性别 (0-男, 1-女): ";
        cin >> gender;
    } while (gender != 0 && gender != 1);
    
    Person person(name, gender);
    
    while (true) {
        person.growOlder();
        
        // 等待用户继续
        cout << "\n按Enter键继续..." << endl;
        cin.ignore();
        cin.get();
    }
    
    return 0;
}    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值