A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liuhao_hm 中级黑马   /  2015-9-16 21:29  /  710 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 liuhao_hm 于 2015-9-16 21:47 编辑

  1. // 虽然是个很简单的小游戏,但是自己做出还是挺不错的
  2. #include <stdio.h>
  3. void printMap(char map[11][12], int row, int col);

  4. int main()
  5. {
  6.     // 定义地图数组
  7.     char map[11][12] = {
  8.         {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
  9.         {'#', ' ', ' ', ' ', '#', '#', '#', ' ', '#', ' ', ' ', '#'},
  10.         {'#', 'R', '#', ' ', '#', '#', '#', '#', ' ', ' ', '#', '#'},
  11.         {'#', ' ', '#', '#', ' ', ' ', '#', '#', ' ', '#', '#', '#'},
  12.         {'#', ' ', '#', '#', '#', '#', '#', '#', ' ', ' ', ' ', '#'},
  13.         {'#', ' ', ' ', '#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'},
  14.         {'#', '#', ' ', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#'},
  15.         {'#', '#', ' ', '#', ' ', '#', ' ', '#', '#', '#', ' ', '#'},
  16.         {'#', '#', ' ', ' ', ' ', '#', ' ', '#', '#', '#', ' ', ' '},
  17.         {'#', '#', ' ', '#', '#', '#', ' ', ' ', '#', '#', '#', '#'},
  18.         {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
  19.     };
  20.    
  21.     int row = sizeof(map) / sizeof(map[0]);
  22.     int col = sizeof(map) / row;
  23.    
  24.     // 打印地图
  25.     printMap(map, row, col);
  26.    
  27.     // 定义人所在的位置
  28.     int rRow = 2;
  29.     int rCol = 1;
  30.    
  31.     // 定义终出口位置
  32.     int endRow = 8;
  33.     int endCol = 11;
  34.    
  35.     // 定义计数器
  36.     int num = 0;
  37.    
  38.     // 循环判断玩家是否走出地图
  39.     while ('R' != map[endRow][endCol]) {
  40.         // 提示用户操作
  41.         printf("亲,请输入相应的操作\n");
  42.         printf("w(向上移动) s(向下移动) a(向左移动) d(向右移动) \n");
  43.         printf("以操作次数%i\n", num++);
  44.         
  45.         // 接收玩家输入的操作
  46.         char run = getchar();
  47.         getchar();
  48.         
  49.         // 根据玩家输入选择对应操作
  50.         switch (run) {
  51.             case 'w':
  52.                 if ('#' != map[rRow-1][rCol]) {
  53.                     map[rRow][rCol] = ' ';
  54.                     rRow--;
  55.                     map[rRow][rCol] = 'R';
  56.                 }
  57.                 break;
  58.             case 's':
  59.                 if ('#' != map[rRow+1][rCol]) {
  60.                     map[rRow][rCol] = ' ';
  61.                     rRow++;
  62.                     map[rRow][rCol] = 'R';
  63.                 }
  64.                 break;

  65.             case 'a':
  66.                 if ('#' != map[rRow][rCol-1]) {
  67.                     map[rRow][rCol] = ' ';
  68.                     rCol--;
  69.                     map[rRow][rCol] = 'R';
  70.                 }
  71.                 break;

  72.             case 'd':
  73.                 if ('#' != map[rRow][rCol+1]) {
  74.                     map[rRow][rCol] = ' ';
  75.                     rCol++;
  76.                     map[rRow][rCol] = 'R';
  77.                 }
  78.                 break;

  79.         }
  80.         // 打印地图
  81.         printMap(map, row, col);
  82.     }
  83.     printf("你太牛逼了\n");
  84.     printf("请购买正版,解锁跟多模式哦\n");
  85.     return 0;
  86. }
  87. void printMap(char map[11][12], int row, int col)
  88. {
  89.     for (int x = 0; x < row; x++) {
  90.         for (int y = 0; y < col; y++) {
  91.             printf("%c",map[x][y]);
  92.         }
  93.         printf("\n");
  94.     }
  95. }
复制代码


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马