Browse Source

first commit

zhuyijun 1 year ago
commit
dd01e2599b
8 changed files with 388 additions and 0 deletions
  1. 73 0
      .gitignore
  2. 26 0
      GoBang.pro
  3. 11 0
      chessitem.cpp
  4. 21 0
      chessitem.h
  5. 15 0
      main.cpp
  6. 166 0
      mainwindow.cpp
  7. 54 0
      mainwindow.h
  8. 22 0
      mainwindow.ui

+ 73 - 0
.gitignore

@@ -0,0 +1,73 @@
+# This file is used to ignore files which are generated
+# ----------------------------------------------------------------------------
+
+*~
+*.autosave
+*.a
+*.core
+*.moc
+*.o
+*.obj
+*.orig
+*.rej
+*.so
+*.so.*
+*_pch.h.cpp
+*_resource.rc
+*.qm
+.#*
+*.*#
+core
+!core/
+tags
+.DS_Store
+.directory
+*.debug
+Makefile*
+*.prl
+*.app
+moc_*.cpp
+ui_*.h
+qrc_*.cpp
+Thumbs.db
+*.res
+*.rc
+/.qmake.cache
+/.qmake.stash
+
+# qtcreator generated files
+*.pro.user*
+
+# xemacs temporary files
+*.flc
+
+# Vim temporary files
+.*.swp
+
+# Visual Studio generated files
+*.ib_pdb_index
+*.idb
+*.ilk
+*.pdb
+*.sln
+*.suo
+*.vcproj
+*vcproj.*.*.user
+*.ncb
+*.sdf
+*.opensdf
+*.vcxproj
+*vcxproj.*
+
+# MinGW generated files
+*.Debug
+*.Release
+
+# Python byte code
+*.pyc
+
+# Binaries
+# --------
+*.dll
+*.exe
+

+ 26 - 0
GoBang.pro

@@ -0,0 +1,26 @@
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+CONFIG += c++11
+
+# You can make your code fail to compile if it uses deprecated APIs.
+# In order to do so, uncomment the following line.
+#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
+
+SOURCES += \
+    chessitem.cpp \
+    main.cpp \
+    mainwindow.cpp
+
+HEADERS += \
+    chessitem.h \
+    mainwindow.h
+
+FORMS += \
+    mainwindow.ui
+
+# Default rules for deployment.
+qnx: target.path = /tmp/$${TARGET}/bin
+else: unix:!android: target.path = /opt/$${TARGET}/bin
+!isEmpty(target.path): INSTALLS += target

+ 11 - 0
chessitem.cpp

@@ -0,0 +1,11 @@
+#include "chessitem.h"
+
+ChessItem::ChessItem()
+{
+
+}
+ChessItem::ChessItem(QPoint point,bool isBlack)
+{
+    _pt = point;
+    _black = isBlack;
+}

+ 21 - 0
chessitem.h

@@ -0,0 +1,21 @@
+#ifndef CHESSITEM_H
+#define CHESSITEM_H
+#include<QPoint>
+
+class ChessItem
+{
+public:
+    ChessItem(void);
+    ChessItem(QPoint point,bool isBlack);
+    bool operator==(const ChessItem &t1) const
+    {
+        return((_pt == t1._pt) && (_black ==t1._black));
+    }
+    //棋子的位置
+    QPoint _pt;
+    //棋子的颜色
+    bool _black;
+
+};
+
+#endif // CHESSITEM_H

+ 15 - 0
main.cpp

@@ -0,0 +1,15 @@
+#include "mainwindow.h"
+
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    //创建一个app对象
+    QApplication a(argc, argv);
+    //实例化一个MainWindow界面
+    MainWindow w;
+    //显示界面
+    w.show();
+    //exec 等待消息循环
+    return a.exec();
+}

+ 166 - 0
mainwindow.cpp

