// 作者:火浇青春
// 日期:20151125
#include <stdio.h>
#define N 10 //宏定义,确定地图的宽度
#define M 10 //宏定义,确定地图的长度
void scene(char put[N][M]);//游戏背景输出函数的声明
int caozuo(char put[N][M]);//游戏操作函数的声明
int sit(char put[N][M]);
int main(int argc, char const *argv[])
{
//1.游戏初始背景,'#'代表墙,'O'代表人,'X'代表箱子
char put[N][M];//2.数组put的定义
caozuo(put);//3.输入字母对小人进行操作
return 0;
}
void scene(char put[N][M])//游戏背景刷新函数
{
int i,j;
for ( i = 0; i < N; ++i)
{
for (j = 0; j < M; ++j)
printf("%c",put[i][j] );
printf("\n");
}
}
int sit(char put[N][M])//此函数用来寻找人的初始坐标
{
int i, j;
for ( i = 0; i <= N; ++i)
for ( j = 0; j <= M; ++j)
if (put[i][j]=='O')
return j;
}
int caozuo(char put[N][M])//对小人进行操作,控制小人行走
{
int guan=1;//用来显示关数
fu:
switch(guan)//开关语句,用来选关 ~\(≧▽≦)/~
{
case 1:beijing1(put);break;//游戏第一关背景
case 2:beijing2(put);break;//游戏第二关背景
case 3:beijing3(put);break;//游戏第三关背景
}
int i;//i为横坐标
int j = sit(put);//寻找人的初始坐标
for ( i = 0; i < M; ++i)
if ('O'==put[i][j])
break;
printf("\n\n\n\n\n\n\n\n\n\n\n第%d关\n",guan);
scene(put);//2.输出游戏背景
char play;//变量,用来存放输入的字母,用来控制方向
while(1)//while循环语句,里面放'1'表示以下永远成立,可以执行,遇到break则推出
{
printf("请输入w,s,a,d来控制小人行动:\n");
scanf(" %c",&play);//输入操作字符
switch(play)//多分支选择语句
{
case 'w'://向上
if(put[i-1][j]!='#')//判断人的上面是否有墙
{
if(put[i-1][j]!='X')//判断人的上面是否有箱子,如果没有则执行下面语句
{
put[--i][j]='O';
put[i+1][j]=' ';
}
else if(put[i-2][j]!='#')//如果有箱子且箱子前面不是墙,则执行下面语句
{
put[--i][j]='O';//改变并记录小人 'O' 的位置
put[i+1][j]=' ';//改变小人原来的位置,达成移动的视觉错觉
put[i-1][j]='X';//改变箱子的位置
}
}
break;
case 's'://向下
if(put[i+1][j]!='#')
{
if(put[i+1][j]!='X')
{
put[++i][j]='O';
put[i-1][j]=' ';
}
else if(put[i+2][j]!='#')
{
put[++i][j]='O';
put[i-1][j]=' ';
put[i+1][j]='X';
}
}
break;
case 'a'://向左
if(put[i][j-1]!='#')
{
if(put[i][j-1]!='X')
{
put[i][--j]='O';
put[i][j+1]=' ';
}
else if(put[i][j-2]!='#')
{
put[i][--j]='O';
put[i][j+1]=' ';
put[i][j-1]='X';
}
}
break;
case 'd'://向右
if(put[i][j+1]!='#')
{
if(put[i][j+1]!='X')
{
put[i][++j]='O';
put[i][j-1]=' ';
}
else if(put[i][j+2]!='#')
{
put[i][++j]='O';
put[i][j-1]=' ';
put[i][j+1]='X';
}
}
break;
case 'p':
{
printf("请输入你想玩的关数:\n");
scanf("%d",&guan);
goto fu;
}
}
//如果箱子到了临界点(地图的边缘),则显示过关
if('X'==put[i][M-1]||'X'==put[i][0]||'X'==put[N-1][j]||'X'==put[0][j])
{
if (3!=guan)//限制只有三关
{
scene(put);//2.输出游戏背景
printf("过关:");
printf("\n\n\n\n\n\n\n\n\n\n\n第%d关\n",guan);
guan++;//过关后进入下一关
}
else
{
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n恭喜通关!!!\n\n\n\n\n\n\n\n\n\n");
return 0;
}
goto fu;
}
printf("\n\n\n\n\n\n\n\n\n\n\n第%d关\n",guan);
scene(put);//2.输出游戏背景
}
}
int beijing1(char put[N][M])//第一关地图背景
{
int i,j;
char pu[N][M]={
{'#','#','#','#','#','#','#','#','#','#'},
{'#',' ','O',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','X','#','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ','#','#',' ',' ',' ','#'},
{'#',' ',' ','#','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'#','#','#','#','#','#','#','#','#','#'}};
for(i=0;i<N;i++)
for(j=0;j<M;j++)
put[i][j]=pu[i][j];
return 0;
}
int beijing2(char put[N][M])//第二关地图背景
{
int i,j;
char pu[N][M]={
{'#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ','#','#','#','#',' ',' ','#'},
{'#','O','X','#','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ','#','#',' ',' ',' ','#'},
{'#',' ',' ','#','#','#','#',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ',' ',' ','#','#','#',' ','#','#'},
{'#','#',' ',' ',' ',' ',' ',' ',' ','|'},
{'#','#','#','#','#','#','#','#','#','#'}};
for(i=0;i<N;i++)
for(j=0;j<M;j++)
put[i][j]=pu[i][j];
return 0;
}
int beijing3(char put[N][M])//第三关地图背景
{
int i,j;
char pu[N][M]={
{'#','#','#','#','#','#','#','#','#','#'},
{'#',' ',' ','#',' ',' ','#',' ',' ','#'},
{'#','O','X','#',' ',' ',' ',' ',' ','#'},
{'#','#',' ','#',' ',' ','#','#',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','|'},
{'#',' ',' ',' ',' ','#','#',' ','#','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ','#'},
{'#',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#',' ',' ',' ',' ','#'},
{'#','#','#','#','#','#','#','#','#','#'}};
for(i=0;i<N;i++)
for(j=0;j<M;j++)
put[i][j]=pu[i][j];
return 0;
}