// 赌徒狂欢2.0 - 修复黑屏版
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;

// ========== 游戏工具类 ==========
namespace GameTools {
    // 颜色常量
    const int BLACK = 0;
    const int BLUE = 1;
    const int GREEN = 2;
    const int CYAN = 3;
    const int RED = 4;
    const int MAGENTA = 5;
    const int YELLOW = 6;
    const int WHITE = 7;
    const int GRAY = 8;
    const int LIGHT_BLUE = 9;
    const int LIGHT_GREEN = 10;
    const int LIGHT_CYAN = 11;
    const int LIGHT_RED = 12;
    const int LIGHT_MAGENTA = 13;
    const int LIGHT_YELLOW = 14;
    const int BRIGHT_WHITE = 15;

    // 设置控制台颜色
    void setColor(int color) {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
    }

    // 隐藏光标
    void hideCursor() {
        CONSOLE_CURSOR_INFO cursorInfo;
        cursorInfo.bVisible = FALSE;
        cursorInfo.dwSize = 1;
        SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo);
    }

    // 移动光标
    void moveCursor(int row, int col) {
        COORD pos;
        pos.X = (SHORT)col;
        pos.Y = (SHORT)row;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    }

    // 清屏
    void clearScreen() {
        system("cls");
    }

    // 延迟函数
    void delay(int ms) {
        Sleep(ms);
    }

    // 渐变输出文字
    void printWithEffect(const string& text, int delayTime = 30) {
        for (size_t i = 0; i < text.size(); ++i) {
            char c = text[i];
            cout << c;
            cout.flush();
            delay(delayTime);
        }
    }
}

using namespace GameTools;

// ========== 游戏全局变量 ==========
long long money = 200;          // 初始资金
int holyWater = 0;              // 圣水数量
bool developerMode = false;     // 开发者模式
bool firstTimePlay = true;      // 首次游玩标记
string devPassword = "mod2025"; // 开发者密码

// ========== 游戏函数声明 ==========
void initConsole();
void showTitle();
void showMainMenu();
void playLottery();
void watchAd();
void playSlotMachine();
void enterBar();
void settingsMenu();
void developerMenu();
void checkGameStatus();

// ========== 广告文本数组 ==========
const char* adTexts[] = {
    "Mod牌笔记 - 学习好助手,你值得拥有!",
    "还在为学习烦恼吗?试试Mod牌笔记!",
    "智能记笔记,高效学习不是梦!",
    "上万用户的选择,品质保证!",
    "咨询热线: 445-2753-1756",
    "\n广告结束,获得15元奖励!"
};
const int adTextsCount = 6;

// ========== 初始化控制台 ==========
void initConsole() {
    // 设置控制台标题
    SetConsoleTitle("赌徒狂欢 2.0");
    
    // 设置控制台窗口大小
    system("mode con cols=60 lines=30");
    
    // 设置控制台编码(先尝试GBK,再尝试UTF-8)
    system("chcp 936 > nul");
    
    // 隐藏光标
    hideCursor();
    
    // 设置白色文本(解决黑屏)
    setColor(WHITE);
    
    // 清屏
    clearScreen();
}

// ========== 主要游戏函数 ==========

// 显示游戏标题
void showTitle() {
    clearScreen();
    setColor(LIGHT_CYAN);
    cout << "==========================================" << endl;
    cout << "          赌徒狂欢 2.0" << endl;
    cout << "==========================================" << endl;
    setColor(WHITE);
    
    if (firstTimePlay) {
        cout << "\n";
        printWithEffect("欢迎来到赌徒狂欢!\n");
        printWithEffect("初始资金:200元\n");
        printWithEffect("目标:成为亿万富翁(达到1亿)!\n");
        firstTimePlay = false;
    }
    delay(1500);
}

// 显示主菜单
void showMainMenu() {
    while (money > 0) {
        clearScreen();
        setColor(LIGHT_YELLOW);
        cout << "========================================" << endl;
        cout << "           主菜单 - 资金: " << money << "元" << endl;
        cout << "========================================" << endl;
        setColor(WHITE);
        
        if (holyWater > 0) {
            setColor(LIGHT_CYAN);
            cout << "圣水: " << holyWater << "瓶" << endl;
            setColor(WHITE);
        }
        
        cout << "\n请选择游戏项目:" << endl;
        cout << "1. 彩票 (3元/次)" << endl;
        cout << "2. 看广告 (免费)" << endl;
        cout << "3. 老虎机 (15元/次)" << endl;
        cout << "4. 酒吧 (200元/次)" << endl;
        cout << "5. 设置" << endl;
        cout << "6. 退出游戏" << endl;
        cout << "\n输入选项 (1-6): ";
        
        char choice;
        cin >> choice;
        
        // 清空输入缓冲区
        cin.ignore(1000, '\n');
        
        switch (choice) {
            case '1': playLottery(); break;
            case '2': watchAd(); break;
            case '3': playSlotMachine(); break;
            case '4': enterBar(); break;
            case '5': settingsMenu(); break;
            case '6': 
                clearScreen();
                printWithEffect("感谢游玩!再见!\n");
                delay(1000);
                exit(0);
            default:
                cout << "无效选项,请重新选择!\n";
                delay(1000);
        }
        
        checkGameStatus();
    }
}