@@ -0,0 +1,166 @@
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+#include<QtDebug>
+MainWindow::MainWindow(QWidget *parent)
+    : QMainWindow(parent)
+    , ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+    InitUI();
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+
+void MainWindow::InitUI()
+{
+    //定义期盼的大小
+    this->resize((::CHESS_C + 1) * ::MAX_X, (::CHESS_R + 1) * ::MAX_Y);
+    //锁定边框大小
+    this->setMaximumSize((::CHESS_C + 1) * ::MAX_X, (::CHESS_R + 1) * ::MAX_Y);
+    this->setMinimumSize((::CHESS_C + 1) * ::MAX_X, (::CHESS_R + 1) * ::MAX_Y);
+    this->setWindowTitle("五子棋小游戏-逝水无痕");
+    b_black = false;
+}
+
+void MainWindow::paintEvent(QPaintEvent *event)
+{
+    DraChessBoard();
+    DrawHandChess();
+    DrawChessItem();
+    //强制更新界面
+    update();
+}
+
+//画棋盘
+void MainWindow::DraChessBoard()
+{
+    //绘图工具
+    QPainter painter(this);
+    //防止棋盘变形
+    painter.setRenderHint(QPainter::Antialiasing,true);
+    //给边框线颜色和大小
+    painter.setPen(QPen(QColor(Qt::black),2));
+    //给棋盘格子颜色
+    painter.setBrush(Qt::yellow);
+    //便利回执期盼的所有的格子
+    for (int i=0;i<::CHESS_C; i++ ) {
+        for (int j=0;j<::CHESS_R; j++ ) {
+            //绘制矩形(棋盘的格子)
+            painter.drawRect((i+0.5) * ::MAX_X,(j+0.5)* ::MAX_Y,::MAX_X,::MAX_Y);
+        }
+    }
+}
+//画鼠标上的棋子
+void MainWindow::DrawHandChess()
+{
+    QPainter painter(this);
+    //给棋子边框绘制透明
+    painter.setPen(QPen(QColor(Qt::transparent)));
+    if(b_black)
+    {
+        painter.setBrush(Qt::black);
+    }else
+    {
+        painter.setBrush(Qt::white);
+    }
+    //绘制
+    painter.drawEllipse(mapFromGlobal(QCursor::pos()),::MAX_X/2,MAX_Y/2);
+}
+//处理棋子位置
+void MainWindow::DrawChessAtPoint(QPainter &painter,QPoint &point)
+{
+    //获取棋子的摆放位置
+    QPoint ptCenter((point.x()+0.5)*::MAX_X,(point.y()+0.5)*::MAX_Y);
+    painter.drawEllipse(ptCenter,::MAX_X/2,::MAX_Y/2);
+}
+//判断五子是否连接
+int MainWindow::CountNearItem(ChessItem item,QPoint pt)
+{
+    int nCount = 0;
+    item._pt+=pt;
+    bool flag = true;
+    while(flag)
+    {
+        int m = nCount;
+        for (int i =0;i < p_ChessItem.size() ;i++ )
+        {
+            if(p_ChessItem[i] == item){
+                nCount++;
+                item._pt += pt;
+            }
+        }
+        if(m == nCount){
+            flag = false;
+        }
+
+    }
+    return nCount;
+}
+//点击事件
+void MainWindow::mousePressEvent(QMouseEvent *event)
+{
+    //获取鼠标点击的位置d
+    qDebug()<<event->pos()<<"\n";
+    //定义点位
+    QPoint pt;
+    pt.setX((event->pos().x())/::MAX_X);
+    pt.setY((event->pos().y())/::MAX_Y);
+    //判断棋子是否存在
+    for (int i =0;i < p_ChessItem.size() ;i++ )
+    {
+        //获取当前棋子样式赋值
+         ChessItem item = p_ChessItem[i];
+        if(item._pt == pt){
+            return;
+        }
+    }
+    //不存在则绘制并判断五子是否连接
+    ChessItem item(pt,b_black);
+    p_ChessItem.append(item);
+    //统计四个点位是否连接
+    int nLeft       = CountNearItem(item,QPoint(-1,0));
+    int nLeftUp     = CountNearItem(item,QPoint(-1,-1));
+    int nLeftDown   = CountNearItem(item,QPoint(-1,1));
+    int nUp         = CountNearItem(item,QPoint(0,-1));
+    int nDown       = CountNearItem(item,QPoint(0,1));
+    int nRight      = CountNearItem(item,QPoint(1,0));
+    int nRightUp    = CountNearItem(item,QPoint(1,-1));
+    int nRightDown  = CountNearItem(item,QPoint(1,1));
+    if(nLeft+nRight >= 4 ||
+        nLeftUp + nRightDown >= 4 ||
+        nUp +nDown >= 4 ||
+        nRightUp + nLeftDown >= 4)
+    {
+        QString str = b_black?"Black win":"Wihte win";
+        QMessageBox::information(NULL,"GAME OVER",str,QMessageBox::Yes);
+        p_ChessItem.clear();
+        return;
+    }
+    //设置颜色取反
+    b_black = !b_black;
+}
+void MainWindow::DrawChessItem()
+{
+   QPainter painter(this);
+   //给棋子边框绘制透明
+   painter.setPen(QPen(QColor(Qt::transparent)));
+//   painter.setBrush(Qt::black);
+//   //绘制
+//   painter.drawEllipse(65,65,::MAX_X,::MAX_Y);
+   for (int i =0;i < p_ChessItem.size() ;i++ )
+   {
+       //获取当前棋子样式赋值
+        ChessItem item = p_ChessItem[i];
+        if(item._black)
+        {
+            painter.setBrush(Qt::black);
+        }else
+        {
+            painter.setBrush(Qt::white);
+        }
+        DrawChessAtPoint(painter,item._pt);
+   }
+}

+ 54 - 0
mainwindow.h

@@ -0,0 +1,54 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+#include<QPainter>
+#include<QPaintEvent>
+#include<QMouseEvent>
+#include<QVector>
+#include<QMessageBox>
+#include"chessitem.h"
+/*
+ QT的2D绘画
+ */
+const int CHESS_R = 15;
+const int CHESS_C = 15;
+const int MAX_X = 50;
+const int MAX_Y = 50;
+
+
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    MainWindow(QWidget *parent = nullptr);
+    ~MainWindow();
+
+protected:
+    //实时绘制界面
+    void paintEvent(QPaintEvent *event);
+    //点击事件
+    void mousePressEvent(QMouseEvent *event);
+private:
+    Ui::MainWindow *ui;
+    bool b_black;
+    //定义棋子个数
+    QVector<ChessItem> p_ChessItem;
+    //初始化界面
+    void InitUI();
+    //画棋盘
+    void DraChessBoard();
+    //画鼠标上的棋子
+    void DrawHandChess();
+    //绘制棋盘上的棋子
+    void DrawChessItem();
+    void DrawChessAtPoint(QPainter &painter,QPoint &point);
+    //判断五子是否连接
+    int CountNearItem(ChessItem item,QPoint pt);
+};
+#endif // MAINWINDOW_H

+ 22 - 0
mainwindow.ui

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>800</width>
+    <height>600</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralwidget"/>
+  <widget class="QMenuBar" name="menubar"/>
+  <widget class="QStatusBar" name="statusbar"/>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>