Number arranging game under turbo c and c++ full solved program

Number arranging game under TURBO C++ with source code:

Description:

In this project, we publish a C code for a number arranging puzzle game in which we take all numbers from 1 to 8 randomly and our aim to arrange all the numbers in order that is 1 to 8 and one vacant space is filled by number “0”.This game is best suited for engineering students to increase mind power and thinking capacity.

 The code of this puzzle game is written using C language under Turbo C++ and use graphics to increase interest of users. Code in this game is easy to understand by an
engineering student and so users can understand and modify code to make it more interesting.

Users are required Turbo C++ for any manipulation and run this code. Manipulation
includes changing color of box, text or background color and Increase/decrease size of
box and cells inside box and other depending on users.

CODE:

#include<stdio.h>           //for standard input/output
#include<conio.h>          //console input/output
#include<graphics.h>     //to include graphic functions
#include<process.h>      //for functions declaration and macros
#include<stdlib.h>          //standard library function
#include<bios.h>             //bios function

#define mx getmaxx()/2
#define my getmaxy()/2

int size=50;

voidform_box(void);

enumdir{UP=75,DOWN=77,LEFT=72,RIGHT=80,QUIT=16};

void main()
{
                inti,j,sum=0,m=0,xm,ym,grd=DETECT,grm,erc,locx,locy,key,temp;
                intmati[3][3]={1,2,3,4,0,5,6,7,8},matw[3][3]={1,2,5,4,6,8,0,7,3};
                charctemp[10];
                clrscr();
                re_enter:
                printf("\nPress any key to play\n");
   //         printf("\n Simply saying enter numbers from 0 to 8 in any order without repeating any number.");
                for(i=0;i<3;i++)
                {
                                for(j=0;j<3;j++)
                                {
//                                            scanf("%d",&matw[i][j]);
                                                if(matw[i][j]==0)  {xm=i;ym=j;}

                                                else if (mati[i][j]==matw[i][j])
                                                                m++;
                                                sum+=matw[i][j];
                                                if(matw[i][j]<0||matw[i][j]>8)
                                                {
                                                                printf("\nYou must have made any mistake. Enter number again.");
                                                                getch();
                                                                clrscr();
                                                                gotore_enter;
                                                }
                                }
                }
                if(sum!=36)
                {
                                printf("\nYou must have made any mistake. Enter number again.");
                                printf("\nSum: %d",sum);
                                sum=0;
                                getch();
                                clrscr();
                                gotore_enter;
                }
                for(i=0;i<3;i++)
                {
                                for(j=0;j<3;j++)
                                                printf("%4d",matw[i][j]);
                                printf("\n");
                }
                printf("\nMatch:  %d",m);
                if(m==8)
                                printf("\nPerfact");
                getch();
                initgraph(&grd,&grm,"");
                erc=graphresult();
                if(erc!=grOk)
                {
                                printf("\nError in graph  %s:",grapherrormsg(erc));
                                getch();
                                exit(1);
                }
                setbkcolor(GREEN);
                do
                {
                                clearviewport();
                                setcolor(BLUE);
                                settextstyle(3,0,3);
                                itoa(m,ctemp,10);
                                outtextxy(2*mx-120,2*my-30,"Matches: ");
                                                outtextxy(2*mx-20,2*my-30,ctemp);
                                setcolor(BROWN);
                                settextstyle(1,0,3);
                                                outtextxy(mx-90,2*my-40,"Press 'q' to quit");

                                setcolor(RED);
                                form_box();

                                settextstyle(4,0,3);
                                for(i=0;i<3;i++)
                                {
                                                if(i==0)
                                                                locy=my-2*size;
                                                if(i==1)
                                                                locy=my;
                                                if(i==2)
                                                                locy=my+2*size;
                                                for(j=0;j<3;j++)
                                                {
                                                                if(j==0)
                                                                                locx=mx-2*size;
                                                                if(j==1)
                                                                                locx=mx;
                                                                if(j==2)
                                                                                locx=mx+2*size;
                                                                if(i==xm&&j==ym)
                                                                {
                                                                                setfillstyle(SOLID_FILL,RED);
                                                                                floodfill(locx,locy,RED);
                                                                                outtextxy(locx,locy," ");
                                                                                continue;
                                                                }
                                                                itoa(matw[i][j],ctemp,10);
                                                                setfillstyle(SOLID_FILL,LIGHTMAGENTA);
                                                                floodfill(locx,locy,RED);
                                                                outtextxy(locx,locy,ctemp);
                                                }

                                }
                                if(m==8)
                                {
                                                settextstyle(4,0,4);
                                                setcolor(MAGENTA);
                                                outtextxy(mx-100,20,"Congratulation");
                                                getch();
                                                exit(1);
                                }
                                key=bioskey(0);
                                key=key>>8;
                                if((key==RIGHT)&&(xm!=2))
                                {
                                    matw[xm][ym]=matw[xm+1][ym];
                                    matw[xm+1][ym]=0;
                                    xm++;
                                }
                                if((key==LEFT)&&(xm!=0))
                                {
                                    matw[xm][ym]=matw[xm-1][ym];
                                    matw[xm-1][ym]=0;
                                    xm--;
                                }
                                if((key==UP)&&(ym!=0))
                                {
                                    matw[xm][ym]=matw[xm][ym-1];
                                    matw[xm][ym-1]=0;
                                    ym--;
                                }
                                if((key==DOWN)&&(ym!=2))
                                {
                                    matw[xm][ym]=matw[xm][ym+1];
                                    matw[xm][ym+1]=0;
                                    ym++;
                                }
                                if(key==QUIT)
                                {
                                                exit(1);
                                }
                                m=0;
                                for(i=0;i<3;i++)
                                {
                                                for(j=0;j<3;j++)
                                                {
                                                                if((matw[i][j]==mati[i][j])&&(matw[i][j]!=0))
                                                                m++;
                                                }
                                }
                }
                while (1);

                getch();
}


voidform_box(void)
{

                rectangle(mx-size,my-size,mx+size,my+size);
                rectangle(mx-size,my-3*size,mx+size,my-size);
                rectangle(mx-3*size,my-3*size,mx-size,my-size);
                rectangle(mx+3*size,my-3*size,mx+size,my-size);
                rectangle(mx-3*size,my-size,mx-size,my+size);
                rectangle(mx+3*size,my-size,mx+size,my+size);
                rectangle(mx-size,my+3*size,mx+size,my+size);
                rectangle(mx-3*size,my+3*size,mx-size,my+size);
                rectangle(mx+3*size,my+3*size,mx+size,my+size);
}

Run this code in Turbo C++ compiler, as no copy/paste work under MS DOS compiler compiler. Simply paste full code without any other term in text file and rename text file as any name that you want. After that drag your text file on Turbo C++ icon on the desktop your file is open in Turbo C++.


No comments:

Powered by Blogger.