/******push box game******/
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define SPACE 0//空地
#define WALL 1//墙
#define PLAYER 2//玩家
#define BOX 3//箱子
#define DEST 4//终点
#define P_DEST 6//玩家和终点
#define B_DEST 7 //箱子和终点
#define ROW 9//地图行
#define COL 11//地图列
#define Maxstep1 88//第一关最大步数
#define Maxstep2 45//第二关最大步数
int map[2][ROW][COL]=
{
{
{0,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,1,0},
{0,1,0,0,3,0,0,3,0,1,0},
{0,1,0,3,0,3,3,0,1,1,0},
{0,1,0,0,0,2,0,0,1,1,0},
{1,1,0,0,4,0,3,0,1,1,0},
{1,0,0,4,0,4,0,0,0,1,0},
{1,0,4,4,0,4,4,3,0,1,0},
{1,1,1,1,1,1,1,1,1,1,0}
},
{
{0,0,1,1,0,0,0,1,1,0,0},
{0,1,0,0,1,0,1,0,0,1,0},
{1,0,0,0,0,1,0,0,0,0,1},
{1,0,3,0,0,0,0,4,0,0,1},
{0,1,0,0,3,0,4,0,4,1,0},
{0,0,1,0,0,0,3,0,1,0,0},
{0,0,0,1,0,2,0,1,0,0,0},
{0,0,0,0,1,0,1,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0},
}
} ;
int level = 0;//初始化游戏关卡
int step = 0 ;//初始化游戏步数
void Begin(); //游戏开始介绍
void Show_Step();//计算步数
void DesignMap();//绘制地图
void Push();//定义人物游戏操作
void WinJudge();//游戏胜利祝贺
void LoseJudge();//游戏失败惋惜
void Begin()
{
printf("——————————————-");
printf("\n");
printf("| |");
printf("\n");
printf("| |");
printf("\n");
printf("| 欢迎来到推箱子小游戏 |");
printf("\n");
printf("| |");
printf("\n");
printf("| |");
printf("\n");
printf("——————————————-") ;
printf("\n\n\n");
printf("操作指南:W S A D控制上下左右运动");
printf("\n\n\n");
printf("温馨提示:请在规定步数内完成游戏,否则游戏失败!\n\n");
system("pause");//暂停程序
}
void Show_Step()
{
if(level==0)
{
printf("\n");
printf("——————");
printf("\n");
printf("| %d/%d |",step,Maxstep1);
printf("\n");
printf("——————");
}
else
{
printf("\n");
printf("——————");
printf("\n");
printf("| %d/%d |",step,Maxstep2);
printf("\n");
printf("——————");
}
}
void DesignMap()
{
WinJudge();
int i,j;
if(level!=2)
{
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
switch(map[level][i][j])
{
case SPACE:
printf(" ");//空地
break;
case WALL:
printf("■");//墙
break;
case PLAYER:
printf("♂");//人
break;
case BOX:
printf("*");//箱子
break;
case DEST:
printf("◎");//终点
break;
case P_DEST:
printf("♀");//人在终点时
break;
case B_DEST:
printf("√");//箱子在终点时
break;
}
}
printf("\n");
}
Show_Step();
}
}
void Push()
{
int row,column=0;//定义人物所在行和列
int i,j;
char PushDrection;//定义方向
//获取人物所在位置
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
if(map[level][i][j]==PLAYER||map[level][i][j]==P_DEST)
{
row=i;
column=j;
}
}
}
PushDrection=getch();//获取键值
//移动后地图的变化设置
switch(PushDrection)
{
case 'W':
case 'w'://控制“↑”的移动
if(map[level][row-1][column]==SPACE||map[level][row-1][column]==DEST)
{
map[level][row][column]-=PLAYER;//如果此位置只有人,则向上移动后变为空地,如果此位置是人和终点则移动后变为终点
map[level][row-1][column]+=PLAYER;
step++;//如果此位置为空地则人物移动后变为人所在地,如果此位置是终点,则移动后变为人和终点
}
else
{
if(map[level][row-2][column]==SPACE||map[level][row-2][column]==DEST)
{
if(map[level][row-1][column]==BOX||map[level][row-1][column]==B_DEST)
{
map[level][row][column]-=PLAYER;
map[level][row-1][column]-=WALL;
map[level][row-2][column]+=BOX;
step++;
}
}
}
break;
case 'S':
case 's'://控制“↓” 的移动
if(map[level][row+1][column]==SPACE||map[level][row+1][column]==DEST)
{
map[level][row][column]-=PLAYER;
map[level][row+1][column]+=PLAYER;
step++;
}
else
{
if(map[level][row+2][column]==SPACE||map[level][row+2][column]==DEST)
{
if(map[level][row+1][column]==BOX||map[level][row+1][column]==B_DEST)
{
map[level][row][column]-=PLAYER;
map[level][row+1][column]-=WALL;
map[level][row+2][column]+=BOX;
step++;
}
}
}
break;
case 'A':
case 'a'://控制“ ←”的移动
if (map[level][row][column-1]==SPACE||map[level][row][column-1]==DEST)
{
map[level][row][column]-=PLAYER;
map[level][row][column-1]+=PLAYER;
step++;
}
else
{
if(map[level][row][column-2]==SPACE||map[level][row][column-2]==DEST)
{
if(map[level][row][column-1]==BOX||map[level][row][column-1]==B_DEST)
{
map[level][row][column]-=PLAYER;
map[level][row][column-1]-=WALL;
map[level][row][column-2]+=BOX;
step++;
}
}
}
break;
case 'D':
case 'd'://控制“→”的移动
if(map[level][row][column+1]==SPACE||map[level][row][column+1]==DEST)
{
map[level][row][column]-=PLAYER;
map[level][row][column+1]+=PLAYER;
step++;
}
else
{
if(map[level][row][column+2]==SPACE||map[level][row][column+2]==DEST)
{
if(map[level][row][column+1]==BOX||map[level][row][column+1]==B_DEST)
{
map[level][row][column]-=PLAYER;
map[level][row][column+1]-=WALL;
map[level][row][column+2]+=BOX;
step++;
}
}
}
break;
}
LoseJudge();
}
void WinJudge()
{
//当箱子全部在终点时判断游戏胜利
int k;
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
if(map[level][i][j]==BOX)
{
k++;
}
}
}
if(k==0)
{
level++;
step=0;//步数重置
}
if(level==2)
{
printf("Win!Congratulation!\n");
}
}
void LoseJudge()
{
if(level==0)
{
if(step>Maxstep1)
{
printf("\n失败了有点遗憾");
level+=2;
}
}
if(level==1)
{
if(step>Maxstep2)
{
printf("\n失败了有点遗憾");
level+=1;
}
}
}
//主函数
int main()
{
Begin();
while(level!=2)
{
system("cls");
DesignMap();
Push();
}
return 0;
}