• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一生很短,请勿将就
博客园    首页    新随笔    联系   管理    订阅  订阅
c语言迷宫游戏的实现
//
//  main.c
//  迷宫游戏代码实现
//

#include <stdio.h>
#define ROW 6   //宏定义行
#define COL 6   //宏定义列

/**
 *  打印地图
 *
 *  @param arr 地图数组
 */
void print_arr (char arr[ROW][COL]) {
    for (int i = 0; i < ROW; i ++) {
        for (int j = 0; j < COL; j ++) {
            printf("%c", arr[i][j]);
        }
        printf("\n");
    }
}
int main(int argc, const char * argv[]) {
    char map[ROW][COL] = {{'#', '#', '#', '#', '#', '#'},
                          {'#', '@', '#', '#', ' ', ' '},
                          {'#', ' ', '#', '#', ' ', '#'},
                          {'#', ' ', ' ', '#', ' ', '#'},
                          {'#', '#', ' ', ' ', ' ', '#'},
                          {'#', '#', '#', '#', '#', '#'}};
    
    print_arr(map);
    printf("迷宫游戏:w.上, s.下, a.左, d.右 q.退出\n");
    char direction, ch, street = ' ', people = '@';
    int currentX = 1, currentY = 1;
    while (1) {
        scanf("%c", &direction);
        scanf("%c", &ch);
        switch (direction) {
            case 'w':
            case 'W':
                if (map[currentX-1][currentY] == street) {
                    map[currentX-1][currentY] = people;
                    map[currentX][currentY] = ' ';
                    currentX--;
                }
                break;
            case 's':
            case 'S':
                if (map[currentX+1][currentY] == street) {
                    map[currentX+1][currentY] = people;
                    map[currentX][currentY] = ' ';
                    currentX++;
                }
                break;
            case 'a':
            case 'A':
                if (map[currentX][currentY-1] == street) {
                    map[currentX][currentY-1] = people;
                    map[currentX][currentY] = ' ';
                    currentY--;
                }
                break;
            case 'd':
            case 'D':
                if (map[currentX][currentY+1] == street) {
                    map[currentX][currentY+1] = people;
                    map[currentX][currentY] = ' ';
                    currentY++;
                }
                break;
            case 'q':
            case 'Q':
                    printf("退出游戏\n");
                    return 0;
                    break;
        }
        print_arr(map);
        if (currentY == 5) {
            printf("恭喜你,走出迷宫!");
            break;
        }
    }
    return 0;
}

  

posted on 2015-09-14 06:30  一生很短,请勿将就  阅读(1311)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3