📄 拼图游戏.htm
字号:
width="85%" bgColor=#dddddd height=20><STRONG><FONT
color=#003399 size=4><B>拼图游戏
</B></FONT></STRONG></TD><BR></TR>
<TR>
<TD align=middle width="100%"><BR></TD></TR>
<TR>
<TD style="FONT-SIZE: 9pt" align=middle
width="100%">发表日期:2003年10月27日 出处:源创 作者:litigo 已经有2787位读者读过此文</TD></TR>
<TR>
<TD align=middle width="100%"><!--下面的这一句是设置阅读文本区的宽度-->
<TABLE style="TABLE-LAYOUT: fixed" cellSpacing=0
cellPadding=0 width="90%" align=center border=0>
<TBODY>
<TR>
<TD align=middle width="100%"></TD></TR>
<TR>
<TD style="WORD-WRAP: break-word"><FONT
class=news><BR>
<P>/*游戏名智力拼图*/<BR>/*在tc3.0++下运行*/<BR>/*游戏中保存与空格相邻方格的位置我用的是单循环键表,其实普通的键表就可以了*/</P>
<P>#include<stdlib.h><BR>#include<dos.h><BR>#include<conio.h><BR>#include<bios.h><BR>#define
KEY_ENTER 0x1c0d<BR>#define KEY_ESC
0x11b<BR>#define KEY_UP 0x4800<BR>#define KEY_DOWN
0x5000<BR>#define KEY_LEFT 0x4b00<BR>#define
KEY_RIGHT 0x4d00</P>
<P><BR>//定义核心数据<BR>typedef struct attr<BR>
{<BR> int a[4][4];<BR> int
b[4][4];<BR> }ATTR;<BR>ATTR rect;</P>
<P>struct
blank<BR>
{<BR> int x, y;<BR> struct blank
*next;<BR>
};<BR>struct blank *head;</P>
<P>int
xpos=39,ypos=13,xago,yago,xb=39,yb=13;
<BR>
/*(xpos,ypos)表示当前位置,(xago,yago)表示上次位置,(xb,yb)表示空格的位置*/</P>
<P>void setmode();<BR>void inita();<BR>void
initb();<BR>void drawpic();<BR>void
drawgoalpic();<BR>void puthelp();<BR>void
establish();<BR>void putrect();<BR>void
recover();<BR>int judge(int *,int *);<BR>int
win();<BR>int search(int *,int *);<BR>void
freemem();</P>
<P>void main()<BR>{<BR>int
key,t,i,j;<BR>textbackground(BLACK);<BR>clrscr();<BR>setmode();<BR>inita();
/*初始化a数组*/<BR>initb();
/*初始化b数组*/<BR>drawpic();
/*画开始时游戏的画面*/<BR>drawgoalpic();<BR>puthelp();<BR>head=(struct
blank * )malloc(sizeof (struct
blank));<BR>head->x=0;head->y=0;<BR>head->next=head;<BR>establish();
/*创建链表记录与方格相邻方格的位置*/<BR>do
/*处理按键*/<BR>
{<BR>
key=bioskey(0);<BR>
xago=xpos;yago=ypos;<BR>
switch(key)<BR> {<BR> case
KEY_UP:<BR> {<BR>
ypos--;<BR>
if(judge(&xpos,&ypos))
/*如果光标位置超出图形范围,则不执行下出*/<BR>
{ypos++;<BR>
continue;}<BR>
putrect();<BR>
recover();<BR>
break;<BR> }<BR> case
KEY_DOWN:{<BR>
ypos++;<BR>
if(judge(&xpos,&ypos))
/*如果光标位置超出图形范围,则不执行下出*/<BR>
{ypos--;<BR>
continue;}<BR>
putrect();<BR>
recover();<BR>
break;<BR> }<BR>
case KEY_LEFT:{<BR>
xpos-=3;<BR>
if(judge(&xpos,&ypos))
/*如果光标位置超出图形范围,则不执行下出*/<BR>
{xpos+=3;<BR>
continue;}<BR>
putrect();<BR>
recover();<BR>
break;<BR> }<BR>
case KEY_RIGHT:{<BR>
xpos+=3;<BR>
if(judge(&xpos,&ypos))
/*如果光标位置超出图形范围,则不执行下出*/<BR>
{xpos-=3;<BR>
continue;}<BR>
putrect();<BR>
recover();<BR>
break;<BR> }<BR>
case KEY_ENTER:{<BR>
if(!search(&xpos,&ypos)||rect.b[ypos-10][xpos/3-10]=='B')
continue;<BR>
gotoxy(xb,yb);<BR>
textattr(RED*16|GREEN);<BR>
cprintf(" %c
",rect.a[ypos-10][xpos/3-10]);<BR>
gotoxy(xpos,ypos);<BR>
textattr(BLUE*16|GREEN);<BR>
cprintf(" %c ",32);<BR>
rect.b[ypos-10][xpos/3-10]='B';
/*改变(xpos,ypos)与(xb,yb)两点的属性*/<BR>
rect.b[yb-10][xb/3-10]='P';<BR>
t=rect.a[ypos-10][xpos/3-10];<BR>
rect.a[ypos-10][xpos/3-10]=rect.a[yb-10][xb/3-10];<BR>
rect.a[yb-10][xb/3-10]=t;<BR>
t=xpos;xpos=xb;xb=t;<BR>
t=ypos;ypos=yb;yb=t;
/*交换(xpos,ypos)与(xb,yb)的位置*/<BR>
gotoxy(1,1);<BR>
freemem();
/*由于空格位置的改变,与空格相邻方格的位置也要改变*/<BR>
/*先释放链表占用的内存,再重新创建链表记录与空格相邻方格的位置*/<BR>
establish();<BR>
}<BR>
}<BR>if(win())<BR>
{<BR>
gotoxy(20,7);<BR> cprintf("well
done ,yon have completed the
misson");<BR>
gotoxy(19,7);<BR>
}<BR>}while(key!=KEY_ESC);<BR>free(head);<BR>}</P>
<P>void
setmode()<BR>{<BR>_AL=3;<BR>_AH=0;<BR>geninterrupt(0x10);<BR>}</P>
<P>void initb()<BR> {<BR>
int i,j;<BR>
for(i=0;i<4;i++)<BR>
for(j=0;j<4;j++)<BR>
rect.b[i][j]='P';<BR>
rect.b[3][3]='B';<BR> }</P>
<P>void inita()<BR>
{<BR> int
n,i,j,c[4][4]={0};<BR>
c[3][3]=1;<BR> long int
t;<BR>
srand(time(&t));<BR>
for(n=1;n<=15;n++)<BR>
{<BR>
do<BR> {<BR> i=random(4);<BR>
j=random(4);<BR>
}while(c[i][j]==1);<BR>
rect.a[i][j]=n+64;<BR>
c[i][j]=1;<BR>
}<BR> }</P>
<P>void drawpic()<BR>
{<BR> int
j;<BR>
textattr(BLUE*16|GREEN);<BR>
gotoxy(30,10);</P>
<P>
for(j=0;j<4;j++)<BR>
cprintf(" %c
",rect.a[0][j]);<BR>
gotoxy(30,11);<BR>
for(j=0;j<4;j++)<BR>
cprintf(" %c
",rect.a[1][j]);<BR>
gotoxy(30,12);<BR>
for(j=0;j<4;j++)<BR> cprintf(" %c
",rect.a[2][j]);<BR>
gotoxy(30,13);<BR>
for(j=0;j<3;j++)<BR> cprintf(" %c
",rect.a[3][j]);<BR>
textattr(RED*16|GREEN);<BR>
cprintf(" %c ",32);<BR>
gotoxy(1,1);<BR> }</P>
<P>void drawgoalpic()<BR>
{<BR> int
i,j,n=65;<BR>
textattr(BLUE*16|GREEN);<BR>
for(i=0;i<4;i++)<BR> {<BR>
gotoxy(10,10+i);<BR>
for(j=0;j<4;j++)<BR>
cprintf(" %c
",n++);<BR> }<BR>
gotoxy(19,13);<BR> cprintf(" %c
",32);<BR> }</P>
<P>int judge(int *xp,int *yp)
/*判断当前光标是否超出图形范围,1表示超出,0表示没有超出*/<BR>
{<BR>
if(*xp<30||*xp>39||*yp<10||*yp>13)
return 1;<BR> else return
0;<BR> }</P>
<P>void insert(int *p1,int
*p2)<BR>
{<BR> struct blank
*p;<BR> p=(struct
blank*)malloc(sizeof(struct
blank));<BR>
p->x=*p1;p->y=*p2;<BR>
p->next=head->next;<BR>
head->next=p;head=p;<BR>
}</P>
<P>void establish()
/*用链表记录与方格相邻方格位置,与空格相邻的方格最多只有四个*/<BR>
{<BR> int
xbup,ybup,xbdown,ybdown,xbleft,ybleft,xbright,ybright;<BR>
xbup=xb;ybup=yb-1;xbdown=xb;ybdown=yb+1;<BR>
xbleft=xb-3;ybleft=yb;xbright=xb+3;ybright=yb;<BR>
/*先在内存中分配空间给结点。并把零值赋给它,便于插入链表记录与空格相邻*/</P>
<P>
/*的方格位置,但是这个内存空就不记录方格位置*/<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -