推箱子:
1、确定数字与字符的对应关系。
0 printf(" “);
2 printf(”@");
3 printf("#");
4 printf(" " ) ; 5 p r i n t f ( " O " ) ; 9 p r i n t f ( " "); 5 printf("O"); 9 printf(" ");5printf("O");9printf("");
2、定义地图
char map[8][8] = {
{0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3},
};
3、定义记录小人儿位置的变量。
6 3
4、进入循环
1、清理屏幕并显示地图。
2、检查是否完成任务
检查数组中9的个数
任务完成后,显示使用了多少步。
3、获取方向键
1、前面是墙
2、前面是路或目标
目标位置 += 2
原位置 -= 2
3、前面是路或箱子
检查前面的前面必须是目标或路
前面的前面 += 4
前面 -= 4
前面 += 4
原位置 -= 2
4、记录走的步数
#include
#include
#include
int main()
{
char map[8][8] = {
{0,0,3,3,3,3,0,0},
{0,0,3,5,5,3,0,0},
{0,3,3,0,5,3,3,0},
{0,3,0,0,4,5,3,0},
{3,3,0,4,0,0,3,3},
{3,0,0,3,4,4,0,3},
{3,0,0,2,0,0,0,3},
{3,3,3,3,3,3,3,3},
};
int x = 6 , y = 3 , step = 0;
for(;;)
{
system("clear");
int cnt = 0;
for(int i=0; i<8; i++)
{
for(int j=0; j<8; j++)
{
switch(map[i][j])
{
case 0: printf(" "); break;
case 2: printf("@ "); break;
case 3: printf("# "); break;
case 4: printf("$ "); break;
case 5: printf("O "); break;
case 7: printf("@ "); break;
case 9: printf("$ "); cnt++;
}
}
printf("\n");
}
if(4 == cnt)
{
printf("恭喜完成任务,一共走%d步!\n", step);
return 0;
}
int ox = 0 , oy = 0;
switch(getch())
{
case 183: ox--; break;
case 184: ox++; break;
case 185: oy++; break;
case 186: oy--; break;
}
if(0 == map[x+ox][y+oy] || 5 == map[x+ox][y+oy])
{
map[x+ox][y+oy] += 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;
continue;
}
if((4 == map[x+ox][y+oy] || 9==map[x+ox][y+oy]) &&
(0 == map[x+ox*2][y+oy*2] || 5 == map[x+ox*2][y+oy*2]))
{
map[x+ox*2][y+oy*2] += 4;
map[x+ox][y+oy] -= 2;
map[x][y] -= 2;
x += ox;
y += oy;
step++;
}
}
}
运行结果:
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713170405279.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjEyNDk4NQ==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200713170312437.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NjEyNDk4NQ==,size_16,color_FFFFFF,t_70)