Magic Square: Using c/c++ with graphics
Hey guys, today I am going to post another interesting game, magic square. Its the game where you have to fill the boxes with numbers from 1 to total number of boxes such that sum of any row or column must be equal to other.
Here's the link if you want to know how to solve it.
http://www.wikihow.com/Solve-a-Magic-Square
Here's the screen shot of the output.
Here I have chosen 9x9 box, you can choose any one you like. With out further due let us see the codes.
Note: Please save the program in .cpp format to run the program or else you can change the declaration portion to run in .c format.
/*
Programmer: Ashok Kumar Shrestha (ak007)
Program details: Magic square, Fill the empty boxes such that sum
of all the numbers in column or row will be equal.
Use mouse pointer and click left button to place
number in ascending order.
*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<stdlib.h>
union REGS i, o;
int initmouse()
{
i.x.ax = 0;
int86(0X33,&i,&o);
return ( o.x.ax );
}
void showmouseptr()
{
i.x.ax = 1;
int86(0X33,&i,&o);
}
void getmousepos(int *button, int *x, int *y)
{
i.x.ax = 3;
int86(0X33,&i,&o);
*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}
void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return 0;
}
struct SREGS s;
int cursor[32]={0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0xffff,0x8001,0xffff,0x8001,
0x4002,0x2004,0x1008,0x0240,
0x0240,0x0810,0x2004,0x4002,
0x8001,0xffff,0x8001,0xffff
};
changecursor(int *shape)
{
i.x.ax=9;
i.x.bx=0;
i.x.cx=0;
i.x.dx=(unsigned)shape;
segread(&s);
s.es=s.ds;
int86x (0x33,&i,&i,&s);
return 0;
}
int sqr[10][10];
void gen_arry(int n)
{
int i, j, count,row,column;
sqr[0][(n-1)/2]=1;
i=0;
j=(n-1)/2;
for(count=2;count<=n*n;count++)
{
row=(i-1<0)?(n-1):(i-1);
column=(j-1<0)?(n-1):(j-1);
if(sqr[row][column])
i=(++i)%n;
else
{
i=row;
j=(j-1<0)?(n-1):--j;
}
sqr[i][j]=count;
}
}
chek(int sqr[10][10],int ay[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(sqr[i][j]!=ay[i][j])
return 1;
}
}
return 0;
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return (o.h.ah);
}
void openmenu()
{
clrscr();
textcolor(2);
gotoxy(28,5);
cprintf(" WELCOME MAGIC SQUARE");
gotoxy(16,8);
textcolor(3);
cprintf(" PROGRAMMER :");
gotoxy(31,8);
textcolor(9);
cprintf(" ASHOK KUMAR SHRESTHA (0 0 7)");
gotoxy(30,11);
textcolor(5);
cprintf(" RULES TO PLAY GAME:");
gotoxy(16,13);
textcolor(6);
cprintf("1.Select the desired position(box) using mouse pointer. ");
gotoxy(16,15);
textcolor(6);
cprintf("2.After selecting press LEFT BUTTON ");
gotoxy(16,17);
textcolor(6);
cprintf("3.After filling each box, press RIGHT BUTTON to check. ");
gotoxy(26,22);
textcolor(9+BLINK);
cprintf(" ENJOY THE GAME %c %c %c %c %c",1,1,1,1,1);
int a;
a=getch();
if(a==27)
exit(0);
}
int level()
{
clrscr();
textcolor(CYAN);
printf("\n\n\n\n\n \t\t\t");
cprintf("Welcome_to MAGIC SQUARE");
textcolor(5+BLINK);
gotoxy(30,11);
//textcolor(5);
cprintf(" SELECT THE LEVEL : ");
gotoxy(30,13);
textcolor(15);
cprintf("1.EASY ( 3 X 3 ). ");
gotoxy(30,15);
textcolor(2);
cprintf("2.MEDIUM ( 5 X 5 ). ");
gotoxy(30,17);
textcolor(9);
cprintf("3.HARD ( 7 X 7 ). ");
gotoxy(30,19);
textcolor(4);
cprintf("4.VERY HARD ( 9 X 9 ). ");
gotoxy(26,22);
delay(126);
int n=getkey()-1;
if(n+1==1) exit(0);
while(n<1||n>4)
{ gotoxy(26,22);
printf("Invalid choice. enter again...");
n=getkey()+1;
}
return (2*n+1);
}
void prnt_side_sum(int sqr[10][10],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{ sum+=sqr[0][i];
}
gotoxy(25,1);
printf("SUM OF NO. OF EACH SIDE = %d ",sum);
}
int ay[10][10];
void tip(int sqr[10][10],int n)
{
int take=0,i,j,x=0,y=0;
switch(n)
{
case 3: take =3;
break;
case 5: take =9;
break;
case 7: take= 16;
break;
case 9: take= 25;
break;
}
randomize();
for(i=0;i<take;i++)
{
x=(random(n*1000))/1000;
y=(random(n*1000))/1000;
ay[x][y]=sqr[x][y];
}
}
void prnt_tip(int ay[10][10],int x1,int y1,int n)
{
int i,j,cur=0,x2=0,y2=0;
char array[40];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(ay[i][j]!=0)
{
x2=j*50+x1;
y2=i*50+y1;
cur=ay[i][j];
hidemouseptr();
moveto(x2+15,y2+10);
sprintf(array,"%d",cur);
outtext(array);
showmouseptr();
}
}
}
}
void magic()
{
int status, button=0, x, y;
char array[50]={0},ar[50],ch='y';
int i,j,x1=0,x2=0,y1=0,y2=0,x3=0,y3=0;
int count=1,mx,my;
int mi,mj,prev,cur=0;
float k;
int n=level();
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\BGI");
settextstyle(GOTHIC_FONT,0,2);
cleardevice();
mx=getmaxx()/2;
my=getmaxy()/2;
gen_arry(n);
tip(sqr,n);
prnt_side_sum(sqr,n);
k=n;
x1=mx-(k/2)*50;
x3=x1+k*50;
y1=my-(k/2)*50;
y3=y1+k*50;
setcolor(1);
setfillstyle(1,2);
rectangle(x1,y1,x3,y3);
floodfill(x1+1,y1+1,1);
for(i=0;i<n;i++)
{
line(x1+i*50,y1,x1+i*50,y3);
line(x1,y1+i*50,x3,y1+i*50);
}
restrictmouseptr(x1+5,y2+5,x3-5,y3-5);
showmouseptr();
prnt_tip(ay,x1,y1,n) ;
while(ch=='y')
{
getmousepos(&button,&x,&y);
gotoxy(10,10);
mi=((x-x1)/50);
mj=((y-y1)/50);
if(ay[mj][mi]!=0)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ if(ay[i][j]==count)
{
count++;
break;
}
}
}
if( button == 1 )
{
x2=((x-x1)/50)*50+x1;
y2=((y-y1)/50)*50+y1;
delay(126);
if(ay[mj][mi]==0)
{
ay[mj][mi]=count++;
cur=ay[mj][mi];
hidemouseptr();
moveto(x2+15,y2+10);
sprintf(array,"%d",cur);
outtext(array);
showmouseptr();
}
button=-1;
}
else if(button==2)
{
button=-1;
delay(126);
hidemouseptr();
delay(126);
cleardevice();
setcolor(2);
if(chek(sqr,ay,n))
{ setcolor(4);
settextstyle(4,0,4);
outtextxy(180,200,"T R Y A G A I N");
}
else
{
setcolor(1);
settextstyle(4,0,4);
outtextxy(180,150,"W E L L D O N E");
setcolor(2);
outtextxy(210,250,"Y O U W I N");
}
gotoxy(27,24);
printf("Do you want to try again?(y/n)");
if(getkey()==21)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ ay[i][j]=0,sqr[i][j]=0;}
closegraph();
magic();
}
else
exit(0);
}
}
button=-1;
clearviewport();
closegraph();
}
void main()
{
openmenu();
magic();
}
//End of the program
Here's the link if you want to know how to solve it.
http://www.wikihow.com/Solve-a-Magic-Square
Here's the screen shot of the output.
Here I have chosen 9x9 box, you can choose any one you like. With out further due let us see the codes.
Note: Please save the program in .cpp format to run the program or else you can change the declaration portion to run in .c format.
/*
Programmer: Ashok Kumar Shrestha (ak007)
Program details: Magic square, Fill the empty boxes such that sum
of all the numbers in column or row will be equal.
Use mouse pointer and click left button to place
number in ascending order.
*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<stdlib.h>
union REGS i, o;
int initmouse()
{
i.x.ax = 0;
int86(0X33,&i,&o);
return ( o.x.ax );
}
void showmouseptr()
{
i.x.ax = 1;
int86(0X33,&i,&o);
}
void getmousepos(int *button, int *x, int *y)
{
i.x.ax = 3;
int86(0X33,&i,&o);
*button = o.x.bx;
*x = o.x.cx;
*y = o.x.dx;
}
void hidemouseptr()
{
i.x.ax=2;
int86(0x33,&i,&o);
}
restrictmouseptr(int x1,int y1,int x2,int y2)
{
i.x.ax=7;
i.x.cx=x1;
i.x.dx=x2;
int86(0x33,&i,&o);
i.x.ax=8;
i.x.cx=y1;
i.x.dx=y2;
int86(0x33,&i,&o);
return 0;
}
struct SREGS s;
int cursor[32]={0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0x0000,0x0000,0x0000,0x0000,
0xffff,0x8001,0xffff,0x8001,
0x4002,0x2004,0x1008,0x0240,
0x0240,0x0810,0x2004,0x4002,
0x8001,0xffff,0x8001,0xffff
};
changecursor(int *shape)
{
i.x.ax=9;
i.x.bx=0;
i.x.cx=0;
i.x.dx=(unsigned)shape;
segread(&s);
s.es=s.ds;
int86x (0x33,&i,&i,&s);
return 0;
}
int sqr[10][10];
void gen_arry(int n)
{
int i, j, count,row,column;
sqr[0][(n-1)/2]=1;
i=0;
j=(n-1)/2;
for(count=2;count<=n*n;count++)
{
row=(i-1<0)?(n-1):(i-1);
column=(j-1<0)?(n-1):(j-1);
if(sqr[row][column])
i=(++i)%n;
else
{
i=row;
j=(j-1<0)?(n-1):--j;
}
sqr[i][j]=count;
}
}
chek(int sqr[10][10],int ay[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(sqr[i][j]!=ay[i][j])
return 1;
}
}
return 0;
}
getkey()
{
union REGS i,o;
while(!kbhit());
i.h.ah=0;
int86(22,&i,&o);
return (o.h.ah);
}
void openmenu()
{
clrscr();
textcolor(2);
gotoxy(28,5);
cprintf(" WELCOME MAGIC SQUARE");
gotoxy(16,8);
textcolor(3);
cprintf(" PROGRAMMER :");
gotoxy(31,8);
textcolor(9);
cprintf(" ASHOK KUMAR SHRESTHA (0 0 7)");
gotoxy(30,11);
textcolor(5);
cprintf(" RULES TO PLAY GAME:");
gotoxy(16,13);
textcolor(6);
cprintf("1.Select the desired position(box) using mouse pointer. ");
gotoxy(16,15);
textcolor(6);
cprintf("2.After selecting press LEFT BUTTON ");
gotoxy(16,17);
textcolor(6);
cprintf("3.After filling each box, press RIGHT BUTTON to check. ");
gotoxy(26,22);
textcolor(9+BLINK);
cprintf(" ENJOY THE GAME %c %c %c %c %c",1,1,1,1,1);
int a;
a=getch();
if(a==27)
exit(0);
}
int level()
{
clrscr();
textcolor(CYAN);
printf("\n\n\n\n\n \t\t\t");
cprintf("Welcome_to MAGIC SQUARE");
textcolor(5+BLINK);
gotoxy(30,11);
//textcolor(5);
cprintf(" SELECT THE LEVEL : ");
gotoxy(30,13);
textcolor(15);
cprintf("1.EASY ( 3 X 3 ). ");
gotoxy(30,15);
textcolor(2);
cprintf("2.MEDIUM ( 5 X 5 ). ");
gotoxy(30,17);
textcolor(9);
cprintf("3.HARD ( 7 X 7 ). ");
gotoxy(30,19);
textcolor(4);
cprintf("4.VERY HARD ( 9 X 9 ). ");
gotoxy(26,22);
delay(126);
int n=getkey()-1;
if(n+1==1) exit(0);
while(n<1||n>4)
{ gotoxy(26,22);
printf("Invalid choice. enter again...");
n=getkey()+1;
}
return (2*n+1);
}
void prnt_side_sum(int sqr[10][10],int n)
{
int i,sum=0;
for(i=0;i<n;i++)
{ sum+=sqr[0][i];
}
gotoxy(25,1);
printf("SUM OF NO. OF EACH SIDE = %d ",sum);
}
int ay[10][10];
void tip(int sqr[10][10],int n)
{
int take=0,i,j,x=0,y=0;
switch(n)
{
case 3: take =3;
break;
case 5: take =9;
break;
case 7: take= 16;
break;
case 9: take= 25;
break;
}
randomize();
for(i=0;i<take;i++)
{
x=(random(n*1000))/1000;
y=(random(n*1000))/1000;
ay[x][y]=sqr[x][y];
}
}
void prnt_tip(int ay[10][10],int x1,int y1,int n)
{
int i,j,cur=0,x2=0,y2=0;
char array[40];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(ay[i][j]!=0)
{
x2=j*50+x1;
y2=i*50+y1;
cur=ay[i][j];
hidemouseptr();
moveto(x2+15,y2+10);
sprintf(array,"%d",cur);
outtext(array);
showmouseptr();
}
}
}
}
void magic()
{
int status, button=0, x, y;
char array[50]={0},ar[50],ch='y';
int i,j,x1=0,x2=0,y1=0,y2=0,x3=0,y3=0;
int count=1,mx,my;
int mi,mj,prev,cur=0;
float k;
int n=level();
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\BGI");
settextstyle(GOTHIC_FONT,0,2);
cleardevice();
mx=getmaxx()/2;
my=getmaxy()/2;
gen_arry(n);
tip(sqr,n);
prnt_side_sum(sqr,n);
k=n;
x1=mx-(k/2)*50;
x3=x1+k*50;
y1=my-(k/2)*50;
y3=y1+k*50;
setcolor(1);
setfillstyle(1,2);
rectangle(x1,y1,x3,y3);
floodfill(x1+1,y1+1,1);
for(i=0;i<n;i++)
{
line(x1+i*50,y1,x1+i*50,y3);
line(x1,y1+i*50,x3,y1+i*50);
}
restrictmouseptr(x1+5,y2+5,x3-5,y3-5);
showmouseptr();
prnt_tip(ay,x1,y1,n) ;
while(ch=='y')
{
getmousepos(&button,&x,&y);
gotoxy(10,10);
mi=((x-x1)/50);
mj=((y-y1)/50);
if(ay[mj][mi]!=0)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ if(ay[i][j]==count)
{
count++;
break;
}
}
}
if( button == 1 )
{
x2=((x-x1)/50)*50+x1;
y2=((y-y1)/50)*50+y1;
delay(126);
if(ay[mj][mi]==0)
{
ay[mj][mi]=count++;
cur=ay[mj][mi];
hidemouseptr();
moveto(x2+15,y2+10);
sprintf(array,"%d",cur);
outtext(array);
showmouseptr();
}
button=-1;
}
else if(button==2)
{
button=-1;
delay(126);
hidemouseptr();
delay(126);
cleardevice();
setcolor(2);
if(chek(sqr,ay,n))
{ setcolor(4);
settextstyle(4,0,4);
outtextxy(180,200,"T R Y A G A I N");
}
else
{
setcolor(1);
settextstyle(4,0,4);
outtextxy(180,150,"W E L L D O N E");
setcolor(2);
outtextxy(210,250,"Y O U W I N");
}
gotoxy(27,24);
printf("Do you want to try again?(y/n)");
if(getkey()==21)
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ ay[i][j]=0,sqr[i][j]=0;}
closegraph();
magic();
}
else
exit(0);
}
}
button=-1;
clearviewport();
closegraph();
}
void main()
{
openmenu();
magic();
}
//End of the program
Comments
Post a Comment