Ошибка в консольной игре C++

только начал изучать C++ и решил сделать простенькую игру. И у меня появилась проблема в том, что когда я добавляю предмет больше одного символа, то стена двигается. Еще не работает появление предмета в случайном месте. введите сюда описание изображения

#include <iostream>
#include <conio.h>

using namespace std;

const int height = 10;
const int width = 10;

int x = width / 2;
int y = height / 2;

int coinX = rand() % width;
int coinY = rand() % height;

enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN} dir;

void setcur(int x, int y) // чтобы консоль не мигала
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
};

void input() {         // управление
    if (_kbhit()) {
        switch (_getch()) {
        case 'a':
            dir = LEFT;
            break;
        case 'd':
            dir = RIGHT;
            break;
        case 'w':
            dir = UP;
            break;
        case 's':
            dir = DOWN;
            break;
        }
    }
}

void logic() {
    switch (dir)
    {
    case LEFT:
        if (x < 1) {
            dir = STOP;
        }
        else {
            x--;
            dir = STOP;
        }
        break;
    case RIGHT:
        if (x > width - 3) {
            dir = STOP;
        }
        else {
            x++;
            dir = STOP;
        }
        break;
    case UP:
        if (y < 1) {
            dir = STOP;
        }
        else {
            y--;
            dir = STOP;
        }
        break;
    case DOWN:
        if (y > height - 2) {
            dir = STOP;
        }
        else {
            y++;
            dir = STOP;
        }
        break;
    }
}

void draw () 
{ // коробка
    for (int i = 0; i < width + 1; i++) {        // потолок
        cout << "#";
    }
    cout << endl;

    for (int i = 0; i < height; i++) {       // стены
        for (int j = 0; j < width; j++) {
            if (j == 0 || j == width - 1) {
                cout << "#";
            }
            if (i == y && j == x) { // персонаж
                cout << "@";
            }
            else if (i == coinY && j == coinX) { // монета
                cout << "()";
            }
            else 
                cout << " ";
        }
        cout << endl;
    }

    for (int i = 0; i < width + 1; i++) {        // пол
        cout << "#";
    }
    cout << endl;
}

int main()
{
    void* handle = GetStdHandle(STD_OUTPUT_HANDLE); // чтобы не мигал курсор
    CONSOLE_CURSOR_INFO structCursorInfo;
    GetConsoleCursorInfo(handle, &structCursorInfo);
    structCursorInfo.bVisible = FALSE;
    SetConsoleCursorInfo(handle, &structCursorInfo);


    while (true) {
        setcur(0, 0);
        draw();
        input();
        logic();
    }
}

Ответы (0 шт):