该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/*拼图(数字型)通过1,2,3,4控制空白位置移动,使1--8顺序排列*/
/*有很多可改进之处,你自己再改改吧*/
#include
#include
#include
void screen(short *);/*屏幕显示*/
int judge(short *); /*是否已完成的判断*/
int flag=0;/*是否已完成标志*/
main()
{
int i,j;
char ch;
short arror[9];
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"D:\\TurboC");/*改为自己的图形驱动所在位置*/
cleardevice();
setactivepage(0);
setvisualpage(0);
randomize();
for(i=0;i<=8;i++)
{
arror[i]=random(9);
for(j=0;j
if(arror[j]==arror[i])
{
i=i-1;
break;
}
}
screen(arror);
while(!flag)
{
i=0;
while(arror[i])i++;
ch=getch();
switch(ch)
{
case '1':if(i%3!=0){arror[i]=arror[i-1];arror[i-1]=0;}screen(arror);break;
case '2':if((i+1)%3!=0){arror[i]=arror[i+1];arror[i+1]=0;}screen(arror);break;
case '3':if(i/3!=0){arror[i]=arror[i-3];arror[i-3]=0;}screen(arror);break;
case '4':if(i/3!=2){arror[i]=arror[i+3];arror[i+3]=0;}screen(arror);break;
default:flag=1;break;/*任意键退出*/
}
}
}
int judge(short arror[9])
{
int i;
for(i=0;i<=8;i++)if(arror[i]!=i-1)return 0;
return 1;
}
void screen(short arror[9])/*屏幕显示*/
{
int i;
cleardevice();
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 6);
setusercharsize(1, 1, 1, 1);
setfillstyle(1,4);
outtextxy(10,10,"1:left; 2:right; 3:up; 4:down; other:quit");
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 6);
setusercharsize(2, 1, 1, 1);
setfillstyle(1,4);
rectangle(100,100,150,150);rectangle(150,100,200,150);rectangle(200,100,250,150);
rectangle(100,150,150,200);rectangle(150,150,200,200);rectangle(200,150,250,200);
rectangle(100,200,150,250);rectangle(150,200,200,250);rectangle(200,200,250,250);
for(i=0;i<=8;i++)
{
switch(arror[i])
{
case 0:floodfill(100+i%3*50+15,100+i/3*50+15,0);break;
case 1:outtextxy(100+i%3*50+10,100+i/3*50+10,"1");break;
case 2:outtextxy(100+i%3*50+10,100+i/3*50+10,"2");break;
case 3:outtextxy(100+i%3*50+10,100+i/3*50+10,"3");break;
case 4:outtextxy(100+i%3*50+10,100+i/3*50+10,"4");break;
case 5:outtextxy(100+i%3*50+10,100+i/3*50+10,"5");break;
case 6:outtextxy(100+i%3*50+10,100+i/3*50+10,"6");break;
case 7:outtextxy(100+i%3*50+10,100+i/3*50+10,"7");break;
case 8:outtextxy(100+i%3*50+10,100+i/3*50+10,"8");break;
}
}
return;
}