使用QT开发2048小游戏

众所周知……在雷课堂后大部分人都掌握了QT界面的能力。
本着浪费人生与无聊写游戏的宗旨,我决定用QT写一个2048玩玩。(以后可能还会更新黑白棋和五子棋哟!)
那么2048其实难度不大,主要考虑几个点,一个是刷新,需要随机更新2或4,一个是堆叠和合并,因为堆叠过程中由于可能出现一行4个2这些情况,所以合并后要再次堆叠。于是乎,首先需要一个游戏界面,一个游戏核心类,外加一个主界面。所以QT先是把Mainwindow设计好,然后处理游戏。
在这过程中还要进行打分,并且还自带了log类似的功能。
先看看总体是这样!
在这里插入图片描述那么再看看核心类
gamecore.h

class gamecore
{
public:
    gamecore();          //构造函数
    ~gamecore();         //析构函数
    void RESET();        //初始化棋盘
    int judgewin();      //分析是否胜利,返回值为1则胜利,0则未胜利,2则输
    bool push(char x);   //推动数字,若能推动则堆叠并返回1,否则返回0表示无法堆叠
    bool mix(char x);    //将可能可合并的数字合并
    void create();       //随机生成一个数字在空白区域,可能为2或4
    int getcurrentscore();//获取当前分数
    int gethighestscore();//获取当前最高分
    bool savetodata();   //保存游戏
    int board[4][4];     //棋盘
private:
    int highest_score;   //记录历史最高分
    int current_score;   //记录当前分数
    QString situation;   //记录游戏情况,类似于log
};


#endif // GAMECORE_H

gamecore.cpp

#include "gamecore.h"
#include <QTime>


gamecore::gamecore()         //构造函数
{
    highest_score = 0;       //每次游戏必须初始化
    current_score = 0;
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<4;j++)
        {
            board[i][j] = 0; //初始化棋盘
        }
    }
    board[0][1] = 2;         //一开始只能先这样凑合
    board[2][2] = 2;
    situation = "Player starts playing\n";
}


gamecore::~gamecore()        //析构函数
{


}




void gamecore::RESET()       //初始化棋盘,随便找两个位置弄数字
{
    int n,n1,n2;
    int i,j;
    for( i = 0;i<4;i++)
    {
        for( j = 0;j<4;j++)
        {
            board[i][j] = 0; //初始化棋盘
        }
    }
    QTime t;
    t=QTime::currentTime();
    qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    n=qrand()%255;        //获取0~255的数字
    n1 = n%16;
    n2 = n/16;
    if(n1 == n2)
    {
        board[n1/4][n1%4] = 2;
        board[n2/4+1][n2%4+1] = 2;
    }
    else
    {
        board[n1/4][n1%4] = 2;
        board[n2/4][n2%4] = 2;
    }
    current_score = 0;               //现有分数也刷新为0
    situation += "player restart game\n";
}






int gamecore::judgewin()     //分析是否胜利,返回值为1则胜利,0则未胜利,2则输
{
    int judge = 2;
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(board[i][j] == 2048)
            {
                judge = 1;
                situation += "the player win!!!\n\n\n";     //先看有没有赢
                return judge;
            }
        }
    }
    for(int i = 0;i<4;i++)                          //再看有没有空位
    {
        for(int j = 0;j<4;j++)
        {
            if(board[i][j] == 0)
            {
                judge = 0;
                return judge;
            }
        }
    }
    //没有空位,看看有没有相邻相同的,这里用镜像棋盘
    int mirror[6][6];
    for(int i = 0;i<6;i++)
    {
        for(int j = 0;j<6;j++)
        {
            mirror[i][j] = -100;               //初始化为负数
        }
    }
    for(int i = 1;i<5;i++)
    {
        for(int j = 1;j<5;j++)
        {
            mirror[i][j] = board[i-1][j-1];      //心部分赋值
        }
    }
    for(int i = 1;i<5;i++)                       //如果内部四块存在相邻相同
    {
        for(int j = 1;j<5;j++)
        {
            if(mirror[i][j] == mirror[i+1][j] || mirror[i][j] == mirror[i-1][j] || mirror[i][j] == mirror[i][j-1] || mirror[i][j] == mirror[i][j+1])
            {
                judge = 0;                  //可继续游戏
                return judge;
            }


        }
    }
    //否则肯定输了
    situation += "the player lose……\n\n\n";
    return judge;
}


