QT编写简单的围棋程序

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QVector>

const int colorWhite = 1;
const int colorBlack = 2;
const int lineMaxNumber = 19;

struct DropPosition
{
    int X;
    int Y;
    int color;
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    int startPointX = 50;
    int startPointY = 50;

    int lineDistance = 20;
    int starRadius = 4;
    int pieceRadius = 18;

    int fontSize = 5;

    int buttonWidth = 80;
    int buttonHeight = 50;
    int buttonDistance = 30;

    bool blackOrWhite = false;
    bool isDisplayNumber = false;

    int number = 1;

    int points[lineMaxNumber][lineMaxNumber];
    int checkPoints[lineMaxNumber][lineMaxNumber];

    QVector<DropPosition> *vector;

    QPushButton *clearPushButton;
    QPushButton *passPushButton;
    QPushButton *undoPushButton;
    QPushButton *displayPushButton;

    QLabel *stepLabel;

    void mousePressEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

    void eat(int xnum, int ynum, int color);
    void clean(int xnum, int ynum, int color);
    int ifthere(int xnum, int ynum, int color);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "qpainter.h"
#include "qlabel.h"
#include "QMouseEvent"
#include "qdebug.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setFixedSize(700,800);
    setWindowTitle("go game");

    startPointX = 50;
    startPointY = 50;

    lineDistance = (this->width()-100)/18;
    starRadius = 4;
    pieceRadius = lineDistance - 2;
    fontSize = lineDistance / 4;

    memset(points,0,sizeof(points));
    vector = new QVector<DropPosition>;

    stepLabel = new QLabel(this);
    stepLabel->setText("Wait for Black to move ...");
    stepLabel->resize(300,50);
    stepLabel->move((this->width()-stepLabel->width())/2,this->height()-stepLabel->height());

    clearPushButton = new QPushButton(this);
    clearPushButton->setText("clear");
    clearPushButton->resize(buttonWidth,buttonHeight);
    clearPushButton->move((this->width()-buttonWidth*4-buttonDistance*3)/2,this->height()-buttonHeight-50);
    connect(clearPushButton,&QPushButton::clicked,this,[=](){
        for(int i=0;i<lineMaxNumber;i++)
        {
            for(int j=0;j<lineMaxNumber;j++)
            {
                points[i][j] = 0;
            }
        }
        number = 1;
        blackOrWhite = false;
        vector->clear();

        QString str = QString("clearPushButton pressed");
        stepLabel->setText(str);

        update();
    });

    undoPushButton = new QPushButton(this);
    undoPushButton->setText("undo");
    undoPushButton->resize(buttonWidth,buttonHeight);
    undoPushButton->move((this->width()-buttonWidth*4-buttonDistance*3)/2+buttonWidth+buttonDistance,this->height()-buttonHeight-50);
    connect(undoPushButton,&QPushButton::clicked,this,[=](){
        if(vector->length() > 0)
        {
            int x = vector->at(vector->length()-1).X;
            int y = vector->at(vector->length()-1).Y;
            if (vector->at(vector->length()-1).color == colorBlack)
            {
                blackOrWhite = false;
            }
            else if (vector->at(vector->length()-1).color == colorWhite)
            {
                blackOrWhite = true;
            }
            if ((x>=0)&&(y>=0))
            {
                points[x][y] = 0;
            }
            number = number - 1;
            vector->remove(vector->length()-1);

            if(vector->length() > 0)
            {
                for(int i=0;i<vector->length();i++)
                {
                    if ((vector->at(i).X >=0) && (vector->at(i).Y >=0))
                    {
                        points[vector->at(i).X][vector->at(i).Y]=vector->at(i).color;
                        eat(vector->at(i).X,vector->at(i).Y,vector->at(i).color);
                    }
                }
            }
        }

        QString str = QString("undoPushButton pressed");
        stepLabel->setText(str);

        update();
    });

