консольная змейка на с++
Решил по приколу написать некое подобие змейки на с++, но столкнулся с проблемой. Когда score доходит до 4, игра прекращается, подскажите пожалуйста, почему ?
#include <iostream>
#include <Windows.h>
#include <conio.h>
using namespace std;
int w = 20;
int h = 20;
bool gameOver;
int x, y, fructX, fructY, score;
enum eDir
{
STOP,
LEFT,
RIGHT,
DOWN,
UP
};
eDir dir;
void Setup()
{
gameOver = false;
x = w / 2;
y = h / 2;
fructX = rand() % w;
fructY = rand() % h;
dir = STOP;
}
void Draw()
{
system("cls");
for (int i = 0; i < w + 1; i++)
{
cout << "#";
}
cout << "\n";
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w ; j++)
{
if (j == 0 || j == w - 1)
{
cout << "#";
}
if (i == y && j == x )
{
cout << "T";
}
else if (j == fructX && i == fructY)
{
cout << "F";
}
else
{
cout << " ";
}
}
cout << "\n";
}
for (int i = 0; i < w + 1; i++)
{
cout << "#";
}
cout << "\nscore: " << score;
}
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()
{
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
}
if (x == fructX && y == fructY )
{
score += 1;
fructX = rand() % w;
fructY = rand() % y;
}
}
int main()
{
Setup();
while(!gameOver)
{
Draw();
Sleep(100);
Input();
Logic();
}
}
Источник: Stack Overflow на русском