黑马程序员技术交流社区
标题:
迷宫分享
[打印本页]
作者:
XiaoBaoMi
时间:
2015-6-26 21:40
标题:
迷宫分享
#include <stdio.h>
#define X 5
#define Y 3
void printMap(char map[X][Y]);
char *randomMap();
int main() {
char map[X][Y] = {
{'#', '#', '#'},
{'O', ' ', '#'},
{'#', ' ', ' '},
{'#', '#', '#'},
{'#', '#', '#'}
};
printf("这是一个小迷宫\n");
printMap(map);
int currentX = 1, currentY = 0;
char control = ' ';
while (currentY != 2) {
printf("请输入您要走的方向\n");
scanf("%c",&control);
getchar();
switch (control) {
case 'w':
case 'W':
{
char middle = ' ';
if (map[currentX - 1][currentY]!= '#') {
middle = map[currentX][currentY];
map[currentX][currentY] = map[currentX - 1][currentY];
map[currentX][currentY] = middle;
printMap(map);
currentX--;
break;
}else{
printf("您撞墙了,不要闹\n");
}
}
case 'a':
case 'A':
{
char middle = ' ';
if (map[currentX][currentY - 1]!= '#') {
middle = map[currentX][currentY];
map[currentX][currentY] = map[currentX][currentY - 1];
map[currentX][currentY] = middle;
printMap(map);
currentY--;
break;
}else{
printf("您撞墙了,不要闹\n");
}
}
break;
case 's':
case 'S':
{
char middle = ' ';
if (map[currentX + 1][currentY]!= '#') {
middle = map[currentX][currentY];
map[currentX][currentY] = map[currentX + 1][currentY];
map[currentX + 1][currentY] = middle;
printMap(map);
currentX++;
break;
}else{
printf("您撞墙了,不要闹\n");
}
}
break;
case 'd':
case 'D':
{
char middle = ' ';
if (map[currentX][currentY + 1]!= '#') {
middle = map[currentX][currentY];
map[currentX][currentY] = map[currentX][currentY + 1];
map[currentX][currentY + 1] = middle;
printMap(map);
currentY++;
break;
}else{
printf("您撞墙了,不要闹\n");
}
}
default:
break;
}
}
printf("逃出生天!\n");
return 0;
}
void printMap(char map[X][Y])
{
for (int i = 0; i < X; i++) {
for (int j = 0; j < Y; j++) {
printf("%c ",map[i][j]);
}
printf("\n");
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2