#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include<windows.h>
#include<time.h>
using namespace std;
//--------隐藏光标-----------
void HideCursor() 
{ 
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); 
}
//--------gotoxy函数-----------
void gotoxy(int x,int y)//注意:x为列,y为行
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle,pos);
}

// 迷宫尺寸
#define SIZE Size
int SIZE;

// 符号定义
#define WALL "■"
#define PATH "  "
#define PLAYER "●"
#define EXIT "▲"
#define FOOD "★"
// 全局变量
string maze[100][100];
int playerX, playerY;
int exitX, exitY;
int tim[100],start,myend,food,tims;
// 初始化迷宫(递归回溯算法)
void generateMaze(int x, int y) {
    // 四个方向
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
    
    // 打乱方向顺序
    for(int i=0; i<4; ++i){
        int r = rand() % 4;
        swap(dir[i], dir[r]);
    }

    maze[y][x] = PATH;
    
    for(int i=0; i<4; ++i){
        int dx = x + dir[i][0]*2;
        int dy = y + dir[i][1]*2;
        
        if(dx>0 && dx<SIZE-1 && dy>0 && dy<SIZE-1 && maze[dy][dx]==WALL){
            maze[y+dir[i][1]][x+dir[i][0]] = PATH;
            generateMaze(dx, dy);
        }
    }
}

// 初始化游戏
void initGame() {
    srand(time(0));
    
    // 初始化全墙
    for(int y=0; y<SIZE; y++)
        for(int x=0; x<SIZE; x++)
            maze[y][x] = WALL;
    
    // 生成迷宫
    generateMaze(1, 1);
    
    // 设置玩家和出口
    playerX = 1;
    playerY = 1;
    exitX = SIZE-2;
    exitY = SIZE-2;
    maze[playerY][playerX] = PLAYER;
    maze[exitY][exitX] = EXIT;
    
    //设置食物 
    for(int y=0; y<SIZE; ++y) {
        for(int x=0; x<SIZE; ++x) {
        	if(maze[y][x] == PATH)
        	{
	        	int b=rand()%20;
				switch(b)
				{
					case 0:
						maze[y][x]=FOOD;
						break;
					default:break;
				 }
			}
        	 
        }
    }
}

// 显示迷宫
void display() {
    gotoxy(0,0);
    cout<<"时间:"<<tim[tims]<<"秒    \n";
    int cnt;
    for(int y=0; y<SIZE; ++y) {
        for(int x=0; x<SIZE; ++x) {
            if(maze[y][x] == PLAYER) 
                cout << "\033[36m" << PLAYER << "\033[0m";
            else if(maze[y][x] == EXIT)
                cout << "\033[32m" << EXIT << "\033[0m";
            else if(maze[y][x] == FOOD)
            	cout << "\033[33m" << FOOD << "\033[0m";
			else
				cout << maze[y][x];
            	
        }
        cout << "\n";
    }
}

// 处理移动
bool movePlayer(int dx, int dy) {
    int newX = playerX + dx;
    int newY = playerY + dy;
    
    if(newX >=0 && newX < SIZE && 
       newY >=0 && newY < SIZE &&
       maze[newY][newX] != WALL) {
        if(maze[newY][newX] == FOOD)
        	food++;
        maze[playerY][playerX] = PATH;
        playerX = newX;
        playerY = newY;
        
        if(playerX == exitX && playerY == exitY) {
            maze[playerY][playerX] = EXIT;
            return true;
        }
        
        maze[playerY][playerX] = PLAYER;
    }
    return false;
}

int main() {
    
    system("cls");
    tims++;
    cout<<"欢迎玩迷宫游戏,你";
    if(tims==1)cout<<"无最佳纪录\n"; 
    else
    {
	   	int ans=10000;
	    for(int i=1;i<=tims;i++)
	    {
	    	if(tim[i]!=0)
	    	ans=min(ans,tim[i]);
		}
		cout<<"的最佳纪录为:"<<ans<<"秒\n";
	}
    
    Sleep(1000);
	cout<<"游戏规则:\n"; 
    cout << "使用 WASD 移动,Q 退出\n";
    cout << "吃到黄色食物\033[33m" <<FOOD<< "\033[0m可以减少时间\n"; 
    cout << "找到绿色出口\033[32m" <<EXIT<< "\033[0m 获胜!\n";
    Sleep(2000);
    cout<<"准备好了吗?";
	system("pause");
	system("cls");
	SIZE=25;
	initGame();
	start=clock();
	food=0;
    while(true) {
    	myend=clock();
    	tim[tims]=(myend-start)/1000-food*2;
        display();
        HideCursor();
        // 获取输入
        int dx=0, dy=0;
        if(_kbhit())
        {
	        char input = _getch();
	        
	        dx=0;dy=0;
	        switch(tolower(input)) {
	            case 'w': dy = -1; break;
	            case 's': dy = 1; break;
	            case 'a': dx = -1; break;
	            case 'd': dx = 1; break;
	            case 'q': main();
	            default: continue;
	        }
		}
        
        
        if(movePlayer(dx, dy)) {
            display();
            cout << "\n\033[32m胜利!你找到了出口!\033[0m\n";
            system("pause");
            main();
        }
    }
    
    return 0;
}