/*
Syed Mehroz Alam
CIS-61
FE(CIS) Sec-A
5 June 2003
*/

#include
#include
#include
#include
#include
#include
#include
#include

#define MAX 10
#define sqr(n) n*n


void shuffle(void);	//shuffles the board
int found(int board[][MAX], int no);	//helper funtion for shuffling
void dispboard(void);	//displays the current contents of board
int getkey(void);	//used to track arrow keys
void swap(int x2,int y2);	//swaps x,y with x2,y2

int board[MAX][MAX];
int x,y;
int win, turns;
int ORDER;


void main(void)
{
	clrscr();
	textcolor(15);
	gotoxy(38,4); cprintf("Puzzle");
	gotoxy(38,5); cprintf("ออออออ");
	gotoxy(31,7); cprintf("by Syed Mehroz Alam");
	gotoxy(20,9);  cprintf("CIS-61, First Year, Sec-A, Batch 2002-03");
	gotoxy(20,10); cprintf("  Computer & Information Systems Dept,");
	gotoxy(20,11); cprintf("   NED University, Karachi, Pakistan.");
	gotoxy(20,13); cprintf("     Email: smehrozalam@yahoo.com");
	gotoxy(20,16); cprintf("     Press any key to begin......");
	getch();
	textcolor(7);
	printf("\n\n\t\tEnter Order (Maximum 10) : ");
	scanf("%d", &ORDER);
	if (ORDER<2 || ORDER >10)
		return;		//ends the program

	shuffle();
	dispboard();

	do
	{
		int move=0;
		while ( !(move==72 || move==80 ||move==75 || move==77 || move==1 || move==31) )
		{
			move=getkey();
		}
		if (move==1)	//escape key
			return;
		//else if arrow keys, the do respective job
		else if (move==31)
			shuffle();
		else if (move==80)
			swap(x,y-1);
		else if (move==72)
			swap(x,y+1);
		else if (move==77)
			swap(x-1,y);
		else if (move==75)
			swap(x+1,y);
		dispboard();

	}
	 while(win!=1);

	printf("\n\nCongratulations, You won in %d turns", turns);
	getch();
}


void shuffle(void)
{
	randomize();
	for (int i=0;i=0 && x2<=ORDER-1 && y2>=0 && y2<=ORDER-1)
	{
		board[y][x]=board[y2][x2];
		board[y2][x2]=sqr(ORDER);
		x=x2; y=y2;
		turns++;
	}
}

    Source: geocities.com/smehrozalam/source

               ( geocities.com/smehrozalam)