头文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPainter>
#include <QEvent>
#include <QMouseEvent>
#include <QMessageBox>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *event);
private:
bool isBlack = true;
int panWidth = 40;
int rWidth = 15;
int startX = 40;
int startY = 40;
int qizis[11][11];
void xiaQiZi(int col, int row);//下棋子
bool isOk(QList<int> values, int value);//是否赢了
};
#endif // WIDGET_H
cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
for(int i = 0; i < 11;i++)
{
for(int j =0 ; j < 11; j++)
{
qizis[i][j] = 0;
}
}
this->resize(480,480);
}
Widget::~Widget()
{
}
//
void Widget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.save();
QColor color1 = QColor(255,255,255);
QColor color2 = QColor(0,0,0);
QColor color3 = QColor(30,160,230);
QColor color4 = QColor(200,200,200);
//背景方框
painter.setPen(color3);
painter.setBrush(color4);
for(int i =0; i < 11; i++)
{
painter.drawLine(startX+i * panWidth,startY, startX + i*panWidth, startY + 10*panWidth);
painter.drawLine(startX,startY+i * panWidth, startX + 10*panWidth, startY +i * panWidth);
}
//绘制棋子
for(int i = 0; i < 11;i++)
{
for(int j =0 ; j < 11; j++)
{
QPoint center;
center.setX( startX + i * panWidth);
center.setY( startY + j * panWidth);
if(qizis[i][j] == 1)
{
painter.setPen(color1);
painter.setBrush(color1);
painter.drawEllipse(center, rWidth, rWidth);
}
else if(qizis[i][j] == 2)
{
painter.setPen(color2);
painter.setBrush(color2);
painter.drawEllipse(center, rWidth, rWidth);
}
}
}
painter.restore();
}
//
void Widget::mousePressEvent(QMouseEvent *event)
{
QPoint pos = event->pos();
for(int i = 0; i < 11;i++)
{
for(int j =0 ; j < 11; j++)
{
QRect rect = QRect(startX + i * panWidth - rWidth, startY + j * panWidth - rWidth, 2* rWidth, 2* rWidth);
if(rect.contains(pos))
{
if(qizis[i][j] == 0)
{
xiaQiZi(i,j);//下棋子
}
break;
}
}
}
}
//下棋子
void Widget::xiaQiZi(int col, int row)
{
int value = 0;
if(isBlack)
value = 2;
else
value = 1;
isBlack = !isBlack;
qizis[col][row] = value;
this->update();
QList<int> values;
for(int i =0 ; i < 9; i++)
{
int x = col - 4 + i;
int y = row;
if(x >= 0 && x < 11)
{
values << qizis[x][y];
}
}
if(isOk(values, value))
return;
values.clear();
for(int i =0 ; i < 9; i++)
{
int x = col ;
int y = row- 4 + i;
if(x >= 0 && x < 11)
{
values << qizis[x][y];
}
}
if(isOk(values, value))
return;
values.clear();
for(int i =0 ; i < 9; i++)
{
int x = col -4 +i ;
int y = row- 4 + i;
if(x >= 0 && x < 11)
{
values << qizis[x][y];
}
}
if(isOk(values, value))
return;
values.clear();
for(int i =0 ; i < 9; i++)
{
int x = col + 4 - i ;
int y = row- 4 + i;
if(x >= 0 && x < 11)
{
values << qizis[x][y];
}
}
if(isOk(values, value))
return;
}
//是否赢了
bool Widget::isOk(QList<int> values,int value)
{
bool start = false;
int count = 0;
for(int i =0 ; i < values.count();i++)
{
if(!start)
{
if(values[i] == value)
{
count=1;
start = true;
}
}
else
{
if(values[i] == value)
{
count++;
if(count >=5)
{
QMessageBox::information(this,"结束",QString(isBlack?QString("白棋子"):QString("黑棋子"))+"赢了!");
isBlack = true;
for(int i = 0; i < 11;i++)
{
for(int j =0 ; j < 11; j++)
{
qizis[i][j] = 0;
}
}
this->update();
return true;
}
}
else
{
return false;
}
}
}
return false;
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}