// 彩票游戏
void playLottery() {
    clearScreen();
    setColor(LIGHT_GREEN);
    cout << "====== 彩票游戏 ======" << endl;
    setColor(WHITE);
    
    if (money < 3) {
        cout << "资金不足!需要3元。" << endl;
        delay(1000);
        return;
    }
    
    money -= 3;
    int prize = 1;
    bool allCorrect = true;
    
    for (int i = 1; i <= 6; i++) {
        cout << "第" << i << "位:请输入0-30的数字: ";
        int playerNum, lotteryNum;
        cin >> playerNum;
        
        // 验证输入
        if (playerNum < 0 || playerNum > 30) {
            cout << "输入无效,请重新开始!" << endl;
            delay(1000);
            money += 3;
            return;
        }
        
        lotteryNum = rand() % 31;
        cout << "开奖数字: " << lotteryNum << endl;
        
        if (playerNum == lotteryNum) {
            prize *= 100;
            setColor(LIGHT_GREEN);
            cout << "第" << i << "位匹配成功!" << endl;
            setColor(WHITE);
        } else {
            allCorrect = false;
            setColor(LIGHT_RED);
            cout << "第" << i << "位匹配失败" << endl;
            setColor(WHITE);
            break;
        }
        
        delay(500);
    }
    
    if (allCorrect) {
        setColor(LIGHT_YELLOW);
        cout << "\n恭喜!全部匹配!" << endl;
        cout << "获得奖金: " << prize << "元" << endl;
        setColor(WHITE);
    } else if (prize > 1) {
        cout << "\n获得奖金: " << prize << "元" << endl;
    } else {
        cout << "\n很遗憾,没有中奖" << endl;
    }
    
    money += prize;
    cout << "\n按Enter键继续...";
    cin.ignore();
    cin.get();
}

// 看广告
void watchAd() {
    clearScreen();
    setColor(LIGHT_CYAN);
    cout << "正在播放广告..." << endl << endl;
    setColor(WHITE);
    
    for (int i = 0; i < adTextsCount; i++) {
        printWithEffect(adTexts[i]);
        cout << endl;
        delay(800);
    }
    
    money += 15;
    cout << "\n当前资金: " << money << "元" << endl;
    cout << "\n按Enter键继续...";
    cin.ignore();
    cin.get();
}

// 老虎机
void playSlotMachine() {
    clearScreen();
    setColor(LIGHT_MAGENTA);
    cout << "====== 老虎机 ======" << endl;
    setColor(WHITE);
    
    if (money < 15) {
        cout << "资金不足!需要15元。" << endl;
        delay(1000);
        return;
    }
    
    money -= 15;
    
    cout << "按下Enter键开始旋转...";
    cin.ignore();
    cin.get();
    
    clearScreen();
    cout << "老虎机旋转中..." << endl;
    
    // 模拟旋转效果
    for (int i = 0; i < 10; i++) {
        cout << "\r[" << rand() % 10 << "][" << rand() % 10 << "][" << rand() % 10 << "]";
        cout.flush();
        delay(100);
    }
    
    int num1 = rand() % 10;
    int num2 = rand() % 10;
    int num3 = rand() % 10;
    
    cout << "\r[" << num1 << "][" << num2 << "][" << num3 << "]" << endl;
    
    int prize = 0;
    
    // 检查中奖条件
    if (num1 == num2 && num2 == num3) {
        // 三个相同 - 大奖
        prize = 1000 * (num1 + 1);
        setColor(LIGHT_YELLOW);
        cout << "大奖!三个数字相同!" << endl;
    } else if (num1 == num2 || num2 == num3 || num1 == num3) {
        // 两个相同
        prize = 100 * ((num1 + num2 + num3) / 3);
        setColor(LIGHT_GREEN);
        cout << "二等奖!两个数字相同!" << endl;
    } else if ((num2 == num1 + 1 && num3 == num2 + 1) || 
               (num2 == num1 - 1 && num3 == num2 - 1)) {
        // 顺子
        prize = 500;
        setColor(LIGHT_CYAN);
        cout << "顺子奖!" << endl;
    } else {
        setColor(LIGHT_RED);
        cout << "很遗憾,没有中奖" << endl;
    }
    
    setColor(WHITE);
    if (prize > 0) {
        cout << "获得奖金: " << prize << "元" << endl;
        money += prize;
    }
    
    cout << "\n按Enter键继续...";
    cin.ignore();
    cin.get();
}

