A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© stud 中级黑马   /  2016-5-8 08:44  /  987 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

请问走出迷宫怎么实现?

3 个回复

倒序浏览
看起来好高深的样子!!
回复 使用道具 举报
其实,就是分几个模块:
1.打印地图.
2.接收输入方向.
3.用switch去判断接收的字符,然后执行每个方向的函数.
ps:步骤3的函数是重点,分析每走一步的后的字符数组的对应值得交换.
附代码(如有不对的地方,请指出):

#include <stdio.h>
#include <stdlib.h>
#define ROWS 15
#define COLS 15

char arr[ROWS][COLS] = {
    {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
    {'#','O',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ',' ','#'},
    {'#','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#','#'},
    {'#','#',' ','#',' ',' ',' ','#',' ',' ',' ','#',' ','#','#'},
    {'#',' ',' ','#','#','#','#','#','#','#',' ','#',' ','#','#'},
    {'#',' ','#','#','#','#','#',' ',' ',' ',' ','#',' ',' ','#'},
    {'#',' ','#',' ',' ',' ','#',' ','#','#','#','#',' ','#','#'},
    {'#',' ','#',' ','#',' ','#',' ','#','#',' ',' ',' ','#','#'},
    {'#',' ',' ',' ','#',' ','#',' ','#','#',' ','#',' ','#','#'},
    {'#',' ','#','#','#',' ',' ',' ','#','#',' ','#',' ','#','#'},
    {'#',' ','#','#','#',' ','#','#','#',' ',' ','#',' ',' ','#'},
    {'#',' ','#','#','#',' ','#',' ','#',' ','#','#','#',' ','#'},
    {'#',' ',' ',' ','#',' ','#','#','#',' ',' ',' ','#',' ',' '},
    {'#','#','#',' ','#',' ',' ',' ','#','#','#',' ','#','#','#'},
    {'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},

};

int peopleCurrentRows = 1;
int peopleCurrentCols = 1;
/**
*  显示地图
*/
void showMap();

/**
*  提示用户输入方向,并接收.
*
*  @return 返回用户输入的方向.
*/
char getDirection();

/**
*  向上移动
*/
void moveToUp();

/**
*  向下移动
*/
void moveToDown();

/**
*  向左移动
*/
void moveToLeft();

/**
*  向右移动
*/
void moveToRight();

int main(int argc, const char * argv[])
{
    while(1)
    {
        system("clear");
        showMap();
        char direction = getDirection();
        switch (direction)
        {
            case 'w':
            case 'W':
                moveToUp();
                break;
            case 's':
            case 'S':
                moveToDown();
                break;
            case 'a':
            case 'A':
                moveToLeft();
                break;
            case 'd':
            case 'D':
                moveToRight();
                break;
            case 'q':
            case 'Q':
                printf("游戏结束!\n");
                return 0;
            default:
                printf("输入错误,请重新输入!\n");
                break;
        }
    }
    return 0;
}

/**
*  显示地图
*/
void showMap()
{
    for (int i = 0; i < 15; i ++)
    {
        for (int j = 0; j < 15; j ++)
        {
            printf("%c ",arr[i][j]);
        }
        printf("\n");
    }
}

/**
*  提示用户输入方向,并接收.
*
*  @return 返回用户输入的方向.
*/
char getDirection()
{
    printf("请输入前进的方向\nW---上\nS---下\nA---左\nD---右\nQ---结束游戏\n");
    char direction;
    rewind(stdin);
    scanf("%c",&direction);
    return direction;
}

/**
*  向上移动
*/
void moveToUp()
{
    int peopleNextRows = peopleCurrentRows - 1;
    int peopleNextCols = peopleCurrentCols;
    if (arr[peopleNextRows][peopleNextCols] == ' ')
    {
        arr[peopleNextRows][peopleNextCols] = 'O';
        arr[peopleCurrentRows][peopleCurrentCols] = ' ';
        peopleCurrentRows = peopleNextRows;
        peopleCurrentCols = peopleNextCols;
    }
}

/**
*  向下移动
*/
void moveToDown()
{
    int peopleNextRows = peopleCurrentRows + 1;
    int peopleNextCols = peopleCurrentCols;
    if (arr[peopleNextRows][peopleNextCols] == ' ')
    {
        arr[peopleNextRows][peopleNextCols] = 'O';
        arr[peopleCurrentRows][peopleCurrentCols] = ' ';
        peopleCurrentRows = peopleNextRows;
        peopleCurrentCols = peopleNextCols;
    }
}

/**
*  向左移动
*/
void moveToLeft()
{
    int peopleNextRows = peopleCurrentRows;
    int peopleNextCols = peopleCurrentCols - 1;
    if (arr[peopleNextRows][peopleNextCols] == ' ')
    {
        arr[peopleNextRows][peopleNextCols] = 'O';
        arr[peopleCurrentRows][peopleCurrentCols] = ' ';
        peopleCurrentRows = peopleNextRows;
        peopleCurrentCols = peopleNextCols;
    }
}

/**
*  向右移动
*/
void moveToRight()
{
    int peopleNextRows = peopleCurrentRows;
    int peopleNextCols = peopleCurrentCols + 1;
    if (arr[peopleNextRows][peopleNextCols] == ' ')
    {
        arr[peopleNextRows][peopleNextCols] = 'O';
        arr[peopleCurrentRows][peopleCurrentCols] = ' ';
        peopleCurrentRows = peopleNextRows;
        peopleCurrentCols = peopleNextCols;
    }
}

点评

6666666666^  发表于 2016-5-9 07:07
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马