#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>

#define SIZE 2000
#define SX 40
#define SY 12
#define UP 'k'
#define DOWN 'j'
#define LEFT 'h'
#define RIGHT 'l'

int main()
{
	int front,rear;
	int i,dx,dy,nx,ny,ex,ey;
	int table[SIZE][2];
	char ch;

	table[0][0] = SX; table[0][1] = SY;
	nx = SX; ny = SY; dx = 1; dy = 0; front = 1; rear = 0;
	ex = random(80);
	ey = random(24)
	gotoxy(SX,SY);
	printf("#");
	delay(100);

	clrscr();

	while(1)
	{
		while(kbhit())
		{
			ch = getch();
			if(ch == UP && dy != 1){
				dx = 0;	dy = -1;
			}else if(ch == DOWN && dy != -1){
				dx = 0;	dy= 1;
			}else if(ch == LEFT && dx != 1){
				dx = -1; dy = 0;
			}else if(ch == RIGHT && dx != -1){
				dx = 1; dy =0;
			}
		}
		delay(200);
		nx += dx; ny += dy;
		if(nx == ex && ny == ey){

		}else{
			table[front][0] = nx; table[front][1] = ny; front++;
			gotoxy(nx,ny);
			printf("#");
			gotoxy(table[rear][0],table[rear][1]);
			rear++;
			printf(" ");
			front %= SIZE; rear %= SIZE;
		}

		if(nx == 0 || nx == 80 || ny == 0 || ny == 25)
		{
			printf("Game Over !\n");
			break;
		}
	}
	return 0;
}

