#include <iostream>
#include <conio.h>
#include <windows.h>
#include <string>
using namespace std;

const int MAP_WIDTH = 50;
const int MAP_HEIGHT = 20;

char map[MAP_HEIGHT][MAP_WIDTH] = {
    "############################################",
    "#                                          #",
    "#  @                                       #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "#                                          #",
    "############################################"
};

void printMap(int x1, int y1, int x2, int y2) {
    system("cls");
    for (int i = 0; i < MAP_HEIGHT; i++) {
        for (int j = 0; j < MAP_WIDTH; j++) {
            if (i == y1 && j == x1) {
                cout << "O"; // 逃生者
            } else if (i == y2 && j == x2) {
                cout << "@"; // 追捕者
            } else {
                cout << map[i][j];
            }
        }
        cout << endl;
    }
}

bool checkCollision(int x1, int y1, int x2, int y2) {
    return (x1 == x2 && y1 == y2);
}

int main() {
    int x1 = 2, y1 = 1; // 逃生者初始位置
    int x2 = 2, y2 = 1; // 追捕者初始位置
    bool gameOver = false;

    while (!gameOver) {
        printMap(x1, y1, x2, y2);

        if (_kbhit()) {
            char key = _getch();
            switch (key) {
                case 'w': // 上
                    if (y1 > 1) y1--;
                    break;
                case 's': // 下
                    if (y1 < MAP_HEIGHT - 2) y1++;
                    break;
                case 'a': // 左
                    if (x1 > 1) x1--;
                    break;
                case 'd': // 右
                    if (x1 < MAP_WIDTH - 2) x1++;
                    break;
            }
        }

        // 追捕者随机移动
        Sleep(500); // 延迟
        int direction = rand() % 4;
        switch (direction) {
            case 0: // 上
                if (y2 > 1) y2--;
                break;
            case 1: // 下
                if (y2 < MAP_HEIGHT - 2) y2++;
                break;
            case 2: // 左
                if (x2 > 1) x2--;
                break;
            case 3: // 右
                if (x2 < MAP_WIDTH - 2) x2++;
                break;
        }

        if (checkCollision(x1, y1, x2, y2)) {
            cout << "游戏结束!追捕者抓到了逃生者!" << endl;
            gameOver = true;
        }
    }

    return 0;
}