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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

[学习交流] 走出迷宫代码。

© a_skting 中级黑马   /  2016-4-13 01:02  /  1553 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

学完了老师讲的走出迷宫,自己写了一遍,感觉萌萌哒。。。

#include <stdio.h>
#include <stdlib.h>
//#include <limits.h>
//#include <string.h>

//宏定义地图的行和列
#define ROWS 7
#define COLS 8
/**
*  走出迷宫
   
1  打印地图
2  玩家输入方向
3  小人移动方向


*/
//地图定义为全局变量。
char gameMap[ROWS][COLS] =
{
    {'#','#','#','#','#','#','#','#'},
    {'#','0','#','#',' ',' ',' ','#'},
    {'#',' ','#','#',' ','#',' ','#'},
    {'#',' ','#','#',' ','#',' ','#'},
    {'#',' ',' ','#',' ','#',' ','#'},
    {'#','#',' ',' ',' ','#',' ','#'},
    {'#','#','#','#','#','#',' ','#'}
};

//定义小人的坐标为全局变量。
int row = 1;
int col = 1;


//打印地图函数
void mapPrint()
{
    for(int i = 0 ; i < ROWS ; i++)
    {
        for(int j = 0 ; j < COLS ; j++)
        {
            printf("%c ",gameMap[i][j]);
        }
        printf("\n");
    }
    printf("请使用 w 、 s 、 a 、d 移动小人。q 退出游戏。\n");
}

//接收用户输入的方向
char getDirection()
{
    char direct = 'a';
    scanf("%c",&direct);
    return direct;
}

//向上的移动指令
void directUp()
{
    int nextRow = row - 1;
    int nextCol = col;
    if(gameMap[nextRow][nextCol] != '#')
    {
        gameMap[row][col] = ' ';
        gameMap[nextRow][nextCol] = '0';
        row = nextRow;
        col = nextCol;
    }
}
//向下的移动指令
void directDown()
{
    int nextRow = row + 1;
    int nextCol = col;
    if(gameMap[nextRow][nextCol] != '#')
    {
        gameMap[row][col] = ' ';
        gameMap[nextRow][nextCol] = '0';
        row = nextRow;
        col = nextCol;
    }
}
//向左的移动指令
void directLeft()
{
    int nextRow = row;
    int nextCol = col - 1;
    if(gameMap[nextRow][nextCol] != '#')
    {
        gameMap[row][col] = ' ';
        gameMap[nextRow][nextCol] = '0';
        row = nextRow;
        col = nextCol;
    }
}
//向右的移动指令
void directRight()
{
    int nextRow = row;
    int nextCol = col + 1;
    if(gameMap[nextRow][nextCol] != '#')
    {
        gameMap[row][col] = ' ';
        gameMap[nextRow][nextCol] = '0';
        row = nextRow;
        col = nextCol;
    }
}



int main()
{
    //打印地图
    mapPrint();
      
    while(1)
    {

        //接收用户输入的方向
        rewind(stdin);
        char direct = getDirection();
        
        //小人移动
        switch(direct)
        {
            case 'w':
            case 'W':
                directUp();
                break;
            case 's':
            case 'S':
                directDown();
                break;
            case 'a':
            case 'A':
                directLeft();
                break;
            case 'd':
            case 'D':
                directRight();
                break;
            case 'q':
            case 'Q':
                printf("game over !\n");
                return 0;
        
        }
        system("clear");
        mapPrint();
   
        if(row == 6 && col == 6)
        {
            printf("恭喜您过关。\n");
            printf("game over!\n");
            return 0;
        }
    }
}

2 个回复

倒序浏览
这个模型可以用OC来实现啊,挺好的一个模型

试着去用OC写一下看看
回复 使用道具 举报
等学完OC后,我去试试楼上说的。           
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马