#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <conio.h> 
#include <windows.h> 
using namespace std;         

int dx[] = {-1, 1, 0, 0}; // w s a d
int dy[] = {0, 0, -1, 1};
int hhh;
/*  
 1 2
0 * 3
 5 4
*/
// 奇数行
int dx1[] = {0, -1, -1, 0, 1, 1};
int dy1[] = {-1, -1, 0, 1, 0, -1};
// 偶数行
int dx2[] = {0, -1, -1, 0, 1, 1};
int dy2[] = {-1, 0, 1, 1, 1, 0};

int board[100][100]; // 游戏地图 0:空 1:路障 2:皮皮
int px, py;        // 皮皮所在的位置
int cx, cy;        // 光标所在的位置
int dis[100][100];   // 到该点的最短长度
int minn = 1e9;    // 逃出最短长度
int choice = -1;   // 逃出方向
int rd = 0;        // 回合数

//int getch() // 不回显函数
//{
//    struct termios nts, ots;
//    if (tcgetattr(0, &ots) < 0) return EOF;
//    nts = ots;
//    cfmakeraw(&nts); 
//    if (tcsetattr(0, TCSANOW, &nts) < 0) return EOF;
//    int cr;
//    cr = getchar();
//    if (tcsetattr(0, TCSANOW, &ots) < 0)  return EOF;
//    return cr;
//}

bool in(int x, int y) // 判断(x, y)是否越界
{
    return 1 <= x && x <= 11 && 1 <= y && y <= 11;
}

void drawBoard() // 绘制游戏地图
{
    cout << "\033c" << flush;
    for (int i = 1; i <= 11; i++)
    {
        cout << "    ";
        if (i % 2 == 0) cout << " ";
        for (int j = 1; j <= 11; j++)
        {
            if (i == cx && j == cy && board[i][j] == 0) // 空白光标
                cout << "☆ ";
            else if (i == cx && j == cy && board[i][j] == 1){ // 黑色光标
            	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
                cout << "☆ ";
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
            }
            else if (board[i][j] == 2) {// 皮皮
            	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
                cout << " P ";
            	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
        	}
            else if (board[i][j] == 1) // 路障
                cout << "● ";
            else // 空白
                cout << "○ ";   
        }
        cout << endl;
    }
}

void init()
{
    srand(time(0));
    // 初始化皮皮、光标位置
    px = 6, py = 6;
    cx = 1, cy = 1;
    board[px][py] = 2;
    rd = 0;
    // 放置8个路障
    for (int i = 1; i <= 15; i++)
    {
        int r = rand() % 121;
        int row = r / 11 + 1; 
        int col = r % 11 + 1;
        while (board[row][col] != 0)
        {
            r = rand() % 121;
            row = r / 11 + 1; 
            col = r % 11 + 1;
        }
        board[row][col] = 1;
    }
}

void moveCursor(int dir) // 移动光标 w s a d
{
    int nx = cx + dx[dir];
    int ny = cy + dy[dir];
    if (in(nx, ny))
    {
        cx = nx;
        cy = ny;
    }
}

bool mark(int x, int y) // 放置路障,返回是否成功放置
{
    if (board[x][y] == 0)
    {
        board[x][y] = 1;
        return true;
    }
    return false;
}

void dfs(int x, int y, int step, int col)
{
    if (step > minn) return; // 剪枝
    if (x == 1 || x == 11 || y == 1 || y == 11) // 到边
    {
        if (dis[x][y] < minn)
        {
            minn = dis[x][y];
            choice = col;
        }
        return;
    }
    for (int i = 0; i < 6; i++) // 枚举6个方向递归
    {
        int nx, ny;
        if (x % 2 == 1) // 奇数行
        {
            nx = x + dx1[i];
            ny = y + dy1[i];
        }
        else // 偶数行
        {
            nx = x + dx2[i];
            ny = y + dy2[i];
        }
        if (in(nx, ny) && board[nx][ny] == 0 && step + 1 < dis[nx][ny])
        {
            // 记录步数、来向
            dis[nx][ny] = step+1;
            if (step == 0) dfs(nx, ny, step+1, i);
            else dfs(nx, ny, step+1, col);
        }
    }
}

int pipiMove() // 皮皮移动,返回皮皮的去向,-1 表示不动
{
    // 初始化
    memset(dis, 0x3f, sizeof(dis));
    minn = 1e9, choice = -1;
    // 标记起点
    dis[px][py] = 0;
    dfs(px, py, 0, -1);
    if (choice == -1) // 不动
    {
        return -1;
    }
    int nx, ny;
    if (px % 2 == 1) // 奇数行
    {
        nx = px + dx1[choice];
        ny = py + dy1[choice];
    }
    else // 偶数行
    {
        nx = px + dx2[choice];
        ny = py + dy2[choice];
    }
    // 皮皮移动
    board[px][py] = 0; 
    board[nx][ny] = 2; 
    px = nx;
    py = ny;
    return choice;
}

void intro() // 游戏规则展示
{
    cout << "***********************************************************" << endl;
    cout << "    欢迎来到【小P哪里逃】游戏~" << endl;
    cout << "    在这个游戏里会有一个 11 * 11 的游戏地图" << endl;
    cout << "    按下w↑、a←、s↓、d→ 控制光标☆ 移动,按下 空格键 放置路障" << endl;
    cout << "    每次皮皮会向相邻的六个方向逃跑,想办法困住小P!不要让小P逃出去!" << endl; 
    cout << "    点击 任意键 开始游戏" << endl;
    cout << "***********************************************************" << endl;
    getch();
//    cout << "***********************************************************" << endl;
//    cout << "    请输入"     
}

void game() // 游戏
{
    intro();
    while (true)
    {
        drawBoard();
        char ch = getch(); // 不回显函数,当用户按下某个字符时,函数自动读取,无需按回车
        if (ch == 'w') moveCursor(0);
        else if (ch == 's') moveCursor(1);
        else if (ch == 'a') moveCursor(2);
        else if (ch == 'd') moveCursor(3);
        else if (ch == '0') exit(0);
        else if (ch == ' ') 
        {
            if (!mark(cx, cy)) // 放置路障
            {
                continue;
            }
            rd++;
            drawBoard();
            if (pipiMove() != -1)
            {
                drawBoard();
            }
            else
            {
                return;
            }
        }
    }
}

void result() // 游戏结果
{
    cout << "***********************************************************" << endl;
    if (px == 1 || px == 11 || py == 1 || py == 11) // 逃出
    {
        cout << "小P : 嘿嘿嘿~我逃出来了!" << endl;
        cout << "游戏失败!" << endl;
    }
    else
    {
        cout << "小P : 呜呜呜……我被困住了!救命呀?? ~~~" << endl;
        cout << "游戏获胜!" << endl;
        cout << "步数: " << rd << endl;
    }
    cout << "***********************************************************" << endl;
}

int main()
{
    // 初始化
    init(); 

    // 开始游戏  
    game();

    // 游戏结果
    result();
    return 0;
}