谁能帮我解释下这个程序,是c语言推箱子的,要答辩,看不太懂,求高手解释 5
#include"stdio.h"#include"bios.h"#defineLEFT75#defineRIGHT77#defineUPPER72#defineDOWN...
#include"stdio.h"
#include"bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct Boxss /*定义箱子结构体,其中包含坐标属性*/
{
int x,y;
};
union keyboard /*定义读取键盘码的共用体类型*/
{
unsigned int iKeyInfo;
char chKeyBit[2];
};
int fnGetKey(void) /*定义读取键盘码的函数*/
{
union keyboard uniKey1; /*定义读取键盘码的共用体变量*/
while(bioskey(1)==0); /*检测用户是否按键*/
uniKey1.iKeyInfo=bioskey(0); /*读取按键信息*/
return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII码或扩充码*/
}
void main()
{
int iKey,x=11,y=6,tx=11,ty=6; /*x,y为人物移动后坐标,tx,ty为人物移动前坐标*/
struct Boxss Box[4]; /*定义箱子数量*/
int chMap[10][10]={ /*用二维数组定义地图*/
{0,0,0,0,0,0,0,0,0,0}, /*0表示墙1表示路2表示目标*/
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
Box[0].x=13; /*定义箱子的坐标属性*/
Box[1].x=11;
Box[2].x=14;
Box[3].x=18;
Box[0].y=8;
Box[1].y=7;
Box[2].y=13;
Box[3].y=7;
while(1) /*反复进行求移动的坐标运算*/
{
for(i=0;i<10;i++) /*输出新地图(刷新地图)*/
{
gotoxy(10,5+i);
for(j=0;j<10;j++)
{
if(chMap[i][j]==0)
printf("#");
if(chMap[i][j]==1)
printf(" ");
if(chMap[i][j]==2)
printf("X");
}
}
j=0;
for(i=0;i<4;i++)
if(chMap[Box[i].y-5][Box[i].x-10]==2)
j++;
if(j==4)
{
clrscr();
printf("You Win!");
break;
}
for(i=0;i<4;i++)
{
gotoxy(Box[i].x,Box[i].y);
printf("0");
}
gotoxy(x,y);
printf("*\b");
tx=x;
ty=y;
iKey=fnGetKey();
if(iKey==LEFT&&chMap[y-5][x-1-10]!=0)
x--;
if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)
x++;
if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)
y--;
if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)
y++;
if(iKey==ESC)
{
clrscr();
printf("You Lost");
break;
}
for(i=0;i<4;i++)
if(Box[i].x==x&&Box[i].y==y)
{
Box[i].x+=(x-tx);
Box[i].y+=(y-ty);
if(chMap[Box[i].y-5][Box[i].x-10]==0)
{
Box[i].x-=(x-tx);
Box[i].y-=(y-ty);
x=tx;
y=ty;
}
break;
}
clrscr();
}
getch();
} 展开
#include"bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct Boxss /*定义箱子结构体,其中包含坐标属性*/
{
int x,y;
};
union keyboard /*定义读取键盘码的共用体类型*/
{
unsigned int iKeyInfo;
char chKeyBit[2];
};
int fnGetKey(void) /*定义读取键盘码的函数*/
{
union keyboard uniKey1; /*定义读取键盘码的共用体变量*/
while(bioskey(1)==0); /*检测用户是否按键*/
uniKey1.iKeyInfo=bioskey(0); /*读取按键信息*/
return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII码或扩充码*/
}
void main()
{
int iKey,x=11,y=6,tx=11,ty=6; /*x,y为人物移动后坐标,tx,ty为人物移动前坐标*/
struct Boxss Box[4]; /*定义箱子数量*/
int chMap[10][10]={ /*用二维数组定义地图*/
{0,0,0,0,0,0,0,0,0,0}, /*0表示墙1表示路2表示目标*/
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
Box[0].x=13; /*定义箱子的坐标属性*/
Box[1].x=11;
Box[2].x=14;
Box[3].x=18;
Box[0].y=8;
Box[1].y=7;
Box[2].y=13;
Box[3].y=7;
while(1) /*反复进行求移动的坐标运算*/
{
for(i=0;i<10;i++) /*输出新地图(刷新地图)*/
{
gotoxy(10,5+i);
for(j=0;j<10;j++)
{
if(chMap[i][j]==0)
printf("#");
if(chMap[i][j]==1)
printf(" ");
if(chMap[i][j]==2)
printf("X");
}
}
j=0;
for(i=0;i<4;i++)
if(chMap[Box[i].y-5][Box[i].x-10]==2)
j++;
if(j==4)
{
clrscr();
printf("You Win!");
break;
}
for(i=0;i<4;i++)
{
gotoxy(Box[i].x,Box[i].y);
printf("0");
}
gotoxy(x,y);
printf("*\b");
tx=x;
ty=y;
iKey=fnGetKey();
if(iKey==LEFT&&chMap[y-5][x-1-10]!=0)
x--;
if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)
x++;
if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)
y--;
if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)
y++;
if(iKey==ESC)
{
clrscr();
printf("You Lost");
break;
}
for(i=0;i<4;i++)
if(Box[i].x==x&&Box[i].y==y)
{
Box[i].x+=(x-tx);
Box[i].y+=(y-ty);
if(chMap[Box[i].y-5][Box[i].x-10]==0)
{
Box[i].x-=(x-tx);
Box[i].y-=(y-ty);
x=tx;
y=ty;
}
break;
}
clrscr();
}
getch();
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询