/*******************************************************************
【函数】push
【功能】按玩家指示方向推动数字并堆叠在一起
【参数】WASD
【返回值】若能推动则堆叠并返回1,否则返回0表示无法堆叠
【开发者】NEO
【修改记录】
************************************************************************/


bool gamecore::push(char x)
{
    int i,j,k;
    bool judge = false;
    if(x == 'W')
    {
       for(i = 1;i<4;i++)
       {
           for(j = 0;j<4;j++)
           {
               if(board[i][j]!= 0)
               {
                   if(board[i-1][j] != 0)
                   {
                       continue;
                   }
                   for(k = i-1;k>=0;k--)
                   {
                       if(board[k][j] != 0 && k != i-1)               //往上看,若出现一个不为0的,且不是贴着的
                       {
                           board[k+1][j] = board[i][j];               //移动到它下方
                           board[i][j] = 0;
                           judge = true;
                           break;
                       }
                       else if(board[k][j] == 0 && k == 0)           //如果上面都空的
                       {
                           board[k][j] = board[i][j];                //移动到顶部
                           board[i][j] = 0;
                           judge = true;
                           break;
                       }
                   }
               }
           }
       }
       situation += "player pressed W\n";
       return judge;
    }
    else if(x == 'S')
    {
        for(i = 2;i>=0;i--)
        {
            for(j = 0;j<4;j++)
            {
                if(board[i][j] != 0)
                {
                    if(board[i+1][j] != 0)
                    {
                        continue;
                    }
                    for(k = i+1;k<4;k++)
                    {
                        if(board[k][j] != 0 && k != i+1)               //往下看,若出现一个不为0的,且不是贴着的
                        {
                            board[k-1][j] = board[i][j];               //移动到它上方
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                        else if(board[k][j] == 0 && k == 3)           //如果下面都空的
                        {
                            board[k][j] = board[i][j];                //移动到底部
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                    }
                }
            }
        }
        situation += "player pressed S\n";
        return judge;
    }
    else if(x == 'A')
    {
        for(j = 1;j<4;j++)
        {
            for(i = 0;i<4;i++)
            {
                if(board[i][j] != 0)
                {
                    if(board[i][j-1] != 0)
                    {
                        continue;
                    }
                    for(k = j-1;k>=0;k--)
                    {
                        if(board[i][k] != 0 && k != j-1)               //往左看,若出现一个不为0的,且不是贴着的
                        {
                            board[i][k+1] = board[i][j];               //移动到它右方
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                        else if(board[i][k] == 0 && k == 0)           //如果左面都空的
                        {
                            board[i][k] = board[i][j];                //移动到左底部
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                    }
                }
            }
        }
        situation += "player pressed A\n";
        return judge;
    }
    else if(x == 'D')
    {
        for(j = 2;j >= 0;j--)
        {
            for(i = 0;i<4;i++)
            {
                if(board[i][j] != 0)
                {
                    if(board[i][j+1] != 0)
                    {
                        continue;
                    }
                    for(k = j+1;k<4;k++)
                    {
                        if(board[i][k] != 0 && k != j+1)               //往右看,若出现一个不为0的,且不是贴着的
                        {
                            board[i][k-1] = board[i][j];               //移动到它左方
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                        else if(board[i][k] == 0 && k == 3)           //如果右面都空的
                        {
                            board[i][k] = board[i][j];                //移动到右底部
                            board[i][j] = 0;
                            judge = true;
                            break;
                        }
                    }
                }
            }
        }
        situation += "player pressed D\n";
        return judge;
    }
    return false;             //如果if语句不进一定false
}








bool gamecore::mix(char x)         //将可能可合并的数字合并
{
    int i,j;
    bool judge = false;


    switch(x)
    {
    case 'W':
        for(i = 1; i < 4; i++)
        {
            for(j = 0; j < 4; j++)
            {
                if(board[i][j] != 0)         //如果发现非空
                {
                    if(board[i][j] == board[i-1][j]) //若相邻上面有数字可合并
                    {
                        board[i-1][j] = board[i][j] * 2;//合并
                        board[i][j] = 0;       //原位置空
                        current_score += board[i-1][j];       //当前分数更新
                        if(highest_score<current_score)       //如果最高分低于当前分数,则更新
                        {
                            highest_score = current_score;
                        }
                        judge = true;
                    }
                }
            }
        }
        push('W');     //最后再推一下防止出现三个四个同数字情况
        break;
    case 'S':
        for(i = 3; i >=0; i--)
        {
            for(j = 0; j < 4; j++)
            {
                if(board[i][j] != 0)         //如果发现非空
                {
                    if(board[i][j] == board[i+1][j]) //若相邻下面有数字可合并
                    {
                        board[i+1][j] = board[i][j] * 2;//合并
                        board[i][j] = 0;       //原位置空
                        current_score += board[i+1][j];       //当前分数更新
                        if(highest_score<current_score)       //如果最高分低于当前分数,则更新
                        {
                            highest_score = current_score;
                        }
                        judge = true;
                    }
                }
            }
        }
        push('S');     //最后再推一下防止出现三个四个同数字情况
        break;


    case 'A':
        for(i = 0; i <4; i++)
        {
            for(j = 1; j < 4; j++)
            {
                if(board[i][j] != 0)                          //如果发现非空
                {
                    if(board[i][j] == board[i][j-1])          //若相邻左面有数字可合并
                    {
                        board[i][j-1] = board[i][j] * 2;      //合并
                        board[i][j] = 0;                      //原位置空
                        current_score += board[i][j-1];       //当前分数更新
                        if(highest_score<current_score)       //如果最高分低于当前分数,则更新
                        {
                            highest_score = current_score;
                        }
                        judge = true;
                    }
                }
            }
        }
        push('A');     //最后再推一下防止出现三个四个同数字情况
        break;
    case 'D':
        for(i = 0; i <4; i++)
        {
            for(j = 3; j >= 0; j--)
            {
                if(board[i][j] != 0)         //如果发现非空
                {
                    if(board[i][j] == board[i][j+1]) //若相邻右面有数字可合并
                    {
                        board[i][j+1] = board[i][j] * 2;//合并
                        board[i][j] = 0;                //原位置空
                        current_score += board[i][j+1];       //当前分数更新
                        if(highest_score<current_score)       //如果最高分低于当前分数,则更新
                        {
                            highest_score = current_score;
                        }
                        judge = true;
                    }
                }
            }
        }
        push('D');     //最后再推一下防止出现三个四个同数字情况
        break;
    }
    return judge;
}




void gamecore::create()       //随机生成一个数字在空白区域,可能为2或4
{
    int i,j,k = 0;
    int loc;
    int location[16] = {0};
    for(i = 0;i<4;i++)
    {
        for(j = 0;j<4;j++)
        {
            if(board[i][j] == 0)
            {
                location[k] = i*4+j;
                k++;
            }
        }
    }
    if(k == 1)
    {
        loc = location[0];
    }
    else
    {
        QTime t;
        t = QTime::currentTime();
        qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
        loc = qrand()%(k-1);        //获取0~k-1的随机数
        loc = location[loc];
    }
    k = qrand()%3;            //获取0~3的随机数
    if(k == 0 || k == 1)
    {
        board[loc/4][loc%4] = 2;
    }
    else if(k == 2 || k == 3)
    {
        board[loc/4][loc%4] = 4;
    }
}






bool gamecore::savetodata()    //保存游戏记录
{
    QString time = "游戏结束时间:";
    QDateTime dat;
    time += dat.currentDateTime().toString("yyyy-MM-dd HH-mm-ss");
    bool opx = false;
    QString fileName = time + "游戏日志.txt";
    QFile file(fileName);
    opx = file.open(QIODevice::WriteOnly | QIODevice::Append);
    if(!opx)
    {
        return opx;
    }
    QTextStream in(&file);
    in<<situation;
    file.close();
    return true;
}


int gamecore::getcurrentscore()//获取当前分数
{
    situation += "current score is " + QString::number(current_score) + "\n";
    return current_score;
}

int gamecore::gethighestscore()//获取当前最高分
{
    situation += "the highest score is " + QString::number(highest_score) + "\n";
    return highest_score;
}

emmm是不是非常浅显易懂。
对于玩游戏的play界面,主要问题一个是显示,要把board上的数字显示到QLabel上,同时还要把颜色标清楚,所以有点麻烦(超级麻烦
但感谢我健壮有力的肝和搬砖的决心,即使不知道怎么ui传参,我也硬怼出来了
下面放上我惨不忍睹的代码……○| ̄|_
play.h

#ifndef PLAY_H
#define PLAY_H
#include <QWidget>
#include <QObject>
#include "gamecore.h"

/***********************************************************
【函数】showing用于把镜像数组board显示到label上给玩家看

*************************************************************/
namespace Ui {
class play;
}


class play : public QWidget
{
    Q_OBJECT


public:
    explicit play(QWidget *parent = nullptr);
    ~play();
    void showing();          //展示到label上
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);
private slots:
    void on_exit_clicked();
    void on_restart_clicked(); 
private:
    Ui::play *ui;
    gamecore *p;           //游戏核心的指针
    bool move;             //判断玩家是否按下任何按键,为真则不能接受其它按键
};


#endif // PLAY_H

play.cpp

#include "play.h"
#include "ui_play.h"
#include <QKeyEvent>
#include <synchapi.h>
#include <QMessageBox>


play::play(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::play)
{
    ui->setupUi(this);
    gamecore *game = new gamecore();
    p = game;
    move = false;
}


play::~play()
{
    delete ui;
}


void play::on_exit_clicked()
{
    if(QMessageBox::Yes == QMessageBox::question(this,QStringLiteral("2048"),QStringLiteral("Do you really want to exit?"),QMessageBox::Yes | QMessageBox:: No))
    {
        p->savetodata();
        this->close();
        delete ui;
        return;
    }
    else
    {
        return;
    }
}


void play::showing()       //展示到label上
{
    if(p->board[0][0] == 0)
    {
        ui->AA->setText(nullptr);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
    }
    else if(p->board[0][0] == 2)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
    }
    else if(p->board[0][0] == 4)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
    }
    else if(p->board[0][0] == 8)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
    }
    else if(p->board[0][0] == 16)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
    }
    else if(p->board[0][0] == 32)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
    }
    else if(p->board[0][0] == 64)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
    }
    else if(p->board[0][0] == 128)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
    }
    else if(p->board[0][0] == 256)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
    }
    else if(p->board[0][0] == 512)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
    }
    else if(p->board[0][0] == 1024)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
    }
    else if(p->board[0][0] == 2048)
    {
        ui->AA->setNum(p->board[0][0]);
        ui->AA->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
    }
    if(p->board[0][1] == 0)
        {
            ui->AB->setText(nullptr);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
        }
        else if(p->board[0][1] == 2)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
        }
        else if(p->board[0][1] == 4)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
        }
        else if(p->board[0][1] == 8)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
        }
        else if(p->board[0][1] == 16)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
        }
        else if(p->board[0][1] == 32)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
        }
        else if(p->board[0][1] == 64)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
        }
        else if(p->board[0][1] == 128)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
        }
        else if(p->board[0][1] == 256)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
        }
        else if(p->board[0][1] == 512)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
        }
        else if(p->board[0][1] == 1024)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
        }
        else if(p->board[0][1] == 2048)
        {
            ui->AB->setNum(p->board[0][1]);
            ui->AB->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
        }
    if(p->board[0][2] == 0)
            {
                ui->AC->setText(nullptr);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[0][2] == 2)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[0][2] == 4)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[0][2] == 8)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[0][2] == 16)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[0][2] == 32)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[0][2] == 64)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[0][2] == 128)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[0][2] == 256)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[0][2] == 512)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[0][2] == 1024)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[0][2] == 2048)
            {
                ui->AC->setNum(p->board[0][2]);
                ui->AC->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[0][3] == 0)
            {
                ui->AD->setText(nullptr);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[0][3] == 2)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[0][3] == 4)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[0][3] == 8)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[0][3] == 16)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[0][3] == 32)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[0][3] == 64)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[0][3] == 128)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[0][3] == 256)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[0][3] == 512)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[0][3] == 1024)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[0][3] == 2048)
            {
                ui->AD->setNum(p->board[0][3]);
                ui->AD->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[1][0] == 0)
            {
                ui->BA->setText(nullptr);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[1][0] == 2)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[1][0] == 4)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[1][0] == 8)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[1][0] == 16)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[1][0] == 32)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[1][0] == 64)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[1][0] == 128)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[1][0] == 256)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[1][0] == 512)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[1][0] == 1024)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[1][0] == 2048)
            {
                ui->BA->setNum(p->board[1][0]);
                ui->BA->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[1][1] == 0)
            {
                ui->BB->setText(nullptr);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[1][1] == 2)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[1][1] == 4)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[1][1] == 8)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[1][1] == 16)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[1][1] == 32)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[1][1] == 64)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[1][1] == 128)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[1][1] == 256)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[1][1] == 512)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[1][1] == 1024)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[1][1] == 2048)
            {
                ui->BB->setNum(p->board[1][1]);
                ui->BB->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[1][2] == 0)
            {
                ui->BC->setText(nullptr);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[1][2] == 2)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[1][2] == 4)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[1][2] == 8)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[1][2] == 16)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[1][2] == 32)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[1][2] == 64)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[1][2] == 128)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[1][2] == 256)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[1][2] == 512)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[1][2] == 1024)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[1][2] == 2048)
            {
                ui->BC->setNum(p->board[1][2]);
                ui->BC->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[1][3] == 0)
            {
                ui->BD->setText(nullptr);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[1][3] == 2)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[1][3] == 4)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[1][3] == 8)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[1][3] == 16)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[1][3] == 32)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[1][3] == 64)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[1][3] == 128)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[1][3] == 256)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[1][3] == 512)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[1][3] == 1024)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[1][3] == 2048)
            {
                ui->BD->setNum(p->board[1][3]);
                ui->BD->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[2][0] == 0)
            {
                ui->CA->setText(nullptr);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[2][0] == 2)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[2][0] == 4)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[2][0] == 8)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[2][0] == 16)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[2][0] == 32)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[2][0] == 64)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[2][0] == 128)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[2][0] == 256)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[2][0] == 512)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[2][0] == 1024)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[2][0] == 2048)
            {
                ui->CA->setNum(p->board[2][0]);
                ui->CA->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[2][1] == 0)
            {
                ui->CB->setText(nullptr);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[2][1] == 2)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[2][1] == 4)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[2][1] == 8)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[2][1] == 16)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[2][1] == 32)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[2][1] == 64)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[2][1] == 128)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[2][1] == 256)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[2][1] == 512)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[2][1] == 1024)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[2][1] == 2048)
            {
                ui->CB->setNum(p->board[2][1]);
                ui->CB->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[2][2] == 0)
            {
                ui->CC->setText(nullptr);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[2][2] == 2)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[2][2] == 4)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[2][2] == 8)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[2][2] == 16)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[2][2] == 32)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[2][2] == 64)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[2][2] == 128)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[2][2] == 256)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[2][2] == 512)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[2][2] == 1024)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[2][2] == 2048)
            {
                ui->CC->setNum(p->board[2][2]);
                ui->CC->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[2][3] == 0)
            {
                ui->CD->setText(nullptr);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[2][3] == 2)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[2][3] == 4)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[2][3] == 8)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[2][3] == 16)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[2][3] == 32)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[2][3] == 64)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[2][3] == 128)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[2][3] == 256)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[2][3] == 512)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[2][3] == 1024)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[2][3] == 2048)
            {
                ui->CD->setNum(p->board[2][3]);
                ui->CD->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }


    if(p->board[3][0] == 0)
            {
                ui->DA->setText(nullptr);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[3][0] == 2)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[3][0] == 4)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[3][0] == 8)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[3][0] == 16)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[3][0] == 32)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[3][0] == 64)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[3][0] == 128)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[3][0] == 256)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[3][0] == 512)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[3][0] == 1024)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[3][0] == 2048)
            {
                ui->DA->setNum(p->board[3][0]);
                ui->DA->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[3][1] == 0)
            {
                ui->DB->setText(nullptr);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[3][1] == 2)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[3][1] == 4)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[3][1] == 8)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[3][1] == 16)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[3][1] == 32)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[3][1] == 64)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[3][1] == 128)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[3][1] == 256)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[3][1] == 512)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[3][1] == 1024)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[3][1] == 2048)
            {
                ui->DB->setNum(p->board[3][1]);
                ui->DB->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[3][2] == 0)
            {
                ui->DC->setText(nullptr);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[3][2] == 2)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[3][2] == 4)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[3][2] == 8)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[3][2] == 16)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[3][2] == 32)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[3][2] == 64)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[3][2] == 128)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[3][2] == 256)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[3][2] == 512)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[3][2] == 1024)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[3][2] == 2048)
            {
                ui->DC->setNum(p->board[3][2]);
                ui->DC->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
    if(p->board[3][3] == 0)
            {
                ui->DD->setText(nullptr);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,255,255);}");
            }
            else if(p->board[3][3] == 2)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
            }
            else if(p->board[3][3] == 4)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,170,0);}");
            }
            else if(p->board[3][3] == 8)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(190,125,100);}");
            }
            else if(p->board[3][3] == 16)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,85,127);}");
            }
            else if(p->board[3][3] == 32)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,85,255);}");
            }
            else if(p->board[3][3] == 64)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(85,85,255);}");
            }
            else if(p->board[3][3] == 128)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
            }
            else if(p->board[3][3] == 256)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(170,0,0);}");
            }
            else if(p->board[3][3] == 512)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
            }
            else if(p->board[3][3] == 1024)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(0,170,127);}");
            }
            else if(p->board[3][3] == 2048)
            {
                ui->DD->setNum(p->board[3][3]);
                ui->DD->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
            }
}


