#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;

// Windows控制台控制
namespace Console {
    void clear() {
        system("cls");
    }

    void gotoxy(int x, int y) {
        COORD coord;
        coord.X = x;
        coord.Y = y;
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }

    void setColor(int color) {
        // 将ANSI颜色代码转换为Windows颜色代码
        int windowsColor;
        switch(color) {
            case 31: windowsColor = 12; break; // 红色
            case 32: windowsColor = 10; break; // 绿色
            case 36: windowsColor = 11; break; // 青色
            default: windowsColor = 7;  break; // 默认白色
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), windowsColor);
    }

    // 重置颜色为默认
    void resetColor() {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    }
}

// 字符画资源 - 使用ASCII字符替代emoji
namespace AsciiArt {
    const string PLAYER = R"(
     O    
    /|\   
    / \   )";

    const string MONSTER = R"(
   [^^]
  /|\/|\
   / \   )";

    const string SWORD = R"(
    =|====>   
    )";

    const string POTION = R"(
    [+]   
    )";

    const string TREASURE = R"(
    [*]
    )";

    const string GAME_OVER = R"(
  _____                         ____                 
 / ____|                       / __ \                
| |  __  __ _ _ __ ___   ___ | |  | |_   _____ _ __ 
| | |_ |/ _` | '_ ` _ \ / _ \| |  | \ \ / / _ \ '__|
| |__| | (_| | | | | | |  __/| |__| |\ V /  __/ |   
 \_____|\__,_|_| |_| |_|\___| \____/  \_/ \___|_|   )";

    const string BATTLE_FRAME = R"(
+------------------------------------------------+
|                                                |
|                                                |
|                                                |
|                                                |
|                                                |
|                                                |
+------------------------------------------------+)";
}

// 玩家类 - 保持不变,只修改显示相关的代码
class Player {
    // ... [保持原有代码不变] ...

    void displayStatus() {
        Console::setColor(32); // 绿色
        cout << "名称: " << name << endl;
        cout << "等级: " << level << endl;
        cout << "生命: " << health << "/" << maxHealth << endl;
        cout << "攻击: " << attack << endl;
        cout << "防御: " << defense << endl;
        cout << "经验: " << experience << endl;
        Console::resetColor();
    }
};

// 怪物类 - 保持不变,只修改显示相关的代码
class Monster {
    // ... [保持原有代码不变] ...

    void displayStatus() {
        Console::setColor(31); // 红色
        cout << "怪物: " << name << endl;
        cout << "生命: " << health << "/" << maxHealth << endl;
        cout << "攻击: " << attack << endl;
        Console::resetColor();
    }
};

// 战斗系统 - 修改显示相关的代码,使用ASCII字符替代emoji
class BattleSystem {
    void displayBattleScreen() {
        Console::clear();
        cout << AsciiArt::BATTLE_FRAME << endl;
        
        Console::gotoxy(2, 3);
        player.displayStatus();

        Console::gotoxy(40, 3);
        monster.displayStatus();

        Console::gotoxy(2, 10);
        cout << "1. [A]攻击  2. [D]防御  3. [P]使用药水  4. [R]逃跑" << endl;
    }

    void showAttackAnimation() {
        for (int i = 0; i < 3; i++) {
            Console::gotoxy(20, 5);
            cout << "==>>"; 
            this_thread::sleep_for(chrono::milliseconds(100));
            Console::gotoxy(20, 5);
            cout << "    ";
            this_thread::sleep_for(chrono::milliseconds(100));
        }
    }

    // ... [其余战斗逻辑保持不变] ...
};

// 主游戏类 - 修改显示相关的代码
class Game {
    void displayMainMenu() {
        Console::clear();
        Console::setColor(36); // 青色
        cout << R"(
    +--------------------------------+
    |         ASCII RPG游戏          |
    +--------------------------------+
    |  1. [E] 探索                   |
    |  2. [S] 查看状态               |
    |  3. [R] 休息                   |
    |  4. [Q] 退出                   |
    +--------------------------------+
        )" << endl;
        Console::resetColor();
    }

    // ... [其余游戏逻辑保持不变] ...
};

int main() {
    // 设置控制台代码页为UTF-8
    SetConsoleOutputCP(65001);
    
    // 设置控制台标题
    SetConsoleTitle(TEXT("ASCII RPG Game"));
    
    // 隐藏光标
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO cursorInfo;
    GetConsoleCursorInfo(consoleHandle, &cursorInfo);
    cursorInfo.bVisible = false;
    SetConsoleCursorInfo(consoleHandle, &cursorInfo);
    
    srand(time(0));
    
    cout << "请输入你的名字: ";
    string playerName;
    getline(cin, playerName);
    
    Game game(playerName);
    game.run();
    
    return 0;
}