#include <stdio.h>
#define ROW 10
#define COL 11
void personMove(char map[ROW][COL],int oldX,int oldY,int newX,int newY){
int temp = map[oldX][oldY];
map[oldX][oldY] = map[newX][newY];
map[newX][newY] = temp;
}
int main(int argc, const char * argv[]) {
char map[ROW][COL]={
"##########",
"#O #### #",
"# X#### #",
"# #",
"###### #",
"# #### #",
"# #",
"# ######",
"# ",
"##########"};
//打印地图
for (int i = 0; i < ROW; i++) {
printf("%s\n",map[i]);
}
//定义变量,人当前位置,箱子当前位置,人移动后位置,箱子移动位置. 接受人输入方向
int personX=1,personY=1,boxX=2,boxY=2;
int personNewX=personX,personNewY=personY;
char direction,ch;
//循环控制
while (1) {
printf("请输入方向:\n");
//保存用户输入方向
scanf("%c",&direction);
scanf("%c",&ch);
personNewX=personX ;
personNewY=personY ;
switch (direction) {
//判断,人和箱子是否移动
case 'w':
personNewX--;
//人是否移动
//人前面是不是箱子箱子是否移动
break;
case 's':
personNewX++;
//人前面是不是箱子箱子是否移动
break;
case 'a':
personNewY--;
break;
case 'd':
personNewY++;
//人前面是不是箱子箱子是否移动
break;
}
if (map[personNewX][personNewY] == ' ') {
personMove(map, personX, personY, personNewX, personNewY);
personX = personNewX;
personY = personNewY;
}else if(map[personNewX][personNewY] == map[boxX][boxY]){
int boxNewX=boxX+(boxX-personX),boxNewY=boxY+(boxY-personY);
if (map[boxNewX][boxNewY] == ' ') {
personMove(map, boxX, boxY, boxNewX, boxNewY);
personMove(map, personX, personY, boxX, boxY);
personX = personNewX;
personY = personNewY;
boxX = boxNewX;
boxY = boxNewY;
}
}
for (int i = 0; i < ROW; i++) {
printf("%s\n",map[i]);
}
if (boxY == 9) {
printf("你赢了!\n");
break;
}
//箱子前面是不是墙
//箱子是否推出来了
}
return 0; |