原理很简单,就是点击鼠标进行下子,电脑与你进行博弈
没事做做还是挺有意思的。
源码在下面:
mylabel.h
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QWidget>
#include <QLabel>
class myLabel : public QLabel
{
Q_OBJECT
public:
explicit myLabel(QWidget *parent = nullptr);
~myLabel();
protected:
void mousePressEvent(QMouseEvent *event);
signals:
void sSendOk();
};
#endif // MYLABEL_H
mylabel.cpp
#include "mylabel.h"
#include <QMouseEvent>
#include <qdebug.h>
#include <QMessageBox>
myLabel::myLabel(QWidget *parent) : QLabel(parent)
{
}
myLabel::~myLabel()
{
}
void myLabel::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
if(this->pixmap()==nullptr)
{
this->setPixmap(QPixmap(":/Img/O.png"));
this->setObjectName("O");
emit sSendOk();
}
else
{
QMessageBox msgBox;
msgBox.setText("There are already chess pieces.");
msgBox.exec();
}
}
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include "mylabel.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected slots:
void DownChess();
private:
myLabel *ChessLabel[9];
int wining[36][3]=
{
{0,1,2},
{1,2,0},
{0,2,1},
{3,4,5},
{3,5,4},
{4,5,3},
{6,7,8},
{6,8,7},
{7,8,6},
{0,3,6},
{0,6,3},
{3,6,0},
{1,4,7},
{1,7,4},
{4,7,1},
{2,5,8},
{2,8,5},
{5,8,2},
{0,4,8},
{0,8,4},
{4,8,0},
{2,4,6},
{2,6,4},
{4,6,2}
};
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QVBoxLayout>
#include <qdebug.h>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
for(int i=0;i<9;++i)
{
ChessLabel[i]=new myLabel(this);
ChessLabel[i]->setFixedSize(100,100);
ChessLabel[i]->setFrameStyle(QFrame::Panel | QFrame::Raised);
//ChessLabel[i]->setPixmap(QPixmap(":/Img/O.png"));
ChessLabel[i]->setAlignment(Qt::AlignCenter);
connect(ChessLabel[i],&myLabel::sSendOk,this,&Widget::DownChess);
}
QGridLayout *mainLayout=new QGridLayout(this);
mainLayout->addWidget(ChessLabel[0],0,0);
mainLayout->addWidget(ChessLabel[1],0,1);
mainLayout->addWidget(ChessLabel[2],0,2);
mainLayout->addWidget(ChessLabel[3],1,0);
mainLayout->addWidget(ChessLabel[4],1,1);
mainLayout->addWidget(ChessLabel[5],1,2);
mainLayout->addWidget(ChessLabel[6],2,0);
mainLayout->addWidget(ChessLabel[7],2,1);
mainLayout->addWidget(ChessLabel[8],2,2);
mainLayout->setMargin(0);
mainLayout->setSpacing(0);
this->setLayout(mainLayout);
}
Widget::~Widget()
{
}
void Widget::DownChess()
{
bool isCheck=false;
for(int i=0;i<36;++i)
{
int x=wining[i][0];
int y=wining[i][1];
int z=wining[i][2];
if(ChessLabel[x]->objectName()=="X"&&ChessLabel[y]->objectName()=="X")
{
if(ChessLabel[z]->objectName()=="")
{
ChessLabel[z]->setPixmap(QPixmap(":/Img/X.png"));
ChessLabel[z]->setObjectName("X");
isCheck=true;
break;
}
}
if(ChessLabel[x]->objectName()=="O"&&ChessLabel[y]->objectName()=="O")
{
if(ChessLabel[z]->objectName()=="")
{
ChessLabel[z]->setPixmap(QPixmap(":/Img/X.png"));
ChessLabel[z]->setObjectName("X");
isCheck=true;
break;
}
}
}
if(!isCheck)
{
for(int i=0;i<9;++i)
{
if(ChessLabel[i]->objectName()=="")
{
ChessLabel[i]->setPixmap(QPixmap(":/Img/X.png"));
ChessLabel[i]->setObjectName("X");
break;
}
}
}
}
有需要的自取
每日一图