    passPushButton = new QPushButton(this);
    passPushButton->setText("pass");
    passPushButton->resize(buttonWidth,buttonHeight);
    passPushButton->move((this->width()-buttonWidth*4-buttonDistance*3)/2+buttonWidth*2+buttonDistance*2,this->height()-buttonHeight-50);
    connect(passPushButton,&QPushButton::clicked,this,[=](){
        DropPosition point;
        point.X = -1;
        point.Y = -1;
        if (blackOrWhite)
        {
           point.color = colorBlack;
           blackOrWhite = false;
        }
        else
        {
            point.color = colorWhite;
            blackOrWhite = true;
        }
        vector->append(point);

        number = number + 1;

        QString str = QString("passPushButton pressed");
        stepLabel->setText(str);
    });

    displayPushButton = new QPushButton(this);
    displayPushButton->setText("display");
    displayPushButton->resize(buttonWidth,buttonHeight);
    displayPushButton->move((this->width()-buttonWidth*4-buttonDistance*3)/2+buttonWidth*3+buttonDistance*3,this->height()-buttonHeight-50);
    connect(displayPushButton,&QPushButton::clicked,this,[=](){
        isDisplayNumber = isDisplayNumber == true ? false : true;

        update();
    });
}

MainWindow::~MainWindow()
{
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    int x = (event->x() - startPointX)/lineDistance;
    if (event->x() - startPointX - lineDistance*x > lineDistance/2)
    {
        x = x+1;
    }
    int y = (event->y() - startPointY)/lineDistance;
    if (event->y() - startPointY - lineDistance*y > lineDistance/2)
    {
        y = y+1;
    }

    if ((x < 0 || x > lineMaxNumber) || (y < 0 || y > lineMaxNumber)) return;
    if (points[x][y]) return;
    if (blackOrWhite)
    {
        points[x][y] = colorWhite;
        number = number + 1;
        blackOrWhite = false;

        eat(x,y,colorWhite);

        DropPosition point;
        point.X = x;
        point.Y = y;
        point.color = colorWhite;
        vector->append(point);

        QString str = QString("White drop position:X:%1,Y:%2").arg(x).arg(y);
        stepLabel->setText(str);
    }
    else
    {
        points[x][y] = colorBlack;
        number = number + 1;
        blackOrWhite = true;

        eat(x,y,colorBlack);

        DropPosition point;
        point.X = x;
        point.Y = y;
        point.color = colorBlack;
        vector->append(point);

        QString str = QString("Black drop position:X:%1,Y:%2").arg(x).arg(y);
        stepLabel->setText(str);
    }
    update();
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //painter.setRenderHint(QPainter::HighQualityAntialiasing);

    QPixmap pix;
    pix.load(":/background.png");
    painter.drawPixmap(startPointX-lineDistance,startPointY-lineDistance,(lineMaxNumber+1)*lineDistance,(lineMaxNumber+1)*lineDistance,pix);

    for(int i=0;i<lineMaxNumber;i++)
    {
        painter.drawLine(startPointX + i*lineDistance, startPointY, startPointX + i*lineDistance, startPointY + (lineMaxNumber-1)*lineDistance);
        painter.drawLine(startPointX, startPointY + i*lineDistance, startPointX + (lineMaxNumber-1)*lineDistance, startPointY + i*lineDistance);

        if ((i == 3) || (i == 9) || (i == 15))
        {
            painter.setBrush(QBrush(Qt::black,Qt::SolidPattern));
            painter.drawEllipse(startPointX + i*lineDistance - starRadius / 2,startPointY + 3*lineDistance - starRadius / 2, starRadius, starRadius);
            painter.drawEllipse(startPointX + i*lineDistance - starRadius / 2,startPointY + 9*lineDistance - starRadius / 2, starRadius, starRadius);
            painter.drawEllipse(startPointX + i*lineDistance - starRadius / 2,startPointY + 15*lineDistance - starRadius / 2, starRadius, starRadius);
        }
    }

    for(int i=0;i<lineMaxNumber;i++)
    {
        for(int j=0;j<lineMaxNumber;j++)
        {
            if (points[i][j])
            {
                if (points[i][j] == colorWhite)
                {
                    //painter.setBrush(QBrush(Qt::white,Qt::SolidPattern));
                    //painter.drawEllipse(startPointX + i*lineDistance - pieceRadius / 2,startPointY + j*lineDistance - pieceRadius / 2, pieceRadius, pieceRadius);
                    pix.load(":/white.png");
                    painter.drawPixmap(startPointX + i*lineDistance - pieceRadius / 2,startPointY + j*lineDistance - pieceRadius / 2, pieceRadius, pieceRadius, pix);
                }
                else if (points[i][j] == colorBlack)
                {
                    //painter.setBrush(QBrush(Qt::black,Qt::SolidPattern));
                    //painter.drawEllipse(startPointX + i*lineDistance - pieceRadius / 2,startPointY + j*lineDistance - pieceRadius / 2, pieceRadius, pieceRadius);
                    pix.load(":/green.png");
                    painter.drawPixmap(startPointX + i*lineDistance - pieceRadius / 2,startPointY + j*lineDistance - pieceRadius / 2, pieceRadius, pieceRadius, pix);
                }
            }
        }
    }

    if (isDisplayNumber)
    {
        for(int i=0;i<vector->length();i++)
        {
            if ((vector->at(i).X>=0) && (vector->at(i).Y>=0) && (points[vector->at(i).X][vector->at(i).Y]))
            {
                QFont font("Helvetica", fontSize, QFont::Bold, true);
                painter.setFont(font);
                if (points[vector->at(i).X][vector->at(i).Y] == colorBlack)
                {
                    painter.setPen(QColor(Qt::white));
                }
                else if (points[vector->at(i).X][vector->at(i).Y] == colorWhite)
                {
                    painter.setPen(QColor(Qt::black));
                }
                QRectF rect(startPointX + vector->at(i).X*lineDistance - pieceRadius / 2,startPointY + vector->at(i).Y*lineDistance - pieceRadius / 4, pieceRadius, pieceRadius);
                painter.drawText(rect,Qt::AlignHCenter,QString::number(i+1));
            }
        }
    }
}

