走出迷宫小游戏

  1 #include <stdio.h>
  2 /*
  3  游戏说明:玩家通过键盘录入 w,s,a,d控制小人向不同方向移动,其中w代表向上移动,s代表向下移动,a代表向左移动,d 代表向右移动,当小人移动到出口位置,玩家胜利
  4  
  5  地图如下:
  6  ######
  7  #O#
  8  # ## #
  9  #  # #
 10  ##   #
 11  ######
 12  说明: # 代表 墙 O代表小人 ‘ ’ 代表路
 13 
 14  
 15  分析:
 16     1、打印地图
 17        1、保存地图
 18        2、打印
 19     2、让玩家移动小人
 20        1、提示玩家如何移动小人
 21        2、接收玩家的录入
 22        3、根据玩家录入来移动小人
 23     3、判断输赢
 24  */
 25 
 26 
 27 int main(int argc, const char * argv[]) {
 28 
 29 //  1、保存地图
 30     char map[6][6] = {
 31         {'#','#','#','#','#','#'},
 32         {'#','O','#',' ',' ',' '},
 33         {'#',' ','#','#',' ','#'},
 34         {'#',' ',' ','#',' ','#'},
 35         {'#','#',' ',' ',' ','#'},
 36         {'#','#','#','#','#','#'}
 37     };
 38     
 39 //   打印地图
 40     for (int i = 0; i < 6; i++) {
 41         
 42         for (int j = 0; j < 6; j++) {
 43             printf("%c",map[i][j]);
 44         }
 45         printf("
");
 46     }
 47     
 48     
 49 
 50     
 51 //  为了计算小人下一个位置,我们必须首先直到小人当前位置
 52 //  小人当前位置的X坐标
 53     int currentX = 1;
 54 //  小人当前位置的Y坐标
 55     int currentY = 1;
 56     
 57     //  3.1根据玩家的录入移动小人
 58     //   为了判断小人的移动,我们需要定义变量来表示路
 59     char street = ' ';
 60     
 61     while (1) {
 62         
 63         //  2.1 提示玩家移动小人
 64         printf("请移动小人,w表示向上,s表示向下,a表示向左,d表示向右
");
 65         
 66         //  2.2接收用户的录入
 67         char direction;
 68 //       %c 前面加一个空格,来吃掉后面的回车
 69         scanf(" %c",&direction);
 70       
 71         switch (direction) {
 72             case  'w':{
 73                 int nextX = currentX -1;
 74                 if (map[nextX][currentY] == street) {
 75                     //               交换位置
 76                     char temp = map[currentX][currentY];
 77                     
 78                     map[currentX][currentY] = map[nextX][currentY];
 79                     
 80                     map[nextX][currentY] = temp;
 81                     //              改变小人当前位置
 82                     currentX = nextX;
 83                 }
 84                 break;
 85             }
 86             case 's':{
 87                 //          如果小人下一个位置是路就移动小人
 88                 int nextX = currentX + 1;
 89                 if (map[nextX][currentY] == street) {
 90                     //               定义中间变量记录小人的当前位置
 91                     //               交换位置
 92                     char temp = map[currentX][currentY];
 93                     
 94                     map[currentX][currentY] = map[nextX][currentY];
 95                     
 96                     map[nextX][currentY] = temp;
 97                     
 98                     //              改变小人当前位置
 99                     currentX = nextX;
100                 }
101                 
102                 break;
103             }
104             case 'a':{
105                 //          计算小人下一个位置
106                 int nextY = currentY - 1;
107                 
108                 if (map[currentX][nextY] == street) {
109                     //                交换位置
110                     char temp = map[currentX][currentY];
111                     map[currentX][currentY] = map[currentX][nextY];
112                     map[currentX][nextY] = temp;
113                     //               改变小人当前位置
114                     currentY = nextY;
115                 }
116                 break;
117             }
118                 
119             case 'd':{
120                 //          计算小人下一个位置
121                 int nextY = currentY + 1;
122                 
123                 if (map[currentX][nextY] == street) {
124                     //                交换位置
125                     char temp = map[currentX][currentY];
126                     map[currentX][currentY] = map[currentX][nextY];
127                     map[currentX][nextY] = temp;
128                     //               改变小人当前位置
129                     currentY = nextY;
130                 }
131                 break;
132             }
133                 
134                 
135             default:
136                 break;
137         }
138         
139         
140         //   打印地图
141         for (int i = 0; i < 6; i++) {
142             
143             for (int j = 0; j < 6; j++) {
144                 printf("%c",map[i][j]);
145             }
146             printf("
");
147         }
148         
149         
150         if(currentX == 1 && currentY == 5)
151         {
152             printf("你好牛X!
");
153             break;
154         }
155     }
156     
157 
158     return 0;
159 } 
原文地址:https://www.cnblogs.com/themost/p/6358914.html