GIF 2020-11-6 11-19-33.gif

这是我2019年某月写的程序,主要玩法是方向键控制角色一次性走完所有格子不重复

功能:

  1. 自定义多关卡
  2. 使用递归自动搜索路径

操作:

  1. 方向键控制角色
  2. 按字母a自动计算路径

控制台的驱动,其实也不能算控制台驱动,只是当时是为了模拟stm32的hel库

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursorInfo;                    //设置控制台用的

void windows_init()
{
    GetConsoleCursorInfo(hOut, &CursorInfo);        //获取控制台光标信息
    CursorInfo.bVisible = false;            //隐藏控制台光标
    SetConsoleCursorInfo(hOut, &CursorInfo);        //设置控制台光标状态

    srand(time(0));
}

void myprint(int y, int x, char colo,char color, const char* p)//
{
    COORD pos = { x,y };
    SetConsoleCursorPosition(hOut, pos);
    SetConsoleTextAttribute(hOut, colo+(color*0x10));
    printf(p);
}
char get_key()                    //获得按键键值
{
    char a;
    if (_kbhit() == 1)            //由于单使用 _getch()会导致程序堵塞,所以用先用_kbhit()获得是否有按键按下
    {
        a = _getch();
        return a;
    }
    return 0;

}

long HAL_GetTick()
{
    return clock();

}

void HAL_Delay(long a)
{
    long b;
    b = HAL_GetTick() + a;
    while (b> HAL_GetTick());

}

主功能代码

#define max_map 20
#define max_level 10
struct home
{
    char l_map[max_map][max_map];
    int l_player_y;
    int l_player_x;
    char flag;


};
home levels[max_level] =
{
    //*************level 0
    {
        {
            { 0,0,1,1,1,1,1,1,1 },
            { 1,1,1,0,0,0,0,0,0 },

        },
        0,
        0,
        1,
    },
    //*************level 1
    {
        {
            {1,1,0},
            {0,1,1},
        },
        1,
        0,
        1,
    },
    //*************level 2
    {
        {
            {1,1,0},
            {1,1,1},
            {1,1,1},
        },
        0,
        2,
        1,
    },

    //*************level 3
    {
        {
            {0,1,1,1,1,1,1},
            {0,1,1,1,0,0,1},
            {0,1,1,1,1,1,1},

        },
        0,
        0,
        1,
    },

};

char map[max_map][max_map];
int player_x=0, player_y=0;

char input_level(int a)
{
    char y, x, z;
    if (levels[a].flag == 1)
    {
        for (y=0;y< max_map;y++)
        {
            for (x = 0; x < max_map; x++)
            {
                map[y][x] = levels[a].l_map[y][x];
            }
        }
        player_y = levels[a].l_player_y;
        player_x = levels[a].l_player_x;
        map[player_y][player_x] = 0;
        return 1;
    }

    return 0;

}


#define entity "[ ]"
#define hole   "   "
#define player " * "

void displaymapandplayer()//显示地图和玩家
{
    int y, x;
    for (y = 0; y < max_map; y++)
    {
        for (x = 0; x < max_map; x++)
        {
            if ((player_y == y) && (player_x == x))
            {
                myprint(y, x * 3, white, 0, player);
            }
            else
            {
                if (map[y][x] == 1)
                {
                    myprint(y, x * 3, white, 0, entity);
                }
                if (map[y][x] == 0)
                {
                    myprint(y, x * 3, white, 0, hole);
                }
            }
        }
    }
}

int cheack_win()//检查是否胜利
{
    int i = 0, j = 0;
    for (i = 0; i < max_map; i++)
    {
        for (j = 0; j < max_map; j++)
        {
            if (map[j][i] == 1)
            {
                return 1;
            }
        }
    }
    return 0;
}

