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

using namespace std;

// 游戏角色属性(新增婚姻状态)
struct Xiangzi {
    int health;     // 健康值 (0-100)
    int money;      // 金钱 (铜子)
    int morale;     // 士气 (0-100)
    bool has_cart;  // 是否拥有自己的车
    int day;        // 当前天数
    bool married;   // 是否和虎妞结婚
    bool met_cao;   // 是否遇到曹先生
};

// 全局变量
Xiangzi player;
const int CART_PRICE = 960;  // 买车需要960个铜子

// 函数声明
void initGame();
void showStatus();
void dailyChoice();
void pullCart();
void rest();
void buyCart();
void randomEvent();
void checkGameOver();
void showEnding();
void tigerGirlEvent();    // 虎妞事件
void mrCaoEvent();        // 曹先生事件

int main() {
    // 设置随机数种子
    srand(time(0));
    
    cout << "=============================================" << endl;
    cout << "           《骆驼祥子》文字冒险游戏           " << endl;
    cout << "=============================================" << endl;
    cout << "你将扮演祥子,体验旧北京拉车夫的人生历程" << endl;
    cout << "你的目标:拥有自己的车,过上好日子" << endl;
    cout << "按回车键开始游戏..." << endl;
    cin.ignore();
    
    // 初始化游戏
    initGame();
    
    // 游戏主循环
    while (true) {
        cout << "\n=============================================" << endl;
        cout << "                   第 " << player.day << " 天" << endl;
        cout << "=============================================" << endl;
        
        // 显示当前状态
        showStatus();
        
        // 触发虎妞事件(第15-25天随机触发)
        if (!player.married && player.day >= 15 && player.day <= 25 && rand() % 5 == 0) {
            tigerGirlEvent();
        }
        
        // 触发曹先生事件(落魄时更容易触发:士气<50 或 金钱<10)
        if (!player.met_cao && (player.morale < 50 || player.money < 10) && rand() % 4 == 0) {
            mrCaoEvent();
        }
        
        // 日常选择
        dailyChoice();
        
        // 随机事件
        randomEvent();
        
        // 检查游戏结束条件
        checkGameOver();
        
        // 天数加1
        player.day++;
    }
    
    return 0;
}

// 初始化游戏(新增婚姻/遇曹先生状态)
void initGame() {
    player.health = 80;
    player.money = 20;
    player.morale = 90;
    player.has_cart = false;
    player.day = 1;
    player.married = false;
    player.met_cao = false;
}

// 显示当前状态(新增婚姻/遇曹先生状态)
void showStatus() {
    cout << "\n【当前状态】" << endl;
    cout << "健康值:" << player.health << "/100" << endl;
    cout << "金钱:" << player.money << " 铜子" << endl;
    cout << "士气:" << player.morale << "/100" << endl;
    cout << "是否有车:" << (player.has_cart ? "是" : "否(需要租车)") << endl;
    cout << "婚姻状态:" << (player.married ? "已和虎妞结婚" : "单身") << endl;
    cout << "是否遇曹先生:" << (player.met_cao ? "是(有稳定活计)" : "否") << endl;
}

// 每日选择(已婚状态下限制部分选择)
void dailyChoice() {
    int choice;
    cout << "\n【今日行动选择】" << endl;
    
    // 已婚状态下,虎妞会限制出门拉车的次数
    if (player.married && rand() % 3 == 0) {
        cout << "虎妞不让你出去拉车,说在家歇着!" << endl;
        rest(); // 强制休息
        return;
    }
    
    cout << "1. 出去拉车赚钱" << endl;
    cout << "2. 在家休息恢复健康" << endl;
    if (player.money >= CART_PRICE && !player.has_cart) {
        cout << "3. 用所有积蓄买车" << endl;
    }
    cout << "\n请选择(1-2" << (player.money >= CART_PRICE && !player.has_cart ? "3" : "") << "):";
    cin >> choice;
    
    // 输入验证
    while (choice < 1 || choice > 3 || 
           (choice == 3 && (player.money < CART_PRICE || player.has_cart))) {
        cout << "输入无效,请重新选择:";
        cin >> choice;
    }
    
    // 执行选择
    switch (choice) {
        case 1:
            pullCart();
            break;
        case 2:
            rest();
            break;
        case 3:
            buyCart();
            break;
    }
}

