#include <iostream>
#include <conio.h>
#include <Windows.h>
#define max 100
using namespace std;
int sl = 4;
int keyEvent;
int xqua = rand() % ( 67 - 11 + 1) + 11;
int yqua = rand() % ( 16 - 2 + 1) + 2;
bool lose = false;
void SnakeVal();
void CoutSnake();
void UpdateVal();
void Fruit();
void CreateBox();
void gotoXY (int column, int line)
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
struct toado
{
int x;
int y;
};
toado ran[max];
int main ()
{
SnakeVal();
while(1)
{
CreateBox();
Fruit();
if(kbhit())
{
char key = getch();
if ((key == 'a') && (keyEvent != 2)) keyEvent = 1;
if ((key == 'd') && (keyEvent != 1)) keyEvent = 2;
if ((key == 'w') && (keyEvent != 4)) keyEvent = 3;
if ((key == 's') && (keyEvent != 3)) keyEvent = 4;
}
if (keyEvent == 1) {ran[0].x--; UpdateVal();}
if (keyEvent == 2) {ran[0].x++; UpdateVal();}
if (keyEvent == 3) {ran[0].y--; UpdateVal();}
if (keyEvent == 4) {ran[0].y++; UpdateVal();}
CoutSnake();
if (ran[0].x == 69 || ran[0].x == 10) lose = true;
if (ran[0].y == 2 || ran[0].y == 18) lose = true;
if (lose == true)
{
system("cls");
cout << sl;
break;
}
system("cls");
}
}
void SnakeVal()
{
ran[0].x = 23;
ran[1].x = 24;
ran[2].x = 25;
ran[0].y = 7;
ran[1].y = 7;
ran[2].y = 7;
}
void CoutSnake()
{
for (int i = 0;i<sl;i++)
{
gotoXY(ran[i].x,ran[i].y);
if (i <= 0) cout << "0"; else cout << "o";
}
}
void UpdateVal()
{
for (int i = sl - 1;i>=1;i--)
{
ran[i] = ran[i-1];
}
}
void Fruit()
{
gotoXY(xqua,yqua); cout << "X";
if ((ran[0].x == xqua) && (ran[0].y == yqua))
{
sl++;
xqua = rand() % (68 - 11 + 1) + 11;
yqua = rand() % (17 - 3 + 1) + 3;
}
}
void CreateBox()
{
for (int i=10;i<70;i++) {gotoXY(i,2); cout << "0";}
for (int i=10;i<70;i++) {gotoXY(i,18); cout << "0";}
for (int i=3;i<18;i++) {gotoXY(10,i); cout << "0";}
for (int i=3;i<18;i++) {gotoXY(69,i); cout << "0";}
}