// 酒吧
void enterBar() {
    clearScreen();
    setColor(LIGHT_BLUE);
    cout << "====== 神秘酒吧 ======" << endl;
    setColor(WHITE);
    
    if (money < 200) {
        cout << "资金不足!需要200元进入酒吧。" << endl;
        delay(1000);
        return;
    }
    
    money -= 200;
    cout << "欢迎来到神秘酒吧!" << endl;
    cout << "当前资金: " << money << "元" << endl;
    cout << "圣水: " << holyWater << "瓶" << endl << endl;
    
    bool inBar = true;
    while (inBar && money >= 0) {
        cout << "\n请选择饮品:" << endl;
        cout << "1. 黄金酒 - 500元(资金×10)" << endl;
        cout << "2. 生死酒 - 免费(50%死亡,50%资金×10000)" << endl;
        cout << "3. 圣水 - 200元(获得1瓶圣水)" << endl;
        cout << "4. 可乐 - 20元(获得1-500元随机)" << endl;
        cout << "5. 离开酒吧" << endl;
        cout << "选择 (1-5): ";
        
        char choice;
        cin >> choice;
        
        switch (choice) {
            case '1': // 黄金酒
                if (money >= 500) {
                    money -= 500;
                    money *= 10;
                    setColor(LIGHT_YELLOW);
                    cout << "金光闪闪!资金翻10倍!" << endl;
                    setColor(WHITE);
                    inBar = false;
                } else {
                    cout << "资金不足!" << endl;
                }
                break;
                
            case '2': // 生死酒
                cout << "你喝下了生死酒..." << endl;
                delay(1000);
                
                if (rand() % 2 == 0) {
                    // 死亡
                    if (holyWater > 0) {
                        holyWater--;
                        setColor(LIGHT_CYAN);
                        cout << "圣水庇护了你!免于死亡!" << endl;
                        setColor(WHITE);
                    } else {
                        clearScreen();
                        setColor(LIGHT_RED);
                        cout << "================================" << endl;
                        cout << "        游戏结束" << endl;
                        cout << "    你喝下了致命的毒酒" << endl;
                        cout << "      达成结局:死亡" << endl;
                        cout << "================================" << endl;
                        setColor(WHITE);
                        cout << "\n按Enter键退出...";
                        cin.ignore();
                        cin.get();
                        exit(0);
                    }
                } else {
                    // 生存并获得巨额奖励
                    money *= 10000;
                    setColor(LIGHT_GREEN);
                    cout << "你活下来了!并获得巨额财富!" << endl;
                    setColor(WHITE);
                    inBar = false;
                }
                break;
                
            case '3': // 圣水
                if (money >= 200) {
                    money -= 200;
                    holyWater++;
                    setColor(LIGHT_CYAN);
                    cout << "获得1瓶圣水!" << endl;
                    setColor(WHITE);
                } else {
                    cout << "资金不足!" << endl;
                }
                break;
                
            case '4': // 可乐
                if (money >= 20) {
                    money -= 20;
                    int prize = rand() % 500 + 1;
                    cout << "瓶盖上写着:中奖" << prize << "元!" << endl;
                    money += prize;
                } else {
                    cout << "资金不足!" << endl;
                }
                break;
                
            case '5': // 离开
                inBar = false;
                break;
                
            default:
                cout << "无效选择!" << endl;
        }
        
        if (inBar) {
            cout << "\n当前资金: " << money << "元" << endl;
            cout << "圣水: " << holyWater << "瓶" << endl;
        }
    }
    
    cout << "\n按Enter键继续...";
    cin.ignore();
    cin.get();
}