// 拉车赚钱(遇曹先生则收入更稳定)
void pullCart() {
    cout << "\n你出门去拉车了..." << endl;
    
    // 基础收入计算(曹先生提供的活计更稳定)
    int base_earn = player.has_cart ? 20 : 12;
    if (player.met_cao) {
        base_earn += 8; // 曹先生的活计多赚钱
        cout << "你去曹先生家拉包月,收入更稳定!" << endl;
    }
    
    int earn = base_earn + (rand() % 10 - 3);   // 随机波动 ±3
    
    // 健康消耗(已婚状态下虎妞会让你少受累)
    int health_cost = player.married ? (3 + rand() % 3) : (5 + rand() % 5);
    player.health -= health_cost;
    
    // 边界处理
    if (player.health < 0) player.health = 0;
    if (earn < 0) earn = 0;
    
    player.money += earn;
    player.morale += (player.met_cao ? 5 : 2);  // 曹先生的活计更提升士气
    if (player.morale > 100) player.morale = 100;
    
    cout << "今天拉车赚了 " << earn << " 个铜子" << endl;
    cout << "身体消耗了 " << health_cost << " 点健康值" << endl;
}

// 休息恢复(已婚状态下恢复更多健康)
void rest() {
    cout << "\n你选择在家休息..." << endl;
    
    // 恢复健康(已婚有虎妞照顾)
    int health_recover = player.married ? (15 + rand() % 10) : (10 + rand() % 10);
    player.health += health_recover;
    
    // 士气变化(已婚则虎妞会唠叨,士气降更多;遇曹先生则士气不降)
    if (player.married) {
        player.morale -= 8;
        cout << "虎妞在一旁唠叨,让你心里烦得很!" << endl;
    } else if (!player.met_cao) {
        player.morale -= 5;
    }
    
    // 边界处理
    if (player.health > 100) player.health = 100;
    if (player.morale < 0) player.morale = 0;
    
    // 休息开销(已婚则开销更大)
    int cost = player.married ? 5 : 3;
    player.money -= cost;
    if (player.money < 0) player.money = 0;
    
    cout << "健康值恢复了 " << health_recover << " 点" << endl;
    cout << "日常开销花了 " << cost << " 个铜子" << endl;
}

// 买车(已婚则虎妞会资助一部分)
void buyCart() {
    int actual_cost = CART_PRICE;
    if (player.married) {
        actual_cost = CART_PRICE - 200; // 虎妞资助200铜子
        cout << "虎妞拿出私房钱资助你,买车能少花200个铜子!" << endl;
    }
    
    cout << "\n你下定决心,买了一辆属于自己的新车!" << endl;
    player.money -= actual_cost;
    player.has_cart = true;
    player.morale += 20;
    if (player.morale > 100) player.morale = 100;
    cout << "现在你终于有了自己的车,士气大振!" << endl;
}

// 虎妞事件
void tigerGirlEvent() {
    cout << "\n=============================================" << endl;
    cout << "                   【虎妞事件】               " << endl;
    cout << "=============================================" << endl;
    cout << "刘四爷的女儿虎妞找到你,她看上了你,逼你和她结婚!" << endl;
    cout << "虎妞说:跟我结婚,我给你钱买车,不用再受这份罪!" << endl;
    cout << "1. 答应结婚(获得资助,但失去自由)" << endl;
    cout << "2. 拒绝结婚(保持自由,但虎妞会报复,士气大跌)" << endl;
    
    int choice;
    cout << "你的选择(1/2):";
    cin >> choice;
    
    while (choice != 1 && choice != 2) {
        cout << "输入无效,请选择1或2:";
        cin >> choice;
    }
    
    if (choice == 1) {
        cout << "\n你答应了和虎妞结婚!" << endl;
        cout << "虎妞给了你150个铜子,还帮你张罗了不少东西!" << endl;
        player.married = true;
        player.money += 150;
        player.morale += 10; // 有钱但不开心
        if (player.morale > 100) player.morale = 100;
    } else {
        cout << "\n你拒绝了虎妞!" << endl;
        cout << "虎妞恼羞成怒,到处说你坏话,没人敢雇你拉车,士气大跌!" << endl;
        player.morale -= 40;
        player.money -= 10; // 几天没拉到活
        if (player.morale < 0) player.morale = 0;
    }
}

