#include <stdio.h>
#define ROW 6
#define COL 8 //有个问题,这个是7就不行,
void print_map(char map[ROW][COL]){
for (int i=0; i<ROW; i++) {
printf("%s\n",map[i]);
}
}
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## ",
"# ## ##",
"# # ##",
"## ##",
"#######"};
print_map(map);
int playerX = 1,playerY = 1;
char direction;
char ch;
printf("请输入小人移动方向:w.上 s.下 a.左 d.右\n");
while (1) {
scanf("%c",&direction);
scanf("%c",&ch);
switch (direction) {
case 'w':
case 'W':
if(map[playerX-1][playerY] == ' '){
personMove(map, playerX, playerY, playerX-1, playerY);
playerX--;
}
break;
case 's':
case 'S':
if(map[playerX+1][playerY] == ' '){
personMove(map, playerX, playerY, playerX+1, playerY);
playerX++;
}
break;
case 'a':
case 'A':
if(map[playerX][playerY-1] == ' '){
personMove(map, playerX, playerY, playerX, playerY-1);
playerY--;
}
break;
case 'd':
case 'D':
if(map[playerX][playerY+1] == ' '){
personMove(map, playerX, playerY, playerX, playerY+1);
playerY++;
}
break;
case 'q':
return 0;
default:
break;
}
print_map(map);
if (playerY == 6) {
printf("恭喜,您赢了!!!\n");
break;
}
}
return 0;
}
|