int moveplayer(int a)//移动玩家 a移动方向  b是否移动 。如果b==0只会返回这一步是否可以走,不会真的移动玩家
{
    if (a == uu)
    {
        if ((map[player_y - 1][player_x] == 0)|| ((player_y - 1)<0))
        {
            return 0;
        }
        else
        {
            
                player_y = player_y - 1;
                map[player_y][player_x] = 0;
            
            return 1;
        }
    }
    if (a == dd)
    {
        if ((map[player_y + 1][player_x] == 0)||((player_y + 1)>=max_map))
        {
            return 0;
        }
        else
        {
            
                player_y = player_y + 1;
                map[player_y][player_x] = 0;
            
            return 1;
        }
    }
    if (a == ll)
    {
        if ((map[player_y][player_x - 1] == 0)||((player_x - 1)<0))
        {
            return 0;
        }
        else
        {
            
                player_x = player_x - 1;
                map[player_y][player_x] = 0;
            
            return 1;
        }
    }
    if (a == rr)
    {
        if ((map[player_y][player_x + 1] == 0)||((player_x + 1)>=max_map))
        {
            return 0;
        }
        else
        {
            
                player_x = player_x + 1;
                map[player_y][player_x] = 0;
            
            return 1;
        }
    }
    return 0;
}


/************************************************************auto*/
int countrun(char a)//计算可以走的方向
{

    return 0;
}

int hc_x[1000];//路径缓存
int hc_y[1000];
int jd_num = 0;//节点数量

int dg_move(char d)
{
    if (d == 0)
    {
        return moveplayer(ll);
    }
    if (d == 1)
    {
        return moveplayer(dd);
    }
    if (d == 2)
    {
        return moveplayer(uu);
    }
    if (d == 3)
    {
        return moveplayer(rr);
    }
    return 0;
}

char buff1[10];
int mainrecursive()//递归
{
    char a;
    for (a = 0; a < 4; a++)
    {
        
            if (dg_move(a)==1)
            {
                hc_x[jd_num] = player_x;
                hc_y[jd_num] = player_y;
                jd_num += 1;
                
                displaymapandplayer();
                sprintf(buff1,"%d", jd_num);
                myprint(21,21,white,0,buff1);
                HAL_Delay(100);

                mainrecursive();
            }
        
    }
    if (cheack_win() == 0)
    {
        return 1;
    }
    if (jd_num == 0)
    {
        return 0;
    }
    else
    {
        jd_num -= 1;
        map[player_y][player_x] = 1;//回溯 地图还原
        player_x = hc_x[jd_num - 1];  //玩家返回上一次的位置
        player_y = hc_y[jd_num - 1];

        displaymapandplayer();
        sprintf(buff1, "%d", jd_num);
        myprint(21, 21, white, 0, buff1);
        HAL_Delay(100);
    }
    

    return 0;
    
}



void showjie()
{
    int a,y,x;
    char str[6];
    for (y = 0; y < max_map; y++)
    {
        for (x = 0; x < max_map; x++)
        {
            myprint(y + 21, x * 4, white, 0, "    ");
        }
    }
    for (a = 0; a < jd_num; a++)
    {
        if (a < 10)
        {
            sprintf(str, "[% 2d]", a);
        }
        else
        {
            sprintf(str, "[%2d]", a);
        }
        
        myprint(hc_y[a]+21,hc_x[a]*4,white,0,str);
    }

}
/****************************************************************/
int main()
{
    enum { star, exel,auto_m, over };
    char run = star, key = 0;
    int level = 0;
    windows_init();
    while (1)
    {
        key = get_key();
        switch (run)
        {
        case star:
            
            if (input_level(level))
            {
                run = exel;
            }
            else
            {
                run = over;
            }
            break;
        case exel:
            if (key == space)
            {
                run = star;
            }
            moveplayer(key);
            
            if (key == 97) 
            { 
                input_level(level);
                run = auto_m; 
            }//
            if (cheack_win() == 0)
            {
                level += 1;
                run = star;
            }
            break;
        case auto_m:
            //
            jd_num = 0;
            if (mainrecursive())
            {                
                //myprint(21, 0, white, 0, "ok");
                showjie();
                run = star;
            }
            else
            {
                //myprint(21, 0, white, 0, "no");
                run = star;
            }
            break;
        case over:
            //game_over();
            level = 0;
            run = star;
            break;
        default:
            break;
        }
        displaymapandplayer();
    }
}

最后修改:2020 年 11 月 06 日
声明:无闻风博客|版权所有,违者必究|如未注明,均为原创| 转载:转载请注明原文链接