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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刨丁 中级黑马   /  2015-7-24 21:42  /  625 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

#include <stdio.h>
#define COL 6
#define ROW 6

int main(int argc, const char * argv[]) {
//定义地图变量 坐标变量 方向变量 路的变量
//    定义地图变量
char map[ROW][COL]={
    {'#','#','#','#','#','#'},
    {'#','O','#','#',' ',' '},
    {'#',' ','#','#',' ','#'},
    {'#',' ',' ','#',' ','#'},
    {'#','#',' ',' ',' ','#'},
    {'#','#','#','#','#','#'}
};

//    定义小人坐标变量
    int currentx = 1;
    int currenty = 1;
//    定义方向输入变量
    char derection;
//    定义路的变量
    char street = ' ';
    for (int i = 0; i < ROW;i++) {
        
        for (int j = 0; j < COL; j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
//    提示玩家输入方向
    printf("请输入方向: W 上  S 下  A 左  D 右\n");
//    判断是否是路,  是路则和路的进行交换
    while (1) {
        char ch;
        scanf("%c",&derection);
        scanf("%c",&ch);
      
        switch (derection) {
            case 'w':
            case 'W':
                if (map[currentx-1][currenty] == street) {
                 char temp;
                temp = map[currentx][currenty];
                map[currentx][currenty] = map[currentx-1][currenty];
                map[currentx-1][currenty] = temp;
                    currentx--;}
                break;
            case 's':
            case 'S':
                if (map[currentx+1][currenty] == street) {
                char temp;
                temp = map[currentx][currenty];
                map[currentx][currenty] = map[currentx+1][currenty];
                map[currentx+1][currenty] = temp;
                    currentx++;}
                break;
            case 'a':
            case 'A':
                if (map[currentx][currenty-1] == street) {
               char temp;
                temp = map[currentx][currenty];
                map[currentx][currenty] = map[currentx][currenty-1];
                map[currentx][currenty-1] = temp;
                    currenty--;}
                break;
            case 'd':
            case 'D':
                if (map[currentx][currenty+1]) {
                 char temp;
                temp = map[currentx][currenty];
                map[currentx][currenty] = map[currentx][currenty+1];
                map[currentx][currenty+1] = temp;
                    currenty++;}
                break;
            case 'q':
            case 'Q':
            return 0;
               
            default:
                break;
               
               
        }
        
        
   
//    不是路什么也不操作
//    循环打出地图显示当前小人位置
    for (int i=0; i<ROW; i++) {
        for (int j=0; j<COL; j++) {
            printf("%c",map[i][j]);
        }
        printf("\n");
    }
//            for(int i=0; i < ROW; i++){
//               
//                for (int j = 0; j < COL; j++) {
//                    printf("%c",map[i][j]);
//                }
//                printf("\n");
//            }
    if (currenty == 5) {
        printf("哈哈!!恭喜你 通关了!!");
        break;
    }
    }
//    判断是否通关,
    return 0;
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马