//
// main.c
// 444
//
// Created by 安广沛 on 15/8/14.
// Copyright (c) 2015年 agp. All rights reserved.
//
//##########
//#x########
//# ####
//# ### ####
//# ### #
//# ########
//# ####
//# ###
//##########
//for (int i = 0; i< 9 ; i++) {
// for (int j= 0; j<10; j++) {
// printf("%c",*(*(p+i)+j));
// }
// printf("\n");
//}
#include <stdio.h>
void daYinDiTu(char (* p)[11]){
for (int j= 0; j < 9; j++) {
printf("%s",p[j]);
printf("\n");}
}
//p=a=&a[0]
int main(int argc, const char * argv[]) {
char diTu[9][11]=
{
"##########",
"#x########",
"# ####",
"# ### ####",
"# ### #",
"# ########",
"# #### ",
"# ###",
"##########"
};
// char *p[]= {
// "##########",
// "#x########",
// "# ####",
// "# ### ####",
// "# ### #",
// "# ########",
// "# #### ",
// "# ###",
// "##########"
// };
//打印地图
daYinDiTu(diTu);
//提示用户,W,S,A,D分别控制方向上下左右
printf("W,S,A,D分别控制方向上下左右 \n");
//定义变量接收用户所输入的方向
char fangXiang;
int x1=1,x2=1,y1=1,y2=1;//x(0,8),y(0,9)
//用户输入方向
while (1) {
// 接受用户输入的方向
scanf("%c",&fangXiang);
getchar();// 接收回车
switch (fangXiang) {
case 'w':
case 'W':
x2 = x1-1;
if (diTu[x1-1][y2]==' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
x1 = x2;
}else
{
x2 = x1;
}
daYinDiTu(diTu);
break;
case 's':
case 'S':
x2 = x1+1;
if (diTu[x1+1][y2] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
x1 = x2;
}else
{
x2 = x1;
}
daYinDiTu(diTu);
break;
case 'A':
case 'a':
y2 = y1-1;
if (diTu[x2][y1-1] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
y1 = y2;
}else{
y2 = y1;
}
daYinDiTu(diTu);
break;
case 'd':
case 'D':
y2 = y1 + 1;
if (diTu[x2][y1+1] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
y1 = y2;
}else{
y2 = y1;
}
daYinDiTu(diTu);
break;
// Y = 9时
default:
printf("输入无效方向");
break;
}
if (y1 == 9) {
printf("恭喜你走出迷宫\n");
return 0;
}
}
//定义变量记录X当前的位置
//当y2等于10时,提示用户走出迷宫
//当遇到墙时,continue跳出当前循环
return 0;
}
// main.c
// 444
//
// Created by 安广沛 on 15/8/14.
// Copyright (c) 2015年 agp. All rights reserved.
//
//##########
//#x########
//# ####
//# ### ####
//# ### #
//# ########
//# ####
//# ###
//##########
//for (int i = 0; i< 9 ; i++) {
// for (int j= 0; j<10; j++) {
// printf("%c",*(*(p+i)+j));
// }
// printf("\n");
//}
#include <stdio.h>
void daYinDiTu(char (* p)[11]){
for (int j= 0; j < 9; j++) {
printf("%s",p[j]);
printf("\n");}
}
//p=a=&a[0]
int main(int argc, const char * argv[]) {
char diTu[9][11]=
{
"##########",
"#x########",
"# ####",
"# ### ####",
"# ### #",
"# ########",
"# #### ",
"# ###",
"##########"
};
// char *p[]= {
// "##########",
// "#x########",
// "# ####",
// "# ### ####",
// "# ### #",
// "# ########",
// "# #### ",
// "# ###",
// "##########"
// };
//打印地图
daYinDiTu(diTu);
//提示用户,W,S,A,D分别控制方向上下左右
printf("W,S,A,D分别控制方向上下左右 \n");
//定义变量接收用户所输入的方向
char fangXiang;
int x1=1,x2=1,y1=1,y2=1;//x(0,8),y(0,9)
//用户输入方向
while (1) {
// 接受用户输入的方向
scanf("%c",&fangXiang);
getchar();// 接收回车
switch (fangXiang) {
case 'w':
case 'W':
x2 = x1-1;
if (diTu[x1-1][y2]==' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
x1 = x2;
}else
{
x2 = x1;
}
daYinDiTu(diTu);
break;
case 's':
case 'S':
x2 = x1+1;
if (diTu[x1+1][y2] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
x1 = x2;
}else
{
x2 = x1;
}
daYinDiTu(diTu);
break;
case 'A':
case 'a':
y2 = y1-1;
if (diTu[x2][y1-1] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
y1 = y2;
}else{
y2 = y1;
}
daYinDiTu(diTu);
break;
case 'd':
case 'D':
y2 = y1 + 1;
if (diTu[x2][y1+1] == ' ') {
int f;
f = diTu[x1][y1];
diTu[x1][y1] = diTu[x2][y2];
diTu[x2][y2] = f;
y1 = y2;
}else{
y2 = y1;
}
daYinDiTu(diTu);
break;
// Y = 9时
default:
printf("输入无效方向");
break;
}
if (y1 == 9) {
printf("恭喜你走出迷宫\n");
return 0;
}
}
//定义变量记录X当前的位置
//当y2等于10时,提示用户走出迷宫
//当遇到墙时,continue跳出当前循环
return 0;
}