void play::on_restart_clicked()
{
    if(QMessageBox::Yes == QMessageBox::question(this,QStringLiteral("2048"),QStringLiteral("Do you really want to restart?"),QMessageBox::Yes | QMessageBox:: No))
    {
        p->RESET();         //重新开始
        ui->score->setNum(0);
        showing();
        return;
    }
    else
    {
        return;
    }
}


void play::keyPressEvent(QKeyEvent *event)
{
    bool judge1 = false,judge2 = false;
    int judge3 = 0;
    if(event->key() == Qt::Key_W || event->key() == Qt::Key_Up)
    {
        if(move == false)
        {
            move = true;
            judge1 = p->push('W');
            judge2 = p->mix('W');
            if(!(judge1 || judge2))      //如果两个操作成功都失败了
            {
                return;                   //说明是无效的移动,既不更新也不判断直接返回
            }
        }


    }
    else if(event->key() == Qt::Key_S || event->key() == Qt::Key_Down)
    {
        if(move == false)
        {
            move = true;
            judge1 = p->push('S');
            judge2 = p->mix('S');
            if(!(judge1 || judge2))      //如果两个操作成功都失败了
            {
                return;                   //说明是无效的移动,既不更新也不判断直接返回
            }
        }
    }
    else if(event->key() == Qt::Key_A || event->key() == Qt::Key_Left)
    {
        if(move == false)
        {
            move = true;
            judge1 = p->push('A');
            judge2 = p->mix('A');
            if(!(judge1 || judge2))      //如果两个操作成功都失败了
            {
                return;                   //说明是无效的移动,既不更新也不判断直接返回
            }
        }
    }
    else if(event->key() == Qt::Key_D || event->key() == Qt::Key_Right)
    {
        if(move == false)
        {
            move = true;
            judge1 = p->push('D');
            judge2 = p->mix('D');
            if(!(judge1 || judge2))      //如果两个操作成功都失败了
            {
                return;                   //说明是无效的移动,既不更新也不判断直接返回
            }
        }
    }
    else
    {
        return;
    }
    if(judge1 || judge2)      //如果两个操作成功
    {
        showing();            //先展示
        ui->highest->setNum(p->gethighestscore());   //显示分数
        ui->score->setNum(p->getcurrentscore());     //显示分数
        Sleep(200);           //停顿
        p->create();          //更新
        showing();            //展示更新后情况
        judge3 = p->judgewin(); //判定胜负
        if(judge3 == 0)        //未胜利
        {
            return;            //继续
        }
        else if(judge3 == 1)    //胜利
        {
            QMessageBox::information(NULL, "2048", "Congratulations,you win!");
            p->RESET();         //重新开始
            ui->score->setNum(0);
            showing();
            return;
        }
        else if(judge3 == 2)    //输了
        {
            QMessageBox::information(NULL, "2048", "OOPS,you lose!");
            p->RESET();         //重新开始
            ui->score->setNum(0);
            showing();
            return;
        }


    }
}


