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("
");
    }
}
int main(int argc, const char * argv[]) {
    char map[ROW][COL] = {{'#', '#', '#', '#', '#', '#'},
                          {'#', '@', '#', '#', ' ', ' '},
                          {'#', ' ', '#', '#', ' ', '#'},
                          {'#', ' ', ' ', '#', ' ', '#'},
                          {'#', '#', ' ', ' ', ' ', '#'},
                          {'#', '#', '#', '#', '#', '#'}};
    
    print_arr(map);
    printf("迷宫游戏:w.上, s.下, a.左, d.右 q.退出
");
    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("退出游戏
");
                    return 0;
                    break;
        }
        print_arr(map);
        if (currentY == 5) {
            printf("恭喜你,走出迷宫!");
            break;
        }
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/ailen226/p/4806106.html