NUMBER PAZZLE GAME
(C program)

Simple number arranging game. There is a 4X4 matrix which contains numbers. You have to arrange the numbers in ascending order.
/* ##################################### # NUMBER PAZIL GAME # # tested in Turbo C++,version 3.0 # ##################################### */ #include<time.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> int cell[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int newCell, oldCell, blankCell; void Put(int x, int y, char str[]) { gotoxy(x,y), cprintf(str); } void DrawTable() { int i,j; textcolor(LIGHTGREEN), Put(11,2,"_NUMBER PAZIL GAME_"); textcolor(MAGENTA); Put(10, 5,"ÉÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍ»"); for(i=0;i<4;i++) for(j=0;j<3;j++) Put(10, i*4+6+j,"º º º º º"); for(i=0;i<4;i++) Put(10,4*i+9,"ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍ¹"); Put(10,21,"ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍ¼"); textcolor(LIGHTRED), Put(3,24,"http://programs.on.googlepages.com"); } void FillCell() { int i, newBlankCell, randNum; blankCell=15; randomize(); for(i=0;i<256;i++) { getRandomNum_: randNum=random(4); if(randNum==0) { if(blankCell<4) goto getRandomNum_; else newBlankCell=blankCell-4; } else if(randNum==1) { if(blankCell>11) goto getRandomNum_; else newBlankCell=blankCell+4; } else if(randNum==2) { if(blankCell%4==3) goto getRandomNum_; else newBlankCell=blankCell+1; } else { if(blankCell%4==0) goto getRandomNum_; else newBlankCell=blankCell-1; } cell[blankCell]=cell[newBlankCell]; blankCell=newBlankCell; } for(i=0;i<16;i++) { if(cell[i]==i+1) textcolor(LIGHTGREEN); else textcolor(WHITE); gotoxy(12+5*(i%4),7+4*(i/4)),cprintf("%2d",cell[i]); } oldCell= newCell= blankCell; Put(12+5*(blankCell%4),7+4*(blankCell/4)," "); } void DrawSelection(int position, int color) { textcolor(color); Put( 11+5*(position%4), 6+4*(position/4), "ÚÄÄ¿" ); Put( 11+5*(position%4), 7+4*(position/4), "³" ); Put( 14+5*(position%4), 7+4*(position/4), "³" ); Put( 11+5*(position%4), 8+4*(position/4), "ÀÄÄÙ" ); } void CellSelect() { DrawSelection(oldCell,BLACK); DrawSelection(newCell,LIGHTCYAN); oldCell=newCell; } int GetSpace() { textcolor(BLACK), Put(1,1," "), gotoxy(1,1); /* curser hidding */ while(1) { switch(getch()) { case 27: exit(0); case' ': return 1; case 72: if(oldCell<4) continue; newCell-=4; return 0; case 75: if(oldCell%4==0) continue; newCell--; return 0; case 77: if(oldCell%4==3) continue; newCell++; return 0; case 80: if(oldCell>11) continue; newCell+=4; return 0; default: continue; } } } void SwapNum() { if( blankCell-newCell==4 || newCell-blankCell==4 || newCell-blankCell==1&&newCell%4!=0 || blankCell-newCell==1&&newCell%4!=3 ) { Put(12+5*(newCell%4),7+4*(newCell/4)," "); if(blankCell+1==cell[newCell]) textcolor(LIGHTGREEN); else textcolor(WHITE); gotoxy(12+5*(blankCell%4),7+4*(blankCell/4)), cprintf("%2d",cell[newCell]); cell[blankCell]=cell[newCell]; blankCell=newCell; } } int Success() { int i; for(i=0;i<14;i++) { if(cell[i]<cell[i+1]) continue; else break; } return (i==14) ? 1 : 0; } void instructions() { clrscr(); puts("_INSTRUCTIONS_\n"); puts("# You have to arrange the numbers as given below."); puts("# You can use the help of arrow-keys and space-key.\n"); puts(" ÉÍÍÍÍËÍÍÍÍËÍÍÍÍËÍÍÍÍ»"); puts(" º 1 º 2 º 3 º 4 º"); puts(" ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍ¹"); puts(" º 5 º 6 º 7 º 8 º"); puts(" ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍ¹"); puts(" º 9 º 10 º 11 º 12 º"); puts(" ÌÍÍÍÍÎÍÍÍÍÎÍÍÍÍÎÍÍÍÍ¹"); puts(" º 13 º 14 º 15 º º"); puts(" ÈÍÍÍÍÊÍÍÍÍÊÍÍÍÍÊÍÍÍÍ¼"); getch(); } void main() { instructions(); textmode(C40); while(1) { DrawTable(); FillCell(); DrawSelection(blankCell,LIGHTCYAN); while(1) { if(GetSpace()) { SwapNum(); if(blankCell==15) if(Success()) { clrscr(); textcolor(WHITE), Put(13,13,"CONGRATULATIONS!"); getch(); break; } } else CellSelect(); } } } /******************************************************* * More programs: http://programs.on.googlepages.com * * Please report suggestions: programs.on@gmail.com * ********************************************222*lines**/