// 曹先生事件
void mrCaoEvent() {
    cout << "\n=============================================" << endl;
    cout << "                   【曹先生事件】             " << endl;
    cout << "=============================================" << endl;
    cout << "你遇到了曹先生,他是个好心的教书先生,看你可怜想收留你!" << endl;
    cout << "曹先生说:来我家拉包月吧,虽然钱不多,但稳定,不用风吹日晒!" << endl;
    cout << "1. 答应去曹先生家拉包月(稳定收入,但有被跟踪的风险)" << endl;
    cout << "2. 拒绝(继续自由拉车,但收入不稳定)" << endl;
    
    int choice;
    cout << "你的选择(1/2):";
    cin >> choice;
    
    while (choice != 1 && choice != 2) {
        cout << "输入无效,请选择1或2:";
        cin >> choice;
    }
    
    if (choice == 1) {
        cout << "\n你答应了曹先生!" << endl;
        cout << "曹先生给了你50个铜子定金,你有了稳定的活计,士气大涨!" << endl;
        player.met_cao = true;
        player.money += 50;
        player.morale += 30;
        if (player.morale > 100) player.morale = 100;
    } else {
        cout << "\n你拒绝了曹先生的好意,继续在街上拉散活!" << endl;
        player.morale -= 5;
        if (player.morale < 0) player.morale = 0;
    }
}

// 随机事件(新增曹先生/虎妞相关事件)
void randomEvent() {
    int event_chance = rand() % 10;  // 10%概率触发事件
    
    if (event_chance < 3) {  // 30%概率触发随机事件
        cout << "\n【突发事件】" << endl;
        
        int event_type = rand() % 8; // 新增2个角色相关事件
        switch (event_type) {
            case 0:  // 遇到坏主顾,不给钱还骂人
                cout << "遇到不讲理的主顾,拉了半天车一分钱没拿到,还受了气!" << endl;
                player.morale -= 15;
                break;
            case 1:  // 下雨了,没法拉车
                cout << "突然下起大雨,没法继续拉车,只能早早回家!" << endl;
                player.money -= 2;
                break;
            case 2:  // 遇到好心的主顾,多给了钱
                cout << "遇到好心的主顾,不仅给足了车钱,还多赏了几个铜子!" << endl;
                player.money += 8;
                player.morale += 10;
                break;
            case 3:  // 生病了
                cout << "身体不舒服,生病了,需要花钱买药!" << endl;
                player.health -= 20;
                player.money -= 10;
                player.morale -= 10;
                break;
            case 4:  // 被巡警敲诈
            {
                cout << "遇到巡警敲诈,被抢走了身上的一些钱!" << endl;
                int lose_money = player.money / 5;
                if (lose_money < 1) lose_money = 1;
                player.money -= lose_money;
                player.morale -= 20;
                break;
            }
            case 5:  // 车被抢走(如果有车)
                if (player.has_cart) {
                    cout << "遭遇乱兵,辛苦买来的车被抢走了!这是你的命根子啊!" << endl;
                    player.has_cart = false;
                    player.morale -= 50;
                } else {
                    cout << "看到别的车夫被抢,庆幸自己还好没有车,但也心有余悸。" << endl;
                    player.morale -= 5;
                }
                break;
            case 6:  // 曹先生相关:被跟踪,需要跑路(扣钱)
                if (player.met_cao) {
                    cout << "曹先生被人盯上了,你受牵连需要跑路,花了不少钱!" << endl;
                    player.money -= 30;
                    player.morale -= 15;
                } else {
                    cout << "听说曹先生被人陷害,你暗自庆幸没去他家拉包月。" << endl;
                }
                break;
            case 7:  // 虎妞相关:虎妞吵架/给钱
                if (player.married) {
                    if (rand() % 2 == 0) {
                        cout << "虎妞和你吵架,把你骂了一顿,士气大跌!" << endl;
                        player.morale -= 20;
                    } else {
                        cout << "虎妞心疼你,给了你一些私房钱,让你买点好吃的!" << endl;
                        player.money += 20;
                        player.health += 5;
                    }
                }
                break;
        }
        
        // 边界处理
        if (player.health < 0) player.health = 0;
        if (player.morale < 0) player.morale = 0;
        if (player.morale > 100) player.morale = 100;
        if (player.money < 0) player.money = 0;
    }
}

