помощь в решении пары ошибок

#include <iostream>
#include <time.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
enum eDirection { stop = 0, left, right, up, down};
eDirection dir;
void Setup()
{
    gameOver = false;
    dir = stop;
    x = width / 2 - 1;
    y = height / 2 - 1;
    srand(time(NULL));
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
   }
    void Draw()
{
        system("cls");
        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 << "0";
            else if (i == fruitY && j == fruitX)
                cout << "@";
            else
            {
                bool print = false;
                for (int k = 0; k < ntail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        print = true;
                        cout << "o";
                    }
                }
                if(!print)
            cout << " ";
            }   
        }
        cout << endl;
    }
    for (int i = 0; i < width + 1; i++)
        cout << "#";
    cout << "Score =>" << score << "<=" << endl;
    cout << endl;
}
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;
        case 'x':
            gameOver = true;
            break;
        }
    }
}
void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < ntail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir)
    {
    case left:
        x--;
        break;
    case right:
        x++;
        break;
    case up:
        y--;
        break;
    case down:
        y++;
        break;
    }
    //if (x > width || x < 0 || y > height || y < 0)
    //  gameOver = true;
    if (x >= width - 1)
        x = 0;
    else if (x < 0)
        x = width - 2;
    if (y >= height)
        y = 0;
    else if (x < 0)
        y = height - 1;
    for (int i = 0; i < ntail; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;
    }
    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        ntail++;
    }
}
int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
    }
}

проблема в паре выводимых ошибок:

Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка C3861 _kbhit: идентификатор не найден zmeyka F:\Projects\zmeyka\zmeyka\game.cpp 63

Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка (активно) E0020 идентификатор "_getch" не определен zmeyka F:\Projects\zmeyka\zmeyka\game.cpp 65

Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка (активно) E0266 "left" не является однозначным zmeyka F:\Projects\zmeyka\zmeyka\game.cpp 68

Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка (активно) E0266 "right" не является однозначным zmeyka F:\Projects\zmeyka\zmeyka\game.cpp 71

Подскажите, что это такое?


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

Автор решения: Константин Николаевич Бояр II

Вот ваш отредактированный код:

#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
enum eDirection { stop = 0, left, right, up, down };
eDirection dir;
void Setup()
{
    gameOver = false;
    dir = stop;
    x = width / 2 - 1;
    y = height / 2 - 1;
    srand(time(NULL));
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
}
void Draw()
{
    system("cls");
    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 << "0";
            else if (i == fruitY && j == fruitX)
                cout << "@";
            else
            {
                bool print = false;
                for (int k = 0; k < ntail; k++)
                {
                    if (tailX[k] == j && tailY[k] == i)
                    {
                        print = true;
                        cout << "o";
                    }
                }
                if (!print)
                    cout << " ";
            }
        }
        cout << endl;
    }
    for (int i = 0; i < width + 1; i++)
        cout << "#";
    cout << "Score =>" << score << "<=" << endl;
    cout << endl;
}
void Input()
{
    if (_kbhit())
    {
        switch (_getch())
        {
        case 'a':
            dir = eDirection::left;
            break;
        case 'd':
            dir = eDirection::right;
            break;
        case 'w':
            dir = up;
            break;
        case's':
            dir = down;
            break;
        case 'x':
            gameOver = true;
            break;
        }
    }
}
void Logic()
{
    int prevX = tailX[0];
    int prevY = tailY[0];
    int prev2X, prev2Y;
    tailX[0] = x;
    tailY[0] = y;
    for (int i = 1; i < ntail; i++)
    {
        prev2X = tailX[i];
        prev2Y = tailY[i];
        tailX[i] = prevX;
        tailY[i] = prevY;
        prevX = prev2X;
        prevY = prev2Y;
    }
    switch (dir)
    {
    case eDirection::left:
        x--;
        break;
    case eDirection::right:
        x++;
        break;
    case up:
        y--;
        break;
    case down:
        y++;
        break;
    }
    //if (x > width || x < 0 || y > height || y < 0)
    //  gameOver = true;
    if (x >= width - 1)
        x = 0;
    else if (x < 0)
        x = width - 2;
    if (y >= height)
        y = 0;
    else if (x < 0)
        y = height - 1;
    for (int i = 0; i < ntail; i++)
    {
        if (tailX[i] == x && tailY[i] == y)
            gameOver = true;
    }
    if (x == fruitX && y == fruitY)
    {
        score += 10;
        fruitX = rand() % width;
        fruitY = rand() % height;
        ntail++;
    }
}
int main()
{
    Setup();
    while (!gameOver)
    {
        Draw();
        Input();
        Logic();
    }
}
→ Ссылка