黑马程序员技术交流社区

标题: 迷宫游戏代码实现,大家走过路过不要错过哦~.~ [打印本页]

作者: wpf986530062    时间: 2015-10-25 21:54
标题: 迷宫游戏代码实现,大家走过路过不要错过哦~.~
呼呼...真累,脑袋都大了
#include<stdio.h>
int main(){
    char map[6][6]={
        {'#','#','#','#','#','#'},
        {'D',' ','#','#',' ',' '},
        {'#',' ','#','#',' ','#'},
        {'#',' ','#',' ',' ','#'},
        {'#',' ',' ',' ','#','#'},
        {'#','#','#','#','#','#'}
    };
    for (int i = 0; i < 6; i++) {        //打印地图
        for (int j = 0; j < 6; j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
    int currentX = 1;
    int currentY = 0;
    char ch;
   
    printf("请移动小人w:上\tx:下\ta:左\td:右\tq:退出\n");
    while (1) {
        if (currentY == 5) {
            printf("哇哦,你逃出去了\n");
            return 0;
        }
        scanf("%c",&ch);
        switch (ch) {
            case 'w' :
            case 'W' :
                if (map[currentX - 1][currentY] == ' ') {
                    int temp = map[currentX - 1][currentY];
                    map[currentX - 1][currentY]=map[currentX][currentY];
                    map[currentX][currentY]=temp;
                    currentX--;
                    for (int i = 0; i < 6; i++) {        //打印地图
                        for (int j = 0; j < 6; j++) {
                            printf("%c",map[i][j]);
                        }
                        printf("\n");
                    }
                }
                break;
            case 's' :
            case 'S' :
                if (map[currentX + 1][currentY] == ' ') {
                    int temp = map[currentX + 1][currentY];
                    map[currentX + 1][currentY]=map[currentX][currentY];
                    map[currentX][currentY]=temp;
                    currentX++;
                    for (int i = 0; i < 6; i++) {        //打印地图
                        for (int j = 0; j < 6; j++) {
                            printf("%c",map[i][j]);
                        }
                        printf("\n");
                    }

                }
                break;
            case 'a' :
            case 'A' :
                if (map[currentX][currentY - 1] == ' ') {
                    int temp = map[currentX][currentY - 1];
                    map[currentX ][currentY - 1]=map[currentX][currentY];
                    map[currentX][currentY]=temp;
                    currentY--;
                    for (int i = 0; i < 6; i++) {        //打印地图
                        for (int j = 0; j < 6; j++) {
                            printf("%c",map[i][j]);
                        }
                        printf("\n");
                    }

                }
                break;
            case 'd' :
            case 'D' :
                if (map[currentX][currentY + 1] == ' ') {
                    int temp = map[currentX][currentY + 1];
                    map[currentX][currentY + 1]=map[currentX][currentY];
                    map[currentX][currentY]=temp;
                    currentY++;
                    for (int i = 0; i < 6; i++) {        //打印地图
                        for (int j = 0; j < 6; j++) {
                            printf("%c",map[i][j]);
                        }
                        printf("\n");
                    }

                }
                break;
            case 'q' :
            case 'Q' :
                return 0;
        }
    }
}
作者: 青菜CC    时间: 2015-10-25 22:15
66666666666
作者: 陈昭燊    时间: 2015-10-25 22:22
哥们,厉害厉害
作者: MLP1355053753    时间: 2015-10-25 22:24
还可以优化一下下,嘿嘿,赞一个,好棒的说
作者: 陌上开花    时间: 2015-10-25 22:33
厉害  66666666
作者: yuweiyuwei    时间: 2015-10-25 22:46
斯过一!




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