// 设置菜单
void settingsMenu() {
    clearScreen();
    setColor(LIGHT_GREEN);
    cout << "================================" << endl;
    cout << "           设置" << endl;
    cout << "================================" << endl;
    setColor(WHITE);
    
    while (true) {
        cout << "\n请选择:" << endl;
        cout << "1. 关于游戏" << endl;
        cout << "2. 版本信息" << endl;
        cout << "3. 开发者模式" << endl;
        cout << "4. 返回主菜单" << endl;
        cout << "选择 (1-4): ";
        
        char choice;
        cin >> choice;
        
        switch (choice) {
            case '1': // 关于游戏
                clearScreen();
                setColor(CYAN);
                cout << "================================" << endl;
                cout << "          关于游戏" << endl;
                cout << "================================" << endl;
                setColor(WHITE);
                cout << "\n游戏名称:赌徒狂欢 2.0" << endl;
                cout << "制作团队:Mod笔谈" << endl;
                cout << "主要开发者:莫晗" << endl;
                cout << "技术支持:林培奇、黄嘉赢" << endl;
                cout << "灵感来源:G币人生、赌神模拟器" << endl;
                cout << "\n游戏目标:从200元开始,通过合理的投资和" << endl;
                cout << "          赌博策略,赚取1亿元!" << endl;
                cout << "\n提示:圣水可以拯救你一次生命!" << endl;
                cout << "\n按Enter键继续...";
                cin.ignore();
                cin.get();
                break;
                
            case '2': // 版本信息
                clearScreen();
                cout << "版本:赌徒狂欢 2.0" << endl;
                cout << "更新日期:2024年" << endl;
                cout << "主要优化:" << endl;
                cout << "  - 代码结构重构" << endl;
                cout << "  - 添加颜色系统" << endl;
                cout << "  - 改进用户界面" << endl;
                cout << "  - 修复已知问题" << endl;
                cout << "\n按Enter键继续...";
                cin.ignore();
                cin.get();
                break;
                
            case '3': // 开发者模式
                clearScreen();
                if (!developerMode) {
                    cout << "请输入开发者密码: ";
                    string inputPwd;
                    cin >> inputPwd;
                    
                    if (inputPwd == devPassword) {
                        developerMode = true;
                        cout << "开发者模式已开启!" << endl;
                    } else {
                        cout << "密码错误!" << endl;
                    }
                } else {
                    developerMenu();
                }
                delay(1000);
                break;
                
            case '4': // 返回
                return;
                
            default:
                cout << "无效选择!" << endl;
        }
        
        clearScreen();
        setColor(LIGHT_GREEN);
        cout << "================================" << endl;
        cout << "           设置" << endl;
        cout << "================================" << endl;
        setColor(WHITE);
    }
}

// 开发者菜单
void developerMenu() {
    clearScreen();
    setColor(LIGHT_RED);
    cout << "================================" << endl;
    cout << "       开发者模式" << endl;
    cout << "================================" << endl;
    setColor(WHITE);
    
    while (true) {
        cout << "\n当前状态:" << endl;
        cout << "资金: " << money << "元" << endl;
        cout << "圣水: " << holyWater << "瓶" << endl;
        
        cout << "\n开发者选项:" << endl;
        cout << "1. 修改资金" << endl;
        cout << "2. 添加圣水" << endl;
        cout << "3. 直接获胜(1亿元)" << endl;
        cout << "4. 关闭开发者模式" << endl;
        cout << "5. 返回设置菜单" << endl;
        cout << "选择 (1-5): ";
        
        char choice;
        cin >> choice;
        
        switch (choice) {
            case '1': // 修改资金
                cout << "输入新的资金数额: ";
                cin >> money;
                cout << "资金已修改为: " << money << "元" << endl;
                break;
                
            case '2': // 添加圣水
                holyWater++;
                cout << "圣水+1,当前: " << holyWater << "瓶" << endl;
                break;
                
            case '3': // 直接获胜
                money = 100000000;
                cout << "已获得1亿元!" << endl;
                break;
                
            case '4': // 关闭开发者模式
                developerMode = false;
                cout << "开发者模式已关闭" << endl;
                delay(1000);
                return;
                
            case '5': // 返回
                return;
                
            default:
                cout << "无效选择!" << endl;
        }
        
        delay(1000);
        clearScreen();
        setColor(LIGHT_RED);
        cout << "================================" << endl;
        cout << "       开发者模式" << endl;
        cout << "================================" << endl;
        setColor(WHITE);
    }
}

// 检查游戏状态
void checkGameStatus() {
    if (money <= 0) {
        clearScreen();
        setColor(LIGHT_RED);
        cout << "================================" << endl;
        cout << "        游戏结束" << endl;
        cout << "       你破产了!" << endl;
        cout << "  记得下载国家反诈中心APP" << endl;
        cout << "    达成结局:破产" << endl;
        cout << "================================" << endl;
        setColor(WHITE);
        cout << "\n按Enter键退出...";
        cin.ignore();
        cin.get();
        exit(0);
    }
    
    if (money >= 100000000) {
        clearScreen();
        setColor(LIGHT_YELLOW);
        cout << "================================" << endl;
        cout << "        恭喜你!" << endl;
        cout << "      成为亿万富翁!" << endl;
        cout << "    达成结局:亿万富翁" << endl;
        cout << "      感谢你的游玩!" << endl;
        cout << "================================" << endl;
        setColor(WHITE);
        cout << "\n按Enter键退出...";
        cin.ignore();
        cin.get();
        exit(0);
    }
}

// ========== 主函数 ==========
int main() {
    // 初始化随机种子
    srand(time(NULL));
    
    // 初始化控制台
    initConsole();
    
    // 显示标题
    showTitle();
    
    // 进入主菜单
    showMainMenu();
    
    return 0;
}