33.png
QT写的界面,界面不开源,只研究算法

    #include "driver.h"
#include "user_main.h"
#include  
#include 
uint8_t num_array[3][3] = { 1,2,3,4,5,6,7,8,0 };
unsigned char zerox;                                //空白的位置
unsigned char zeroy;
unsigned char dir;                                //按键方向
unsigned char louck=0,dlouck=0;                                //按键锁  防止重复触发按键
unsigned char game_star = 0;                                //游戏模式
unsigned int step=0;                                //步数
unsigned char hh = 0, mm = 0, ss = 0;                                //时间  时分秒
unsigned int allss;                                //所有秒
unsigned int astepnum = 0;                            //移动步骤
unsigned int cacemap[999][3][3];                            //缓存地图
uint32_t second_t, speed_t;                            //时间变量
uint8_t str[17];                                //保存字符串

void lookupzero()//找空白的位置
{
    char x, y;
    for (y = 0; y < 3; y++)
    {
        for (x = 0; x < 3; x++)
        {
            if (num_array[y][x] == 0)
            {
                zerox=x;
                zeroy=y;
            }
        }
    }
}

int move(char dir,char t)//移动方向,是否真移动
{
    char index;
    if (dir == 0)
    {
        lookupzero();
        if (zeroy != 2)
        {
            if(t==1)
            { 
                num_array[zeroy][zerox] = num_array[zeroy + 1][zerox];
                num_array[zeroy + 1][zerox] = 0;
            }
            return 1;
        }
    }

    if (dir == 1)
    {
        lookupzero();
        if (zeroy != 0)
        {
            if (t == 1)
            {
                num_array[zeroy][zerox] = num_array[zeroy - 1][zerox];
                num_array[zeroy - 1][zerox] = 0;
            }
            return 1;
        }
    }
    if (dir == 2)
    {
        lookupzero();
        if (zerox != 2)
        {
            if (t == 1)
            {
                num_array[zeroy][zerox] = num_array[zeroy][zerox + 1];
                num_array[zeroy][zerox + 1] = 0;
            }
            return 1;
        }
    }
    if (dir == 3)
    {
        lookupzero();
        if (zerox != 0)
        {
            if (t == 1)
            {
                num_array[zeroy][zerox] = num_array[zeroy][zerox - 1];
                num_array[zeroy][zerox - 1] = 0;
            }
            return 1;
        }
    }
    return 0;
}
void randommove(int i)//随机移动i次
{
    int a,b,c;
    for (a = 0; a < i; a++)
    {
        do
        {
            b = rand() % 4;
            c = move(b, 1);
        } while (!c);
    }
}

