#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
// 0 -> ' '空地
// 1 -> ▓墙
// 2 -> 人
// 3 -> □箱子
// 4 -> ☆目的地
// 5 -> ★箱子所在目的地
#define M 8
void draw();
int map[M][8] = {
{0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 1, 4, 1, 0, 0, 0},
{0, 0, 1, 0, 1, 1, 1, 1},
{1, 1, 1, 3, 0, 3, 4, 1},
{1, 4, 0, 3, 2, 1, 1, 1},
{1, 1, 1, 1, 3, 1, 0, 0},
{0, 0, 0, 1, 4, 1, 0, 0},
{0, 0, 0, 1, 1, 1, 0, 0}
};
int indexX = 5, indexY = 5;
int oldValue; // 记录一下小人下一个位置的数值
// 画图函数
__inline void draw()
{
int i, j;
for(i = 0; i < M; i++)
{
for(j = 0; j < 8; j++)
{
switch(map[i][j])
{
case 0:
printf(" ");//空地
break;
case 1:
printf("▓");//墙
break;
case 2:
printf("");//人
break;
case 3:
printf("□");//箱子
break;
case 4:
printf("☆");//目的地
break;
case 5:
printf("★");//箱子在目的地
break;
}
}
printf("\n");
}
}
void fun1()
{
}
void fun2(int v1)
{
printf("fun2 call....\n");
fun1();
}
void move(int row, int colume)
{
if(map[indexX + row][indexY + colume] == 1)
return;
if(map[indexX + row][indexY + colume] == 3)
{
if(map[indexX + 2 * row][indexY + 2 * colume] == 0)
{
map[indexX + 2 * row][indexY + 2 * colume] = 3;
map[indexX + row][indexY + colume] = 0;
}
else if(map[indexX + 2 * row][indexY + 2 * colume] == 4)
{
map[indexX + 2 * row][indexY + 2 * colume] = 5;
map[indexX + row][indexY + colume] = 0;
}
else
return;
}
if(map[indexX + row][indexY + colume] == 5)
{
if(map[indexX + 2 * row][indexY + 2 * colume] == 0)
{
map[indexX + 2 * row][indexY + 2 * colume] = 3;
map[indexX + row][indexY + colume] = 0;
}
else if(map[indexX + 2 * row][indexY + 2 * colume] == 4)
{
map[indexX + 2 * row][indexY + 2 * colume] = 5;
map[indexX + row][indexY + colume] = 0;
}
else
return;
}
// 更改地图小人的位置
map[indexX][indexY] = oldValue;
indexX += row;
indexY += colume;
oldValue = map[indexX][indexY]; // 记录下一个位置的数值
map[indexX][indexY] = 2;
}
int main()
{
int i, j;
char ch;
draw();
while(-1)
{
ch = getch();
switch(ch)
{
case 'w':
move(-1, 0);
break;
case 's':
move(1, 0);
break;
case 'a':
move(0, -1);
break;
default:
move(0, 1);
break;
}
system("cls");
draw();
}
printf("Welcome to the game\n");
system("pause");
return 0;