码迷,mamicode.com
首页 > 编程语言 > 详细

c语言迷宫游戏的实现

时间:2015-09-14 07:11:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

//
//  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;
}

  

c语言迷宫游戏的实现

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!