The Source Code for
Slate Game.
/* SLATE (GAME) */
/* B.E(INFORMATION TECHNOLOGY) IInd YR */
/* FOR SUGGESTIONS contact me at [email protected]
// INCLUDE LIBRARIES //
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<time.h>
#include"sound.h"
// GLOBAL VARIABLES //
int a[4][4],c[16];
// FUNCTION PROTOTYPES //
int select();
void arrange(int);
//void autoplay();
int zero();
int getkey();
void display();
void interchange(int*,int*);
int *detect();
int check();
void main() // THE MAIN FUNCTION //
{
int i,j;
for(i=0;i<16;i++)
c[i]=1;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
a[i][j]=select();
}
display();
while(!check())
{
i=getkey();
arrange(i);
getsound(random(37));
}
}
void interchange(int *m,int *n) // FUNCTION WHICH INTERCHANGES THE POSITION
{ // OF TWO NUMBERS.
int *p; *p = *m;* m=*n; *n=*p;
}
int select() // FUNCTION WHICH RANDOMLY SELECTS A NUMBER
{ // FROM 0 TO 16,AND FILLS THE 2-D ARRAY.
int n;
randomize();
while(!zero())
{
n = random(16);
if(n<0)
n*=-1;
if(c[n]!=0)
{
c[n]=0;
return(n);
}
else continue;
}
return(0);
}
int zero() // FUNCTION WHICH CHECKS WHETHER THE DUMMY ARRAY b[] IS
{ // FILLED WITH ZEROES OR NOT.
int i;
for(i=0;i<16;i++)
{
if(c[i]==0)
continue;
else
return(0);
}
return(1);
}
void display() // FUNCTION TO DISPLAY THE SLATE BOARD //
{
int i,j;
clrscr();
gotoxy(28,7); textcolor(GREEN);
cputs("WELCOME TO THE SLATE GAME");
gotoxy(20,12);textcolor(CYAN);
for(i=0;i<4;i++)
{
printf("\n");
gotoxy(20,12+i);
for(j=0;j<4;j++)
{
if(a[i][j]==0)
printf("\t ");
else
printf("\t%d",a[i][j]);
}
}
gotoxy(10,20);textcolor(YELLOW);
cputs("Press UP/DOWN/LEFT/RIGHT arrow keys to arrange or ESC to quit.");
gotoxy(10,21);
cputs("Press Home for a new board.");
}
int getkey() // FUNCTION WHICH RETURNS THE SCAN CODE OF THE KEY HIT//
{
union REGS i,o;
while(!kbhit())
;
i.h.ah = 0;
int86(22,&i,&o);
return(o.h.ah);
}
int *detect() // FUNCTION WHICH DETECTS THE POSITION OF ZERO ON THE BOARD//
{
int i,j,k[2];
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]==0)
{
k[0]=i;
k[1]=j;
}
}
}
return(k);
}
int check() // FUNCTION WHICH CHECKS WHETHER THE BOARD IS ARRANGED IN
{ // ASCENDING ORDER OR NOT
int i,j,k[16],l=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
k[l] = a[i][j];
l++;
}
}
i=0;
for(i=0;i<16;i++)
{
if((k[i]+1)==(k[i+1]))
continue;
else
return(0);
}
return(1);
}
void arrange(int ch) // FUNCTION WHICH ARRANGES THE BOARD //
{
int i,j;
i = *detect();
j = *(detect()+1);
switch(ch)
{
case 72:{if(i==0)break;
else interchange(&a[i][j],&a[i-1][j]);}
break;
case 75:{if(j==0) break;
else interchange(&a[i][j],&a[i][j-1]);}
break;
case 77:{if(j==3) break;
else interchange(&a[i][j],&a[i][j+1]);}
break;
case 80:{if(i==3) break;
else interchange(&a[i][j],&a[i+1][j]);}
break;
case 71:main();
break;
case 1: gotoxy(20,22);textcolor(RED);
cputs(" So You give Up ?!! Never mind. Press any Key. " );
getch();
exit(0);
default: break;
}
display();
gotoxy(10,24);textcolor(CYAN);
if(check()&&(a[3][3]==0))
{
cputs("Congratulations ! You have arranged the numbers successfully. ");
getch();
exit(0);
}
}
/*void autoplay() // FUNCTION WHICH AUTOMATICALLY ARRANGES THE BOARD//
{
int i;
randomize();
while((!check())&&(!kbhit()))
{
i = random(81);
if(i<72)
continue;
if((i==72)||(i==75)||(i==77)||(i==80))
{
getsound(random(37));
delay(100);
arrange(i);
}
}
} */