void play::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_W || event->key() == Qt::Key_Up||event->key() == Qt::Key_S || event->key() == Qt::Key_Down||event->key() == Qt::Key_A || event->key() == Qt::Key_Left||event->key() == Qt::Key_D || event->key() == Qt::Key_Right)
    {
        if(move == true)
        {
            move = false;
        }
    }
}

那么有需要的我把文件先放这里可以随意取用
https://download.csdn.net/download/Mr__NEO/12565934
如果有帮到你或是有所启发欢迎点赞+收藏+评论。

  • 8
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
2048是一款非常受欢迎的数字益智游戏,玩家通过滑动屏幕上下左右四个方向,使相同数字的方块相加,最终得到数字2048的方块。 要实现这个游戏的代码,我们可以使用Qt框架来开发。首先,我们需要创建一个游戏界面,可以使用Qt的QWidget或QML来实现。在界面中,我们可以使用QGridLayout来布局2048方块的格子。 每个方块都可以表示为一个对象,我们称之为Block。每个Block对象都有一个数字属性和一个放置在游戏界面上的位置属性。在游戏开始时,我们可以生成两个随机数并创建对应的Block对象。 接下来,我们需要实现玩家滑动屏幕的操作。当玩家滑动时,我们需要判断滑动的方向并将相同数字的方块相加。例如,如果玩家向左滑动,则从游戏界面的最左边开始,对每一列进行判断,如果相邻两个方块的数字相同,则将它们相加,并更新游戏界面。 在每次滑动后,我们还需要生成一个新的方块,可以使用QRandomGenerator在空白位置中随机选择一个并生成一个新的Block对象。 同时,我们还需要判断游戏是否胜利或失败。当玩家得到2048方块时,游戏胜利;当没有空白位置且无法进行任何移动时,游戏失败。 最后,我们还可以添加一些额外功能,如计分和最高分记录等。 总之,实现2048小游戏的代码需要创建游戏界面,实现滑动操作和数字相加,判断胜负等功能。使用Qt框架可以方便地布局界面、处理用户输入和更新界面等操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值