int checkwin()//检查胜利
{
    if (num_array[0][0] == 1 & num_array[0][1] == 2 & num_array[0][2] == 3 & num_array[1][0] == 4 & num_array[1][1] == 5 & num_array[1][2] == 6 & num_array[2][0] == 7 & num_array[2][1] == 8 & num_array[2][2] == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void goandback(char a) //0前进记录一步   1后退还原一步
{
    char x, y;
    if (a == 0)
    {
        for (y = 0; y < 3; y++)
        {
            for (x = 0; x < 3; x++)
            {
                cacemap[astepnum][y][x] = num_array[y][x];
            }
        }
        astepnum += 1;
    }
    if (a == 1)
    {
        astepnum -= 1;
        for (y = 0; y < 3; y++)
        {
            for (x = 0; x < 3; x++)
            {
                num_array[y][x]=cacemap[astepnum-1][y][x];
            }
        }
    }

}

int constback(char a)//对方向取个反  防止抽搐
{
    if (a == 0)
    {
        return 1;
    }
    if (a == 1)
    {
        return 0;
    }
    if (a == 2)
    {
        return 3;
    }
    if (a == 3)
    {
        return 2;
    }
}

int dil(char t)   //
{
    char a=0, b,c;
    char x, y;
    b = 0;
    for (a = 0; a < 4; a++)
    {
        if (a!= constback(t)&astepnum <= 25)
        {    
            if (move(a, 1))
            {
                goandback(0);
                //RGBBuffer2MatrixScan(num_array);
                //sprintf((char *)str, "UU           %d", astepnum);
                //strToLCD(str, 0x10);
                //HAL_Delay(100);
                //while (!swj_detect());
                if (checkwin())
                {
                    return 1;
                }
                else
                {
                    if (dil(a)) { return 1; }
                }
            }
        }
    }
        goandback(1);
        //for (b = 0; b < 30; b++)
        //{
        //    if (dilmove(a))
        //    {
        //        RGBBuffer2MatrixScan(num_array);
        //        sprintf((char *)str, "UU           %d", astepnum);
        //        strToLCD(str, 0x10);
        //        HAL_Delay(100);
        //        while (!swj_detect());
        //    }
        //    else
        //    {
        //        a += 1;
        //        if (a == 4) { a = 0; }
        //    }
        //}
        //if (checkwin())
        //{
        //    return 1;
        //}
        //else
        //{
        //    dil();
        //}
        //if (checkmap())
        //{
        //    goandback(1);//退一步
        //    sprintf((char *)str, "DD            %d", astepnum);
        //    strToLCD(str, 0x10);
        //    //HAL_Delay(100);
        //    //while (!swj_detect());
        //}
        //else
        //{
        //    goandback(0);//如果这一步可以走 记录一下
        //    RGBBuffer2MatrixScan(num_array);
        //    sprintf((char *)str, "UU           %d", astepnum);
        //    strToLCD(str, 0x10);
        //    HAL_Delay(10);
        //    //while (!swj_detect());
        //    if (checkwin())
        //    {
        //        return 1;
        //    }
        //        dil();
        //    
        //}    
    //goandback(1);//退一步
    return 0;
}

void autorun()
{
    char a;
    char x, y;
    goandback(0);
    if (dil(0))
    {
        //sprintf((char *)str, "    YES!!!!!    ");
        //strToLCD(str, 0x00);
        for (a = 0; a < astepnum; a++)
        {
            for (y = 0; y < 3; y++)
            {
                for (x = 0; x < 3; x++)
                {
                    num_array[y][x] = cacemap[a][y][x];
                }
            }
            RGBBuffer2MatrixScan(num_array);
            HAL_Delay(400);
            step += 1;
            sprintf((char *)str, "step couter  %d", step);
            strToLCD(str, 0x10);
        }
        game_star = 1;
    }
    else
    {
        sprintf((char *)str, "    NO!!!!!!    ");//无解
        strToLCD(str, 0x00);
        sprintf((char *)str, "                ");
        strToLCD(str, 0x10);
        game_star = 0;
    }
}

void setup()
{
    sprintf((char *)str, "    WELLCOME    ");
    strToLCD(str, 0x00);
    sprintf((char *)str, "Digital Klotski!");
    strToLCD(str, 0x10);
}

void loop()
{
    /*time*/
    if (HAL_GetTick()> second_t)
    {
        second_t += 1000;
        ss++;
        if (ss == 60) { ss = 0; mm++; if (mm == 60) { mm = 0; hh++; } }
    }
    /*游戏中*/
    if (game_star == 0)
    {
        /*空格检测对应操作*/
        if (dlouck == 0)
        {
            if (swj_detect())
            {
                game_star = 1;
                randommove(HAL_GetTick()%1000);//
                step = 0;
                ss = 0;
                mm = 0;
                hh = 0;
                sprintf((char *)str, "                ");
                strToLCD(str, 0x00);
                sprintf((char *)str, "                ");
                strToLCD(str, 0x10);
                dlouck = 1;
            }
        }
        if (dlouck == 1)
        {
            if (!swj_detect())
            {
                dlouck = 0;
            }
        }
        /*方向按键检测对应操作*/
        dir = getDir();
        if (louck == 0)
        {
            if (move(dir, 1))
            {
                step += 1;
            }
            louck = 1;
        }
        if (louck == 1 & dir == NN)
        {
            louck = 0;
        }
    }
    if (game_star == 1)
    {
        /*空格检测对应操作*/
        if (dlouck == 0)
        {
            if (swj_detect())
            {
                game_star = 2;
                dlouck = 1;
            }
        }
        if (dlouck == 1)
        {
            if (!swj_detect())
            {
                dlouck = 0;
            }
        }
        sprintf((char *)str, "timer:");
        strToLCD(str, 0x00);
        if (mm < 10)
        {
            sprintf((char *)str, "0%d", mm);
            strToLCD(str, 0x0a);
        }
        else
        {
            sprintf((char *)str, "%d", mm);
            strToLCD(str, 0x0a);
        }
        if (ss<10)
        {
            sprintf((char *)str, ":0%d", ss);
            strToLCD(str, 0x0d);
        }
        else
        {
            sprintf((char *)str, ":%d", ss);
            strToLCD(str, 0x0d);
        }
        sprintf((char *)str, "step couter  %d",step);
        strToLCD(str, 0x10);
        if (checkwin())
        {
            sprintf((char *)str, "                ");
            strToLCD(str, 0x00);
            sprintf((char *)str, "                ");
            strToLCD(str, 0x10);
            game_star = 0;
            sprintf((char *)str, "    Victory!    ");
            strToLCD(str, 0x00);
            allss = ss+(mm*60);
            sprintf((char *)str, "%d",allss);
            strToLCD(str, 0x10);
            sprintf((char *)str, "sec");
            strToLCD(str, 0x14);
            sprintf((char *)str, "%d",step-1);
            strToLCD(str, 0x18);
            sprintf((char *)str, "step");
            strToLCD(str, 0x1b);
        }
        /*方向按键检测对应操作*/
        dir = getDir();
        if (louck == 0)
        {
            if (move(dir, 1))
            {
                step += 1;
            }
            louck = 1;
        }
        if (louck == 1 & dir == NN)
        {
            louck = 0;
        }
    }
    if (game_star == 2)
    {
        autorun();
    }
    RGBBuffer2MatrixScan(num_array);

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