其实,就是分几个模块:
1.打印地图.
2.接收输入方向.
3.用switch去判断接收的字符,然后执行每个方向的函数.
ps:步骤3的函数是重点,分析每走一步的后的字符数组的对应值得交换.
附代码(如有不对的地方,请指出):
#include <stdio.h>
#include <stdlib.h>
#define ROWS 15
#define COLS 15
char arr[ROWS][COLS] = {
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','O',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#'},
{'#','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#','#'},
{'#','#',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ','#','#'},
{'#',' ',' ','#','#','#','#','#','#','#',' ','#',' ','#','#'},
{'#',' ','#','#','#','#','#',' ',' ',' ',' ','#',' ',' ','#'},
{'#',' ','#',' ',' ',' ','#',' ','#','#','#','#',' ','#','#'},
{'#',' ','#',' ','#',' ','#',' ','#','#',' ',' ',' ','#','#'},
{'#',' ',' ',' ','#',' ','#',' ','#','#',' ','#',' ','#','#'},
{'#',' ','#','#','#',' ',' ',' ','#','#',' ','#',' ','#','#'},
{'#',' ','#','#','#',' ','#','#','#',' ',' ','#',' ',' ','#'},
{'#',' ','#','#','#',' ','#',' ','#',' ','#','#','#',' ','#'},
{'#',' ',' ',' ','#',' ','#','#','#',' ',' ',' ','#',' ',' '},
{'#','#','#',' ','#',' ',' ',' ','#','#','#',' ','#','#','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
};
int peopleCurrentRows = 1;
int peopleCurrentCols = 1;
/**
* 显示地图
*/
void showMap();
/**
* 提示用户输入方向,并接收.
*
* @return 返回用户输入的方向.
*/
char getDirection();
/**
* 向上移动
*/
void moveToUp();
/**
* 向下移动
*/
void moveToDown();
/**
* 向左移动
*/
void moveToLeft();
/**
* 向右移动
*/
void moveToRight();
int main(int argc, const char * argv[])
{
while(1)
{
system("clear");
showMap();
char direction = getDirection();
switch (direction)
{
case 'w':
case 'W':
moveToUp();
break;
case 's':
case 'S':
moveToDown();
break;
case 'a':
case 'A':
moveToLeft();
break;
case 'd':
case 'D':
moveToRight();
break;
case 'q':
case 'Q':
printf("游戏结束!\n");
return 0;
default:
printf("输入错误,请重新输入!\n");
break;
}
}
return 0;
}
/**
* 显示地图
*/
void showMap()
{
for (int i = 0; i < 15; i ++)
{
for (int j = 0; j < 15; j ++)
{
printf("%c ",arr[i][j]);
}
printf("\n");
}
}
/**
* 提示用户输入方向,并接收.
*
* @return 返回用户输入的方向.
*/
char getDirection()
{
printf("请输入前进的方向\nW---上\nS---下\nA---左\nD---右\nQ---结束游戏\n");
char direction;
rewind(stdin);
scanf("%c",&direction);
return direction;
}
/**
* 向上移动
*/
void moveToUp()
{
int peopleNextRows = peopleCurrentRows - 1;
int peopleNextCols = peopleCurrentCols;
if (arr[peopleNextRows][peopleNextCols] == ' ')
{
arr[peopleNextRows][peopleNextCols] = 'O';
arr[peopleCurrentRows][peopleCurrentCols] = ' ';
peopleCurrentRows = peopleNextRows;
peopleCurrentCols = peopleNextCols;
}
}
/**
* 向下移动
*/
void moveToDown()
{
int peopleNextRows = peopleCurrentRows + 1;
int peopleNextCols = peopleCurrentCols;
if (arr[peopleNextRows][peopleNextCols] == ' ')
{
arr[peopleNextRows][peopleNextCols] = 'O';
arr[peopleCurrentRows][peopleCurrentCols] = ' ';
peopleCurrentRows = peopleNextRows;
peopleCurrentCols = peopleNextCols;
}
}
/**
* 向左移动
*/
void moveToLeft()
{
int peopleNextRows = peopleCurrentRows;
int peopleNextCols = peopleCurrentCols - 1;
if (arr[peopleNextRows][peopleNextCols] == ' ')
{
arr[peopleNextRows][peopleNextCols] = 'O';
arr[peopleCurrentRows][peopleCurrentCols] = ' ';
peopleCurrentRows = peopleNextRows;
peopleCurrentCols = peopleNextCols;
}
}
/**
* 向右移动
*/
void moveToRight()
{
int peopleNextRows = peopleCurrentRows;
int peopleNextCols = peopleCurrentCols + 1;
if (arr[peopleNextRows][peopleNextCols] == ' ')
{
arr[peopleNextRows][peopleNextCols] = 'O';
arr[peopleCurrentRows][peopleCurrentCols] = ' ';
peopleCurrentRows = peopleNextRows;
peopleCurrentCols = peopleNextCols;
}
}
|