#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

void initialize();
void display();
int randValue();
void winCheck();

#define ROWS 4
#define COLS 4

#define RIGHT 77
#define LEFT 75
#define TOP 72
#define BOTTOM 80
#define ESC 27

int  a[ROWS][COLS] = {0};
char cr, cc;

void main()
{
	char choice;
	int  temp;
	clrscr();
	_setcursortype(_NOCURSOR);

	initialize();
	display();

	while(1)
	{
		choice = getch();
		choice = getch();

		switch(choice)
		{
			case LEFT   : if( cc != COLS-1 )
						  {
							  temp = a[cr][cc];
							  a[cr][cc] = a[cr][cc+1];
							  a[cr][cc+1] = temp;
						  }
						  display();
						  break;
			case RIGHT  : if( cc != 0 )
						  {
							  temp = a[cr][cc];
							  a[cr][cc] = a[cr][cc-1];
							  a[cr][cc-1] = temp;
						  }
						  display();
						  break;
			case TOP    : if( cr != ROWS-1 )
						  {
							  temp = a[cr][cc];
							  a[cr][cc] = a[cr+1][cc];
							  a[cr+1][cc] = temp;
						  }
						  display();
						  break;
			case BOTTOM : if( cr != 0 )
						  {
							  temp = a[cr][cc];
							  a[cr][cc] = a[cr-1][cc];
							  a[cr-1][cc] = temp;
						  }
						  display();
						  break;
			case 'n':
			case 'N':
						  printf("");
						  initialize();
						  display();
						  break;
			case ESC    : exit(0);
		}
	}
}

void initialize()
{
	char i, j;
	randomize();
	// Initialize the array
	for(i=0; i<ROWS; i++)
		for(j=0; j<COLS; j++)
			a[i][j] = 0;

	// Initialize the array
	for(i=0; i<ROWS; i++)
	{
		for(j=0; j<COLS; j++)
		{
			a[i][j] = randValue();
		}
	}
}

int randValue()
{
	int temp;
	int x=0, i, j;
	int flag=0;

	temp = (ROWS * COLS) + 1;

	while(1)
	{
		flag = 0;
		x = rand() % temp;

		for(i=0; i<ROWS; i++)
		{
			for(j=0; j<COLS; j++)
			{
				if(a[i][j] == x)
					flag = 1;
			}
		}
		if( !flag )
			return x;
	}
}

void display()
{
	//int i, j;
	char i, j, c=2,r=2;
	int temp = ROWS * COLS;

	for(i=0; i<ROWS; i++)
	{
		c = 2;
		r += 2;
		for(j=0; j<COLS; j++)
		{
			if(i==0)	// first row
			{
				if(j==0)
				{
					gotoxy(c, r);   printf("ÚÄÄÄÂ");

					if(a[i][j] != temp)
						{ gotoxy(c, r+1); printf("³%3d³", a[i][j]); }
					else
						{ gotoxy(c, r+1); printf("³   ³"); cr=i; cc=j; }

					gotoxy(c, r+2); printf("ÃÄÄÄÅ");
					c += 5;
				}
				else
				{
					gotoxy(c, r);   printf("ÄÄÄÂ");

					if(a[i][j] != temp)
						{ gotoxy(c, r+1); printf("%3d³", a[i][j]); }
					else
						{ gotoxy(c, r+1); printf("   ³"); cr=i; cc=j; }

					gotoxy(c, r+2); printf("ÄÄÄÅ");
					c += 4;
				}
			}
			else		// rest of the rows
			{
				if(j==0)
				{

					if(a[i][j] != temp)
						{ gotoxy(c, r+1); printf("³%3d³", a[i][j]); }
					else
						{ gotoxy(c, r+1); printf("³   ³"); cr=i; cc=j; }

					gotoxy(c, r+2); printf("ÃÄÄÄÅ");
					c += 5;
				}
				else
				{
					if(a[i][j] != temp)
						{ gotoxy(c, r+1); printf("%3d³", a[i][j]); }
					else
						{ gotoxy(c, r+1); printf("   ³"); cr=i; cc=j; }

					gotoxy(c, r+2); printf("ÄÄÄÅ");
					c += 4;
				}
			}
		}
		//printf("\n");
	}
}

void winCheck();
{
	char i, j, k=1;
	char flag=1;

	for(i=0; i<ROWS; i++)
		for(j=0; j<COLS; j++)
			if(a[i][j] != k++)
				flag = 0;

	if(flag)
	{
		gotoxy(20,2);
		printf("You win...");
		getch();
		initialize();
		display();
	}
}