//吃棋总逻辑
void MainWindow::eat(int X, int Y, int color)
{
    if (X - 1 >= 0)
    {
        if ((points[X - 1][Y] != color)&&(points[X-1][Y]))
        {
            memset(checkPoints, 0, lineMaxNumber * lineMaxNumber * sizeof(int));
            if (!ifthere((X - 1), Y, points[X - 1][Y]))
            {
                clean((X - 1), Y, points[X - 1][Y]);
            }
        }
    }

    if (X + 1 < lineMaxNumber)
    {
        if ((points[X+1][Y] != color) && (points[X+1][Y]))
        {
            memset(checkPoints, 0, lineMaxNumber * lineMaxNumber * sizeof(int));
            if (!ifthere((X+1), Y, points[X +1][Y]))
            {
                clean((X+1), Y, points[X+1][Y]);
            }
        }
    }

    if (Y + 1 < lineMaxNumber)
    {
        if ((points[X][Y+1] != color) && (points[X][Y+1]))
        {
            memset(checkPoints, 0, lineMaxNumber * lineMaxNumber * sizeof(int));
            if (!ifthere(X, (Y+1), points[X][Y+1]))
            {
                clean(X, (Y+1), points[X][Y+1]);
            }
        }
    }

    if (Y - 1 >= 0)
    {
        if ((points[X][Y-1] != color) && (points[X][Y-1]))
        {
            memset(checkPoints, 0, lineMaxNumber * lineMaxNumber * sizeof(int));
            if (!ifthere(X, (Y-1), points[X][Y-1]))
            {
                clean(X, (Y-1), points[X][Y-1]);
            }
        }
    }
}

