经过修改并加了注解
运行程序后,会产生9个数字,当人把数字拼成
1 2 3
4 5 6
7 8 9
你就过关了,很简单的。
#include<math.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
#define left 0x4b
#define right 0x4d
#define down 0x50
#define esc 0x01
#define up 0x48
#define col 4
#define row 3
int a[9]=;
int board[3][3];//面板
int k,m;//9的坐标
int get_key(void);
void Draw9(int x,int y,char s[]);//显示方格9
void Draw(int x,int y,char s[]);//显示其它方格
main()
{
int gdriver=VGA,gmode=VGAHI,errorcode;
int i,j,key,dec=9,temp,temp1;
char *s[]=;
initgraph(&gdriver,&gmode,"d:\tc");
errorcode=graphresult();
if(errorcode!=0)
{printf("the graph error:%s",grapherrormsg(errorcode));
getch();
printf("press any key to halt:");
exit(1);
}
/*将个数字随机放在9个格子里*/
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{temp=rand()%dec;dec--;
board[i][j]=a[temp];
for(temp1=temp;temp1<dec;temp1++)
a[temp1]=a[temp1+1];
}
setcolor(15);
rectangle(198,148,351,301);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
}
while(1)
{while(bioskey(1)==0);
key=get_key();
switch(key)
{case up:if(k>0)
{board[k][m]=board[k-1][m];
board[k-1][m]=9;
k--;
Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
Draw((col+m)*50,(row+k+1)*50,s[board[k+1][m>);
if(judge()){printf("you win!!!,press any key to exit");getch();exit(0); } }
break;
case down:if(k<2)
{
board[k][m]=board[k+1][m];
board[k+1][m]=9;
k++;
Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
Draw((col+m)*50,(row+k-1)*50,s[board[k-1][m>);
if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
}
break;
case left:if(m>0)
{
board[k][m]=board[k][m-1];
board[k][m-1]=9;
m--;
Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
Draw((col+m+1)*50,(row+k)*50,s[board[k][m+1>);
if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
}
break;
case right:if(m<2)
{board[k][m]=board[k][m+1];
board[k][m+1]=9;
m++;
Draw9((col+m)*50,(row+k)*50,s[board[k][m>);
Draw((col+m-1)*50,(row+k)*50,s[board[k][m-1>);
if(judge()){printf("you win!!!,press any key to exit");getch();exit(0);}
}
break;
case esc:exit(0);
}
}
}
int get_key(void)
{union REGS reg;
reg.h.ah=0;
int86(0x16,?,?);
return(reg.h.ah);
}
void Draw9(int x,int y,char s[])/* x, y为左上角坐标sizex,sizey为长和宽*/
{int sizx=50-1;
int sizy=50-1;
setcolor(0);/*这里最好用灰色*/
line(x,y,x+sizx-1,y);
line(x,y+1,x+sizx-2,y+1);
line(x,y,x,y+sizy-1);
line(x+1,y,x+1,y+sizy-2);
setcolor(8);/*这里最好用深灰色*/
line(x+1,y+sizy,x+sizx,y+sizy);
line(x+2,y+sizy-1,x+sizx,y+sizy-1);
line(x+sizx-1,y+1,x+sizx-1,y+sizy);
line(x+sizx,y+2,x+sizx,y+sizy);
setcolor(15);/*这里最好用白色*/
putpixel(x,y+sizy,3);
putpixel(x+1,y+sizy-1,3);
putpixel(x+sizx,y,3);
putpixel(x+sizx-1,y+1,3);
setfillstyle(1, 7);/*这里最好用灰色,设置填充模式*/
bar(x+2,y+2,x+sizx-2,y+sizy-2);setcolor(0);
setcolor(0);
settextboardify(CENTER_TEXT,CENTER_TEXT);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(x+25,y+25,s);
}
void Draw(int x,int y,char s[])/* x, y为左上角坐标sizex,sizey为长和宽*/
{int sizx=50-1;
int sizy=50-1;
setcolor(15);/*这里最好用白色*/
line(x,y,x+sizx-1,y);
line(x,y+1,x+sizx-2,y+1);
line(x,y,x,y+sizy-1);
line(x+1,y,x+1,y+sizy-2);
setcolor(8);/*这里最好用深灰色*/
line(x+1,y+sizy,x+sizx,y+sizy);
line(x+2,y+sizy-1,x+sizx,y+sizy-1);
line(x+sizx-1,y+1,x+sizx-1,y+sizy);
line(x+sizx,y+2,x+sizx,y+sizy);
setcolor(7);/*这里最好用灰色*/
putpixel(x,y+sizy,3);
putpixel(x+1,y+sizy-1,3);
putpixel(x+sizx,y,3);
putpixel(x+sizx-1,y+1,3);
setfillstyle(1, 7);/*这里最好用灰色,设置填充模式*/
bar(x+2,y+2,x+sizx-2,y+sizy-2);setcolor(0);
setcolor(0);
settextboardify(CENTER_TEXT,CENTER_TEXT);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy(x+25,y+25,s);
}
int judge()
{if((board[0][0]==1)&&(board[0][1]==2)&&(board[0][2]==3)&&(board[1][0]==4)&&(board[1][1]==5)&&(board[1][2]==6)&&(board[2][0]==7)&&(board[2][1]==8)&&(board[2][2]==9))
return 1;
esle return 0;
}