Загрузка данных


#include <iostream>
#include <vector>
#include <string>
#include <conio.h>
#include <windows.h>

using namespace std;

const char WALL   = '#';
const char PLAYER = '@';
const char EXIT_C = 'E';

vector<string> maze = {
    "####################",
    "#   #    #     #   #",
    "# # # ## # ### # # #",
    "# #   #  #   # # # #",
    "# ##### ##### # # # #",
    "#       #   # # #   #",
    "####### # # # # ### #",
    "#     # # # #   # # #",
    "# ### # # ####### # #",
    "# #   #       #   # #",
    "# # ######### # ### #",
    "#   #         #     E",
    "####################"
};

int playerX = 1, playerY = 1;

// Устанавливаем курсор в нужную позицию
void setCursor(int x, int y) {
    COORD coord = { (SHORT)x, (SHORT)y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

// Скрываем курсор
void hideCursor() {
    CONSOLE_CURSOR_INFO info = { 1, FALSE };
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
}

// Перерисовываем только изменившиеся клетки
void drawCell(int x, int y, char ch) {
    setCursor(x, y + 3); // +3 — смещение под заголовок
    if (ch == WALL)
        cout << '#';
    else if (ch == EXIT_C)
        cout << 'E';
    else if (ch == PLAYER)
        cout << '@';
    else
        cout << ' ';
}

void drawFull() {
    setCursor(0, 0);
    // Заголовок
    cout << "=== ЛАБИРИНТ ===" << endl;
    cout << "Управление: W A S D  |  Выход из игры: Q" << endl;
    cout << "Цель: добраться до [E]" << endl;

    for (int y = 0; y < (int)maze.size(); y++) {
        for (int x = 0; x < (int)maze[y].size(); x++) {
            if (x == playerX && y == playerY)
                cout << PLAYER;
            else
                cout << maze[y][x];
        }
        cout << "\n";
    }
}

bool canMove(int x, int y) {
    if (y < 0 || y >= (int)maze.size()) return false;
    if (x < 0 || x >= (int)maze[y].size()) return false;
    return maze[y][x] != WALL;
}

int main() {
    // Устанавливаем кодировку UTF-8 для русского языка
    SetConsoleOutputCP(65001);
    SetConsoleCP(65001);

    hideCursor();
    system("cls");
    drawFull();

    while (true) {
        if (maze[playerY][playerX] == EXIT_C) {
            setCursor(0, (int)maze.size() + 4);
            cout << "\n  *** ПОЗДРАВЛЯЕМ! Вы нашли выход! ***" << endl;
            break;
        }

        char key = _getch();

        int nx = playerX, ny = playerY;

        if      (key == 'w' || key == 'W') ny--;
        else if (key == 's' || key == 'S') ny++;
        else if (key == 'a' || key == 'A') nx--;
        else if (key == 'd' || key == 'D') nx++;
        else if (key == 'q' || key == 'Q') break;

        if (canMove(nx, ny)) {
            // Стираем старую позицию
            drawCell(playerX, playerY, maze[playerY][playerX]);
            playerX = nx;
            playerY = ny;
            // Рисуем новую позицию
            drawCell(playerX, playerY, PLAYER);
        }
    }

    return 0;
}