15 SQUARE GAME using c with graphics



Hey guys, this program is simple classic 15 square game. I have coded this using c and included graphics. Use the arrow keys to move the boxes. Here the aim is to arrange those 15 boxes in correct order before the time limit. Hope you guys like it...

////15 square game using c and graphics.

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

void main();

getkey()
{
    union REGS i,o;
    while(!kbhit());
    i.h.ah=0;
    int86(22,&i,&o);
    return (o.h.ah);
}

int check(int mat[4][4],int n)
{
    int i,j;

    if(mat[3][3]==0)    mat[3][3]=16;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            if(mat[i][j]!=i*4+j+1)
                return 0;
    return 1;
}

void win()
{
    gotoxy(28,5);
    cout<<"Congrats! You have won....";
    gotoxy(27,24);
    cout<<"Do you want to try again?(y/n)";
    if(getkey()==21)
        main();
    else
        exit(0);
}

void lose()
{
    gotoxy(28,5);
    cout<<"Sorry! your time is up....";
    gotoxy(27,24);
    cout<<"Do you want to try again?(y/n)";
    if(getkey()==21)
        main();
    else
        exit(0);
}

void main_page()
{

    cleardevice();
    setcolor(14);
    settextstyle(10,0,3);
    outtextxy(90,50,"WELCOME TO 15 SQUARE");
    setcolor(11);
    settextstyle(9,0,1);
    //outtextxy(260,150,"R U L E :");
    outtextxy(240,150,"HOW to play :");
    setcolor(12);
    settextstyle(6,0,2);
    outtextxy(100,200,"1.Move cursor arrows(up,down,rt,lt) to move box.");

    outtextxy(100,250,"2.Aim is to arrange boxes in correct sequence.");
    outtextxy(100,300,"3.Press 'd' or 'Esc' to exit.");
    setcolor(14);
    outtextxy(220,450,"ENJOY The GAME . . . .");
    getch();
}

void main()
{
    int gd=DETECT,gm;
    initgraph(&gd,&gm,"..\\bgi");

    main_page();

    cleardevice();

    struct time t;

    int x=getmaxx()/2,y=getmaxy()/2;
    int len=200,x1=x-len/2,y1=y-len/2;
    int i,j,n,row=0,col=0;
    int mat[4][4]={0},count=0;

    setcolor(15);
    randomize();
    for(i=0;i<16;i++)
    {
        n=random(31999)%16;
        if(!mat[n%4][n/4])
        {
            mat[n%4][n/4]=count++;
        }
        else    i--;
        if(count>=16)
            break;

    }
    gotoxy(1,1);
    for(i=0;i<4;i++)
    {    for(j=0;j<4;j++)
        {
            if(mat[i][j]==0)
            {
                row=j,    col=i;
            }
        }
    }

    n=4;
    char str[40];

    settextstyle(2,0,7);
    rectangle(x1,y1,x1+len,y1+len);
    rectangle(x1-1,y1-42,x1+len+1,y1+len+42);

    setfillstyle(1,2);
    bar(x1,y1-40,x1+len,y1-3);
    outtextxy(x1+10,y1-30,"Time : ");

    bar(x1,y1+len+3,x1+len,y1+len+40);
    outtextxy(x1+10,y1+len+10,"Steps : ");

    gotoxy(1,1);
    gettime(&t);
    int strtm=t.ti_min,strts=t.ti_sec;
    settextstyle(1,0,2);
    setfillstyle(1,2);
    for(i=0;i<n;i++)
    {    for(j=0;j<n;j++)
        {
            bar(x1+i*50+2,y1+j*50+2,x1+(i+1)*50-2,y1+(j+1)*50-2);
            moveto(x1+i*50+10,y1+j*50+10);
            sprintf(str,"%2d",mat[j][i]);
            outtext(str);
        }
    }
    i=row,j=col;
    setfillstyle(1,0);
    bar(x1+i*50+2,y1+j*50+2,x1+(i+1)*50-2,y1+(j+1)*50-2);

    int ch,i1,j1;
    count=0;
    while(1)
    {
        ch=getkey();
        if(ch==32||ch==1)
        {    exit(0);    }

        i1=i,    j1=j;

        gotoxy(1,3);
        gettime(&t);
        int endm=t.ti_min,ends=t.ti_sec;
        int tim=(endm-strtm)*60+(ends-strts);

        settextstyle(2,0,7);
        setfillstyle(1,2);
        bar(x1+80,y1-40,x1+len,y1-3);
        moveto(x1+90,y1-30);
        sprintf(str,"%02d:%02d",tim/60,tim%60);
        outtext(str);

        switch(ch)
        {
            case 77:    if(i>0)    i--;
                    else continue;
                    break;
            case 80:    if(j>0)    j--;
                    else continue;
                    break;
            case 72:    if(j<n-1)        j++;
                    else continue;
                    break;
            case 75:    if(i<n-1)        i++;
                    else continue;
                    break;
            default:    continue;
        }
        int temp=mat[j][i];
        mat[j][i]=mat[j1][i1];
        mat[j1][i1]=temp;

        setfillstyle(1,0);      //deleting old box
        bar(x1+i*50+2,y1+j*50+2,x1+(i+1)*50-2,y1+(j+1)*50-2);

        settextstyle(1,0,2);
        setfillstyle(1,2);     //printing shifted box
        bar(x1+i1*50+2,y1+j1*50+2,x1+(i1+1)*50-2,y1+(j1+1)*50-2);

        moveto(x1+i1*50+10,y1+j1*50+10);//printing previous no. int he box
        sprintf(str,"%2d",mat[j1][i1]);
        outtext(str);
        if(check(mat,n))    win();
        gotoxy(1,2);

        settextstyle(2,0,7);
        setfillstyle(1,2);
        bar(x1+90,y1+len+3,x1+len,y1+len+40);
        moveto(x1+100,y1+len+10);
        sprintf(str,"%3d",++count);
        outtext(str);
    }
}
/// End of program

Note:

If you are getting error in compiling then save the file in .cpp format. Here is the screen shot of the output.





Comments

  1. I seen some of your programs... really talleted work brother
    all the best continue

    ReplyDelete

Post a Comment

Popular posts from this blog

mTranslator

Circular Pattern Using C/C++ and Graphics

Sorting Hat: Privacy Policy