-1

I am writing a snake game, and I have been able to produce a complete result that works as planned.

However, the game seems to randomly crash. I hit another command and the game closes even with the GameOver conditions are not met.

I have attached the code:

#include <iostream>
#include <conio.h>
#include <windows.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 Direction { STOP = 0, LEFT, RIGHT, UP, DOWN};
Direction dir;

int Setup()

{
    GameOver = false;
    dir = STOP;
    x = width / 2;
    y = height / 2;
    fruitX = rand() % width;
    fruitY = rand() % height;
    score = 0;
    return 0;
}

int Draw()

{
        system("cls"); //system("clear");
        for (int i = 0; i < width+2; i++)
            cout << "#";
        cout << endl;

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                if (j == 0)
                    cout << "#";
                if (i == y && j == x)
                    cout << "O";
                else if (i == fruitY && j == fruitX)
                    cout << "F";
                else
                {
                    bool print = false;
                    for (int k = 0; k < nTail; k++)
                    {
                        if (tailX[k] == j && tailY[k] == i)
                        {
                            cout << "o";
                            print = true;
                        }
                    }
                    if (!print)
                        cout << " ";
                }


                if (j == width - 1)
                    cout << "#";
            }
            cout << endl;
        }

        for (int i = 0; i < width+2; i++)
            cout << "#";
        cout << "\n";
        cout << "Score:" << score << "\n";
        return 0;
}

int 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;
            }
        }
    return 0;
}

int 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;
        default:
            break;
        }

        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++;
        }
        return 0;
}

int main()

{
    Setup();
    while (!GameOver)
    {
        Draw();
        Input();
        Logic();
        Sleep(50);
    }
    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
KDX
  • 61
  • 2
  • 7

1 Answers1

1

Your tailX/tailY arrays have a capacity of 100, but you don't enforce a limit on how large nTail gets. So when nTail becomes larger than 100, the behaviour of your program becomes undefined and may randomly crash.

But really, you should 1) learn how to use a debugger, and 2) get a more modern C++ compiler. Very few here would be able to compile or test this.

Ove
  • 770
  • 4
  • 9
  • Is there a beginner's guide that you would recommend? – KDX Jan 22 '18 at 20:43
  • Or any debugger, in particular, that is beginner friendly? – KDX Jan 22 '18 at 20:44
  • I don't have any particular recommendations, but you could look at https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282 – Ove Jan 23 '18 at 20:47