写个推箱子的c语言代码
时间: 2023-09-24 20:13:43 浏览: 39
下面是一份简单的推箱子代码:
```
#include <stdio.h>
#include <stdlib.h>
#define ROW 5
#define COL 5
char map[ROW][COL]={
{'#','#','#','#','#'},
{'#','S','.','.','#'},
{'#','.','#','.','#'},
{'#','.','.','T','#'},
{'#','#','#','#','#'}
};
void printMap()
{
int i, j;
for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
printf("%c ", map[i][j]);
}
printf("\n");
}
printf("\n");
}
void move(int x, int y, int x2, int y2)
{
map[x2][y2]=map[x][y];
map[x][y]='.';
}
int main()
{
int x, y, x2, y2;
printMap();
while(1)
{
printf("请输入要移动的方向(W/A/S/D):");
char c=getchar();
getchar();
switch(c)
{
case 'w':
case 'W':
x=0; y=0;
for(x=0; x<ROW; x++)
{
for(y=0; y<COL; y++)
{
if(map[x][y]=='S')
{
if(map[x-1][y]!='#')
{
if(map[x-1][y]=='.')
{
move(x, y, x-1, y);
}
else
{
if(map[x-2][y]=='.')
{
move(x, y, x-2, y);
move(x-1, y, x-1, y);
}
}
}
break;
}
}
if(map[x][y]=='S')
{
break;
}
}
break;
case 's':
case 'S':
x=0; y=0;
for(x=0; x<ROW; x++)
{
for(y=0; y<COL; y++)
{
if(map[x][y]=='S')
{
if(map[x+1][y]!='#')
{
if(map[x+1][y]=='.')
{
move(x, y, x+1, y);
}
else
阅读全文
相关推荐
