// 检查游戏结束条件(新增曹先生/虎妞相关结局条件)
void checkGameOver() {
    // 游戏结束条件
    if (player.health <= 0) {
        cout << "\n你的健康值为0,祥子病倒了,游戏结束..." << endl;
        showEnding();
        exit(0);
    }
    
    if (player.morale <= 0) {
        cout << "\n祥子彻底失去了生活的希望,变得麻木不仁,游戏结束..." << endl;
        showEnding();
        exit(0);
    }
    
    if (player.day > 100) {
        cout << "\n100天过去了,游戏结束..." << endl;
        showEnding();
        exit(0);
    }
    
    // 好结局条件:拥有车+(已婚/遇曹先生)+ 金钱>500 + 士气>70 + 健康>60
    if (player.has_cart && (player.married || player.met_cao) && 
        player.money > 500 && player.morale > 70 && player.health > 60 && player.day >= 50) {
        cout << "\n经过不懈的努力,你终于稳定下来,有了自己的车和依靠!" << endl;
        showEnding();
        exit(0);
    }
}

// 显示结局(新增虎妞/曹先生相关结局)
void showEnding() {
    cout << "\n=============================================" << endl;
    cout << "                   游戏结局                   " << endl;
    cout << "=============================================" << endl;
    
    if (player.has_cart && (player.married || player.met_cao) && 
        player.money > 500 && player.morale > 70 && player.health > 60) {
        cout << "\n【好结局:安稳的生活】" << endl;
        if (player.married) {
            cout << "你和虎妞一起过日子,虽然偶尔吵架,但有车有家,也算安稳。" << endl;
        } else {
            cout << "你跟着曹先生拉包月,稳定的收入让你不用再颠沛流离,日子有了盼头。" << endl;
        }
    } else if (player.married && player.has_cart) {
        cout << "\n【普通结局:凑活的日子】" << endl;
        cout << "你和虎妞结婚了,也有了自己的车,但虎妞的唠叨和生活的压力让你喘不过气," << endl;
        cout << "日子凑活过,但再也没有了年轻时的心气。" << endl;
    } else if (player.met_cao && !player.has_cart) {
        cout << "\n【普通结局:安稳但平凡】" << endl;
        cout << "你跟着曹先生拉包月,虽然没有自己的车,但胜在稳定,不用再受街头的苦," << endl;
        cout << "平凡地度过一生,也算个不错的归宿。" << endl;
    } else {
        cout << "\n【悲剧结局:麻木的行尸走肉】" << endl;
        cout << "经历了一次次的打击,你失去了所有的希望和斗志,不再要强," << endl;
        cout << "变成了吃喝嫖赌、麻木不仁的行尸走肉,彻底沦为了城市的垃圾。" << endl;
    }
    
    cout << "\n【最终统计】" << endl;
    cout << "生存天数:" << player.day << " 天" << endl;
    cout << "最终金钱:" << player.money << " 铜子" << endl;
    cout << "最终健康:" << player.health << "/100" << endl;
    cout << "最终士气:" << player.morale << "/100" << endl;
    cout << "是否拥有车:" << (player.has_cart ? "是" : "否") << endl;
    cout << "婚姻状态:" << (player.married ? "已和虎妞结婚" : "单身") << endl;
    cout << "是否遇曹先生:" << (player.met_cao ? "是" : "否") << endl;
    
    cout << "\n感谢游玩《骆驼祥子》文字冒险游戏!" << endl;
}