2019.3.2 Qt学习---简单五子棋

主要用的东西:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
QPainter
QPen
QMessageBox
黑白棋子的图片是自己随便用win自带的画图画的,拿美图秀秀改成透明背景。。。

写代码时发现的一些问题:
1.隐藏最大化按钮和固定窗口大小

    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);      //禁止最大化
    setFixedSize(800, 800);                                           //固定大小

2.drawPixmap的图片路径是在Debug中
进入下一目录需用两条\
QPixmap("pic\\black.png")
3.画线画图什么的不能写在自己写的函数中 会报错 只能写在paintEvent

代码:
mywidget.h代码:

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();
    void Init();
    int check();
protected:
    void mousePressEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);

private:
    Ui::MyWidget *ui;
    int step;           // 下棋步数
    int turn;           // 0表示黑子下 1表示白子下
    int w,h;            // 每一小格的宽度和高度
    int chessX,chessY;  // 棋子坐标所对应的棋盘坐标
    int state[N][N];    // -1表示没有棋子 0表示黑子 1表示白子
};

mywidget.cpp部分代码:

int MyWidget::check()
{
    if(step == N*N)
        return 2;                               //平局

    int num = 0;                                //检查chessY那一横行
    for(int i = 0;i < N && num < 5;i++)
        if(state[i][chessY] == turn)
            num++;
        else num = 0;
    if(num >= 5)
        return turn;

    num = 0;                                    //检查chessX那一竖行
    for(int i = 0;i < N && num < 5;i++)
        if(state[chessX][i] == turn)
            num++;
        else num = 0;
    if(num >= 5)
        return turn;

    num = 0;                                    //检查次对角线
    int start,end,sum,sub;
    sum = chessX + chessY;
    sub = chessX - chessY;
    if(sum > N-1)
    {
        start = sum - N + 1;
        end = N;
    }
    else
    {
        start = 0;
        end = sum + 1;
    }
    for(int i = start;i < end && num < 5;i++)
        if(state[i][sum-i] == turn)
            num++;
        else num = 0;
    if(num >= 5)
        return turn;

    num = 0;                                    //检查主对角线
    if(sub > 0)
    {
        start = sub;
        end = N;
    }
    else
    {
        start = 0;
        end = N - sub;
    }
    for(int i = start;i < end && num < 5;i++)
        if(state[i][i-sub] == turn)
            num++;
        else num = 0;
    if(num >= 5)
        return turn;

    return -1;                                  //没有胜负
}

void MyWidget::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    QPen pen;
    pen.setWidth(4);
    p.setPen(pen);

    if( turn == 0)                                                  //提示下棋颜色
    {
        this->setWindowTitle("Turn to Black");
        p.drawPixmap(this->width()/2-0.5*w+8, 8, 0.8*w, 0.8*h, QPixmap("pic\\black.png"));
    }
    else
    {
        this->setWindowTitle("Turn to White");
        p.drawPixmap(this->width()/2-0.5*w+8, 8, 0.8*w, 0.8*h, QPixmap("pic\\white.png"));
    }

    for(int i = 0;i <= N;i++)
        p.drawLine(w, h+i*h, w+N*w, h+i*h);                         //画横线
    for(int i = 0;i <= N;i++)
        p.drawLine(w+i*w, h, w+i*w, h+N*h);                         //画竖线

    for(int i = 0;i < N;i++)
        for(int j = 0;j < N;j++)                                    //画棋子
            if(state[i][j] != -1)
            {
                if(state[i][j] == 0)
                    p.drawPixmap(w+i*w, h+j*h, w, h, QPixmap("pic\\black.png"));
                else p.drawPixmap(w+i*w, h+j*h, w, h, QPixmap("pic\\white.png"));
            }

}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
    int x = event->x();
    int y = event->y();
    if(x < w || x > (N+1)*w || y < h || y > (N+1)*h)
        return;

    chessX = (x-w)/w;
    chessY = (y-h)/h;
    if(state[chessX][chessY] != -1)
        return;

    state[chessX][chessY] = turn;
    step++;

    qDebug() << chessX << chessY << turn << step;

    int flag = check();                                             //检查胜负
    if(flag == 0)
    {
        QMessageBox::information(this,"Tip","Black Win!!!");
        Init();
        return;
    }
    else if(flag == 1)
    {
        QMessageBox::information(this,"Tip","White Win!!!");
        Init();
        return;
    }
    else if(flag == 2)
    {
        QMessageBox::information(this,"Tip","Draw!!!");
        Init();
        return;
    }

    turn = (turn+1)%2;
    update();
}

效果图:
Alt
Alt

立个Flag:
以后有水平了写一个AI的五子棋~~~~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值