//清除逻辑
void MainWindow::clean(int X, int Y, int color)
{
    points[X][Y] = 0;

    if (X - 1 >= 0)
    {
        if (points[X - 1][Y] == color)
            clean((X - 1), Y, color);
    }

    if (X + 1 < lineMaxNumber)
    {
        if (points[X+1][Y] == color)
            clean((X+1), Y, color);
    }

    if (Y - 1 >= 0)
    {
        if (points[X][Y-1] == color)
            clean(X,(Y-1), color);
    }

    if (Y + 1 < lineMaxNumber)
    {
        if (points[X][Y+1] == color)
            clean(X, (Y+1), color);
    }
    return;
}

//判断棋是否存活
int MainWindow::ifthere(int X, int Y, int color)
{
    checkPoints[X][Y] = 1;
    int ret = 0;

    if (X + 1 < lineMaxNumber)
    {
        if (points[X+1][Y] == 0)
        {
            return 1;
        }

        if (points[X+1][Y] == color)
        {
            if (!checkPoints[X+1][Y])
                ret = ret + ifthere((X+1), Y, color);
        }
    }

    if (X - 1 >= 0)
    {
        if (points[X-1][Y] == 0)
        {
            return 1;
        }

        if (points[X-1][Y] == color)
        {
            if (!checkPoints[X-1][Y])
                ret = ret + ifthere((X-1), Y, color);
        }
    }

    if (Y + 1 <lineMaxNumber)
    {
        if (points[X][Y+1] == 0)
        {
            return 1;
        }

        if (points[X][Y+1] == color)
        {
            if (!checkPoints[X][Y+1])
                ret = ret + ifthere(X, (Y+1), color);
        }
    }

    if (Y - 1 >= 0)
    {
        if (points[X][Y-1] == 0)
        {
            return 1;
        }

        if (points[X][Y-1] == color)
        {
            if (!checkPoints[X][Y-1])
                ret = ret + ifthere(X, (Y-1), color);
        }
    }

    if (ret > 0)
        return 1;
    else
        return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt是一款跨平台的C++应用程序开发框架,它提供了丰富的类库和工具,可以用来开发各种类型的应用程序,包括串口通信程序Qt提供了QSerialPort类,可以方便地进行串口通信的编程。下面是一个简单的例子,演示了如何利用Qt编写串口通信程序: ```cpp #include <QtSerialPort/QSerialPort> #include <QtSerialPort/QSerialPortInfo> #include <QtCore/QString> #include <QtCore/QDebug> int main() { // 扫描可用的串口 QList<QSerialPortInfo> serialPortInfos = QSerialPortInfo::availablePorts(); foreach (const QSerialPortInfo &serialPortInfo, serialPortInfos) { qDebug() << "Port: " << serialPortInfo.portName() << "Description: " << serialPortInfo.description(); } // 创建串口对象 QSerialPort serial; // 设置串口参数 serial.setPortName("COM1"); // 设置串口名字 serial.setBaudRate(QSerialPort::Baud115200); // 设置波特率 serial.setDataBits(QSerialPort::Data8); // 设置数据位 serial.setParity(QSerialPort::NoParity); // 设置校验位 serial.setStopBits(QSerialPort::OneStop); // 设置停止位 serial.setFlowControl(QSerialPort::NoFlowControl); // 设置流控制 // 打开串口 if (serial.open(QIODevice::ReadWrite)) { qDebug() << "Serial port opened!"; // 读取数据 QByteArray data = serial.readAll(); qDebug() << "Read data: " << data; // 写入数据 serial.write("Hello, Serial!"); // 关闭串口 serial.close(); } else { qDebug() << "Failed to open serial port!"; } return 0; } ``` 以上代码演示了如何使用Qt进行串口通信。首先需要扫描可用的串口,然后创建一个QSerialPort对象,并设置串口参数。接下来,打开串口,并可以通过read()函数读取数据,通过write()函数写入数据。最后,关闭串口。 使用Qt编写串口通信程序,可以方便地实现串口相关的功能,并且跨平台性强,可以在不同的操作系统上运行。希望对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值