- #include <stdio.h>
- #define ROW 6
- #define COL 6
- int main(int argc, const char * argv[]) {
-
- //1.定义变量
-
- //定义变量、地图,
- int x=1,y=1;//行,列
- char map[ROW][COL]={
- {'#','#','#','#','#','#'},
- {'#','O','#','#',' ',' '},
- {'#',' ','#','#',' ','#'},
- {'#',' ','#','#',' ','#'},
- {'#',' ',' ',' ',' ','#'},
- {'#','#','#','#','#','#'}};
-
- //打印地图
- void pri_arr(char map[ROW][COL]);
-
- pri_arr(map);
- printf("输入WASD来控制方向,Q退出游戏\n");
-
- while((1!=x)||(5!=y))
- {
- char ch;
- scanf("%c",&ch);
- if(('w'==ch)&&('#'!=map[x-1][y]))
- {
-
- map[x-1][y]='O';
- map[x][y]=' ';
- x=x-1;
- pri_arr(map);
- }
- if(('s'==ch)&&('#'!=map[x+1][y]))
- {
-
- map[x+1][y]='O';
- map[x][y]=' ';
- x=x+1;
- pri_arr(map);
- }
- if(('a'==ch)&&('#'!=map[x][y-1]))
- {
-
- map[x][y-1]='O';
- map[x][y]=' ';
- y=y-1;
- pri_arr(map);
- }
- if(('d'==ch)&&('#'!=map[x][y+1]))
- {
-
- map[x][y+1]='O';
- map[x][y]=' ';
- y=y+1;
- pri_arr(map);
- }
- }
- printf("suceess\n");
- return 0;
- }
- void pri_arr(char map[ROW][COL])
- {
- for (int i=0; i<ROW; i++) {
- for (int j=0; j<COL; j++) {
- printf("%c",map[i][j]);
- }
- printf("\n");
-
-
- }
- }
复制代码 |
|