#include<stdio.h>
#define ROW 6
#define COL 6
int main()
{
char ditu[ROW][COL]={{'*','*','*','*','*','*'},
{'*',' ','*','*','*','*'},
{'*',' ',' ','*',' ',' '},
{'*','*',' ', ',' ','*'},
{'*','*','*','*','*','*'},
{'*','*','*','*','*','*'}};//定义地图
//打印一次地图
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
printf("%c",ditu[i][j]);
}
printf("\n");
}
//提示用户游戏玩法
printf("请输入w(上)s(下)a(左)d(右)来控制小人的移动");
char dircter;
//定义当前小人位置
int currentX=1;
int currentY=1;
//判断玩家输入的位置
while(1)
{
scanf("%c", &dircter);
switch( dircter)
{
case 'W':
case 'w':if(ditu[currentX-1][currentY]==' ')
{
char temp;
temp=ditu[currentX][currentY];
ditu[currentX][currentY]=ditu[currentX-1][currentY];
ditu[currentX-1][currentY]=temp;
currentX--;
}
break;
case 's':
case 'S':if(ditu[currentX+1][currentY]==' ')
{
char temp;
temp=ditu[currentX][currentY];
ditu[currentX][currentY]=ditu[currentX-1][currentY];
ditu[currentX-1][currentY]=temp;
currentX++;
}
break;
case 'A':
case 'a':if(ditu[currentX-1][currentY-1]==' ')
{
char temp;
temp=ditu[currentX][currentY];
ditu[currentX][currentY]=ditu[currentX-1][currentY];
ditu[currentX-1][currentY]=temp;
currentY--;
}
break;
case 's':
case 'S':if(ditu[currentX-1][currentY+1]==' ')
{
char temp;
temp=ditu[currentX][currentY];
ditu[currentX][currentY]=ditu[currentX-1][currentY];
ditu[currentX-1][currentY]=temp;
currentY++;
}
break;
case 'q':
case 'Q':
return 0;
break;
default:
break;
}
//重绘地图
for(int i=0;i<ROW;i++)
{
for(int j=0;j<COL;j++)
{
printf("%c",ditu[i][j]);
}
printf("\n");
}
//判断是否走出来
if(currentY==5)
{
printf("恭喜你,成功了!\n");
break;
}
}
return 0;
}
|
|