c 走迷宫

走迷宫
    1、定义二位字符数组作为迷宫
    2、定义变记录老鼠的位置
    3、获取游戏开始时间
    4、进入循环
        1、清理屏幕,使用system调用系统命令
        2、显示迷宫(遍历二位字符数组)
        3、检查是否到达出口
            获取游戏结束时间,计算走出迷宫用了多少秒
        4、获取方向键并处理
            判断接下来要走的位置是否是路
            1、把走过的位置赋值为空格
            2、把新位置赋值为人
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getch.h>

int main()
{
    char arr[10][10]={                                               //自己设定迷宫路径,' '表示路径,'1'表示墙,'s'表示人
        's',' ','1','1','1','1','1','1','1','1',
        '1',' ',' ','1','1','1','1','1','1','1',
        '1','1',' ',' ','1','1','1','1','1','1',
        '1','1','1',' ',' ','1','1','1','1','1',
        '1','1','1','1',' ',' ','1','1','1','1',
        '1','1','1','1','1',' ',' ','1','1','1',
        '1','1','1','1','1','1',' ',' ','1','1',
        '1','1','1','1','1','1','1',' ',' ','1',
        '1','1','1','1','1','1','1','1',' ','1',
        '1','1','1','1','1','1','1','1',' ',' '};
    int lsx,lsy;
    time_t start_time=time(NULL);                                      
    while(10!=lsy)
    {
        system("clear");
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                printf("%c",arr[i][j]);
                if(arr[i][j]=='s')
                {
                    lsx=i;
                    lsy=j;
                }        
            }
            printf("\n");
        }
        if(9==lsx && 9==lsy)
        {
            printf("恭喜你走出迷宫,共用时%u秒!\n",time(NULL)-start_time);
            return 0;
        }
        switch(getch())
        {
        case 185:    
        if(9!=lsy && arr[lsx][lsy+1]==' ')
            {
                arr[lsx][lsy+1]='s';
                arr[lsx][lsy]=' ';
                ++lsy;
            }
            break;
        case 186:
        if(0!=lsy && arr[lsx][lsy-1]==' ')
            {
                arr[lsx][lsy-1]='s';
                arr[lsx][lsy]=' ';
                --lsy;
            }
            break;
        case 184:
        if(9!=lsx && arr[lsx+1][lsy]==' ')
            {    
                arr[lsx+1][lsy]='s';
                arr[lsx][lsy]=' ';
                ++lsx;
            }
            break;
        case 183:
        if(0!=lsx && arr[lsx-1][lsy]==' ')
            {
                arr[lsx-1][lsy]='s';
                arr[lsx][lsy]=' ';
                --lsx;
            }
            break;
    }
}
}

 

#ifndef GETCH_H
#define GETCH_H

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

// 修改终端的控制方式,1取消回显、确认 2获取数据 3还原
static int getch(void)
{
    // 记录终端的配置信息
    struct termios old;
    // 获取终端的配置信息
    tcgetattr(STDIN_FILENO,&old);
    // 设置新的终端配置   
    struct termios _new = old;
    // 取消确认、回显
    _new.c_lflag &= ~(ICANON|ECHO);
    // 设置终端配置信息
    tcsetattr(STDIN_FILENO,TCSANOW,&_new);

    // 在新模式下获取数据   
    unsigned int key_val = 0; 
    do{
        key_val = key_val+getchar();
    }while(stdin->_IO_read_end - stdin->_IO_read_ptr);

    // 还原配置信息
    tcsetattr(STDIN_FILENO,TCSANOW,&old); 
    return key_val; 
}

#endif//GETCH_H

 

posted @ 2020-07-13 20:15  竹kj  阅读(175)  评论(0编辑  收藏  举报