黑马程序员技术交流社区

标题: 迷宫小游戏~~~~~哈哈 [打印本页]

作者: ydy2511000    时间: 2016-1-9 12:54
标题: 迷宫小游戏~~~~~哈哈
  1. //
  2. //  main.c
  3. //  迷宫小游戏
  4. /*
  5. ###########
  6. #0###          #
  7. #  ###  ###  #
  8. #  ###  ###  #
  9. #  ###      #  #
  10. #      ###  #  #
  11. ###          #  #
  12. #########  #
  13. */

  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #define cows 8
  17. #define cols 11
  18. void showMap(char map[][cols]);
  19. char enterDirection();
  20. int moveUp();
  21. int moveDown();
  22. int moveLeft();
  23. int moveRight();
  24. char map [cows][cols]=
  25. {
  26.     {'#','#','#','#','#','#','#','#','#','#','#',},
  27.     {'#','0','#','#','#',' ',' ',' ',' ',' ','#',},
  28.     {'#',' ','#','#','#',' ','#','#','#',' ','#',},
  29.     {'#',' ','#','#','#',' ','#','#','#',' ','#',},
  30.     {'#',' ','#','#','#',' ',' ',' ','#',' ','#',},
  31.     {'#',' ',' ',' ','#','#','#',' ','#',' ','#',},
  32.     {'#','#','#',' ',' ',' ',' ',' ','#',' ','#',},
  33.     {'#','#','#','#','#','#','#','#','#',' ','#',}
  34. };

  35. int main(int argc, const char * argv[])
  36. {
  37.     //显示地图
  38.     //让用户输入控制方向
  39.     //判断移动,并不断重复,判断是否走出,直到走出为止
  40.     while(1)
  41.     {
  42.         system("clear");              //先用system-clear函数清除终端的显示
  43.         showMap(map);            //显示地图
  44.          char dir = enterDirection();         //取得用户的移动指令,用char型数据接收
  45.         switch(dir)                     //根据移动的指令,将“小人”进行移动
  46.         {
  47.             case'w':
  48.                 moveUp();
  49.                 break;
  50.             case's':
  51.                 moveDown();
  52.                 break;
  53.             case'a':
  54.                 moveLeft();
  55.                 break;
  56.             case'd':
  57.                 moveRight();
  58.                 break;
  59.         }
  60.         if(map[7][9] == '0')
  61.         {
  62.             break;
  63.         }
  64.     }
  65.     system("clear");
  66.     showMap(map);
  67.     printf("牛逼牛逼!\n");
  68.    
  69.     return 0;
  70. }

  71. //显示地图

  72. void showMap(char map[][cols])
  73. {
  74.     for(int i = 0; i < cows; i++)
  75.     {
  76.         for(int j = 0; j < cols; j++)
  77.         {
  78.             printf("%2c\t",map[i][j]);   
  79.         }
  80.         printf("\n");
  81.     }
  82. }
  83. //接收用户的移动指令
  84. char enterDirection()
  85. {
  86.     char dir = 'o';
  87.     printf("输入移动方向(w 上,s 下,a 左,d 右):\n");
  88.     rewind(stdin);
  89.     scanf("%c",&dir);
  90.     return dir;
  91. }
  92. //上移
  93. int moveUp()
  94. {
  95.     for(int i = 0; i < cows; i++)
  96.     {
  97.         for(int j = 0; j < cols; j++)
  98.         {
  99.             if(map[i][j] == '0' && map[i-1][j] != '#')
  100.             {
  101.                 int temp = map[i][j];
  102.                 map[i][j] = map[i - 1][j];
  103.                 map[i - 1][j] = temp;
  104.                 return 0;
  105.             }
  106.         }
  107.     }
  108.     return 0;
  109. }

  110. //下移
  111. int moveDown()
  112. {
  113.     for(int i = 0; i < cows; i++)
  114.     {
  115.         for(int j = 0; j < cols; j++)
  116.         {
  117.             if(map[i][j] == '0' && map[i+1][j] != '#')
  118.             {
  119.                 int temp = map[i][j];
  120.                 map[i][j] = map[i + 1][j];
  121.                 map[i + 1][j] = temp;
  122.                 return 0;
  123.             }
  124.         }
  125.     }
  126.     return 0;
  127. }

  128. //左移
  129. int moveLeft()
  130. {
  131.     for(int i = 0; i < cows; i++)
  132.     {
  133.         for(int j = 0; j < cols; j++)
  134.         {
  135.             if(map[i][j] == '0' && map[i][j-1] != '#')
  136.             {
  137.                 int temp = map[i][j];
  138.                 map[i][j] = map[i][j - 1];
  139.                 map[i][j - 1] = temp;
  140.                 return 0;
  141.             }
  142.         }
  143.     }
  144.     return 0;
  145. }

  146. //右移
  147. int moveRight()
  148. {
  149.     for(int i = 0; i < cows; i++)
  150.     {
  151.         for(int j = 0; j < cols; j++)
  152.         {
  153.             if(map[i][j] == '0' && map[i][j + 1] != '#')
  154.             {
  155.                 int temp = map[i][j];
  156.                 map[i][j] = map[i][j + 1];
  157.                 map[i][j + 1] = temp;
  158.                 return 0;
  159.             }
  160.         }
  161.     }
  162.     return 0;
  163. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2