推箱子简易版,拿来就可以跑
#include<stdio.h>
#include<conio.h>//_getch()
#include<stdlib.h>//system
int main()
{
//人:♀ 5
//箱子:★ 4
//目的地:☆ 3
//人和目的地融合:8
//墙:■ 1
//箱子推到目的地:● 7
//空地: 0
//1.设计地图:
int map[8][8] =
{
1,1,1,1,1,1,1,1,
1,3,1,0,0,4,3,1,
1,4,1,0,1,1,1,1,
1,0,0,5,0,0,1,1,
1,1,1,0,0,0,1,1,
1,3,4,0,0,4,1,1,
1,0,0,0,0,3,1,1,
1,1,1,1,1,1,1,1
};
while (1)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
switch (map[i][j])
{
case 0:
printf(" ");
break;
case 1:
printf("■");
break;
case 3:
printf("☆");
break;
case 4:
printf("★");
break;
case 7:
printf("●");
break;
case 5:
case 8:
printf("♀");
break;
}
}
printf("\n");
}
//2.控制小人移动
//先要找到小人:
int i=0, j = 0;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
if (map[i][j] == 5||map[i][j]== 8)
{
goto Next;
}
}
}
Next:
;
//3.按键控制:
char userKey = _getch();//从键盘得到不可见输入
switch (userKey)
{
case 72:
case 'W':
case 'w':
if (map[i - 1][j] == 0 || map[i - 1][j] == 3)
{
map[i - 1][j] += 5; //空地上人来了
map[i][j] -= 5; //原来的位置人走了
}
if (map[i - 1][j] == 4 || map[i - 1][j] == 7)
{
if (map[i - 2][j] == 0 || map[i - 2][j] == 3)
{
map[i - 2][j] += 4;//箱子来了
map[i - 1][j] += 1;//箱子走了人来了
map[i][j] -= 5; //人走了
}
}
break;
case 80:
case 'S':
case 's':
if (map[i + 1][j] == 0 || map[i + 1][j] == 3)
{
map[i + 1][j] += 5;
map[i][j] -= 5;
}
if (map[i + 1][j] == 4 || map[i + 1][j] == 7)
{
if (map[i + 2][j] == 0 || map[i + 2][j] == 3)
{
map[i + 2][j] += 4;//箱子来了
map[i + 1][j] += 1;//箱子走了人来了
map[i][j] -= 5; //人走了
}
}
break;
case 75:
case 'A':
case 'a':
if (map[i][j - 1] == 0 || map[i][j - 1] == 3)
{
map[i][j - 1] += 5;
map[i][j] -= 5;
}
if (map[i][j - 1] == 4 || map[i][j - 1] == 7)
{
if (map[i][j - 2] == 0 || map[i][j - 2] == 3)
{
map[i][j] -= 5; //人走了
map[i][j - 1] += 1;//箱子走了人来了
map[i][j - 2] += 4;//箱子来了
}
}
break;
case 77:
case 'D':
case 'd':
if (map[i][j + 1] == 0 || map[i][j + 1] == 3)
{
map[i][j + 1] += 5;
map[i][j] -= 5;
}
if ( map[i][j + 1] == 4 || map[i][j + 1] == 7)
{
if (map[i][j + 2] == 0 || map[i][j + 2] == 3)
{
//map[i + 2][j] += 4;//箱子来了
//map[i + 1][j] += 1;//箱子走了人来了
//map[i][j] -= 5; //人走了
map[i][j] -= 5;
map[i][j + 1] += 1;
map[i][j + 2] += 4;
}
}
break;
}
system("cls");//清屏操作
}
return 0;
}