傻逼
This commit is contained in:
parent
87df93e775
commit
4b9c9d4b9c
11 changed files with 633 additions and 0 deletions
24
SoftwareDesign/Lab5/lab5_1_painter/lab5_1_painter.pro
Normal file
24
SoftwareDesign/Lab5/lab5_1_painter/lab5_1_painter.pro
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
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 += \
|
||||||
|
main.cpp \
|
||||||
|
mainwindow.cpp
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
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
SoftwareDesign/Lab5/lab5_1_painter/main.cpp
Normal file
11
SoftwareDesign/Lab5/lab5_1_painter/main.cpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include "mainwindow.h"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
MainWindow w;
|
||||||
|
w.show();
|
||||||
|
return a.exec();
|
||||||
|
}
|
71
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.cpp
Normal file
71
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.cpp
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#include "mainwindow.h"
|
||||||
|
#include "ui_mainwindow.h"
|
||||||
|
#include <QColorDialog>
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QDialog>
|
||||||
|
#include <QFileDialog>
|
||||||
|
#include <QFontDialog>
|
||||||
|
#include <QInputDialog>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QMouseEvent>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QProgressDialog>
|
||||||
|
bool enable = false;
|
||||||
|
bool drawing = false;
|
||||||
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
|
: QMainWindow(parent), ui(new Ui::MainWindow) {
|
||||||
|
ui->setupUi(this);
|
||||||
|
this->setProperty("canMove", false);
|
||||||
|
resize(600, 500); //窗口大小设置为600*500
|
||||||
|
pix = QPixmap(600, 500);
|
||||||
|
pix.fill(Qt::white);
|
||||||
|
}
|
||||||
|
|
||||||
|
MainWindow::~MainWindow() { delete ui; }
|
||||||
|
|
||||||
|
void MainWindow::on_styCor_triggered() //边框颜色
|
||||||
|
{
|
||||||
|
QPainter pp(&pix); // 根据鼠标指针前后两个位置就行绘制直线
|
||||||
|
pp.drawLine(
|
||||||
|
lastPoint,
|
||||||
|
endPoint); // 让前一个坐标值等于后一个坐标值,这样就能实现画出连续的线
|
||||||
|
lastPoint = endPoint;
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.drawPixmap(0, 0, pix);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::paintEvent(QPaintEvent *event) {
|
||||||
|
if (enable)
|
||||||
|
switch (drawStatus) {
|
||||||
|
case line:
|
||||||
|
QPainter pp(&pix); // 根据鼠标指针前后两个位置就行绘制直线
|
||||||
|
pp.drawLine(
|
||||||
|
lastPoint,
|
||||||
|
endPoint); // 让前一个坐标值等于后一个坐标值,这样就能实现画出连续的线
|
||||||
|
enable = false;
|
||||||
|
}
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.drawPixmap(0, 0, pix);
|
||||||
|
|
||||||
|
drawStatus = idle;
|
||||||
|
}
|
||||||
|
void MainWindow::drawLineTriger() { drawStatus = line; }
|
||||||
|
|
||||||
|
void MainWindow::on_drawLine_triggered() {
|
||||||
|
enable = true;
|
||||||
|
drawStatus = line;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::mousePressEvent(QMouseEvent *event) {
|
||||||
|
|
||||||
|
if (event->button() == Qt::LeftButton) //鼠标左键按下
|
||||||
|
if (drawing) {
|
||||||
|
endPoint = event->pos();
|
||||||
|
drawing = false;
|
||||||
|
enable = true;
|
||||||
|
update();
|
||||||
|
} else {
|
||||||
|
drawing = true;
|
||||||
|
lastPoint = event->pos();
|
||||||
|
}
|
||||||
|
}
|
40
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.h
Normal file
40
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.h
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#ifndef MAINWINDOW_H
|
||||||
|
#define MAINWINDOW_H
|
||||||
|
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QPainter>
|
||||||
|
enum DS { idle, line, circle, rec, tri, set, clr };
|
||||||
|
|
||||||
|
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 drawLineTriger();
|
||||||
|
int num1, num2, num3, num4;
|
||||||
|
void mousePressEvent(QMouseEvent *);
|
||||||
|
// void mouseMoveEvent(QMouseEvent *);
|
||||||
|
// void mouseReleaseEvent(QMouseEvent *);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void on_styCor_triggered();
|
||||||
|
void on_drawLine_triggered();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QPixmap pix;
|
||||||
|
QPoint lastPoint;
|
||||||
|
QPoint endPoint;
|
||||||
|
DS drawStatus = idle;
|
||||||
|
Ui::MainWindow *ui;
|
||||||
|
};
|
||||||
|
#endif // MAINWINDOW_H
|
94
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.ui
Normal file
94
SoftwareDesign/Lab5/lab5_1_painter/mainwindow.ui
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
<?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="QStatusBar" name="statusbar"/>
|
||||||
|
<widget class="QMenuBar" name="menubar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>800</width>
|
||||||
|
<height>30</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<widget class="QMenu" name="menu">
|
||||||
|
<property name="title">
|
||||||
|
<string>绘制</string>
|
||||||
|
</property>
|
||||||
|
<addaction name="drawLine"/>
|
||||||
|
<addaction name="drawCir"/>
|
||||||
|
<addaction name="drawRec"/>
|
||||||
|
<addaction name="drawClear"/>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenu" name="menu_2">
|
||||||
|
<property name="title">
|
||||||
|
<string>设置</string>
|
||||||
|
</property>
|
||||||
|
<addaction name="styCor"/>
|
||||||
|
<addaction name="StyBon"/>
|
||||||
|
<addaction name="styOut"/>
|
||||||
|
<addaction name="styBcor"/>
|
||||||
|
</widget>
|
||||||
|
<addaction name="menu"/>
|
||||||
|
<addaction name="menu_2"/>
|
||||||
|
</widget>
|
||||||
|
<action name="drawLine">
|
||||||
|
<property name="text">
|
||||||
|
<string>直线</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="drawCir">
|
||||||
|
<property name="text">
|
||||||
|
<string>圆形</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="drawRec">
|
||||||
|
<property name="text">
|
||||||
|
<string>矩形</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="drawClear">
|
||||||
|
<property name="text">
|
||||||
|
<string>晴空</string>
|
||||||
|
</property>
|
||||||
|
<property name="toolTip">
|
||||||
|
<string>清空</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="styCor">
|
||||||
|
<property name="text">
|
||||||
|
<string>边框颜色</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="StyBon">
|
||||||
|
<property name="text">
|
||||||
|
<string>边框粗细</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="styOut">
|
||||||
|
<property name="text">
|
||||||
|
<string>边框样式</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
<action name="styBcor">
|
||||||
|
<property name="text">
|
||||||
|
<string>背景颜色</string>
|
||||||
|
</property>
|
||||||
|
</action>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
236
SoftwareDesign/Lab5/lab5_2_gomoku/boardwidget.cpp
Normal file
236
SoftwareDesign/Lab5/lab5_2_gomoku/boardwidget.cpp
Normal file
|
@ -0,0 +1,236 @@
|
||||||
|
#include "boardwidget.h"
|
||||||
|
#include <QMouseEvent>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
/*类静态数据成员定义*/
|
||||||
|
const QSize BoardWidget::WIDGET_SIZE(430, 430);
|
||||||
|
const QSize BoardWidget::CELL_SIZE(25, 25);
|
||||||
|
const QPoint BoardWidget::START_POS(40, 40);
|
||||||
|
const QPoint BoardWidget::ROW_NUM_START(15, 45);
|
||||||
|
const QPoint BoardWidget::CLU_NUM_START(39, 25);
|
||||||
|
const int BoardWidget::BOARD_WIDTH;
|
||||||
|
const int BoardWidget::BOARD_HEIGHT;
|
||||||
|
const int BoardWidget::NO_PIECE;
|
||||||
|
const int BoardWidget::WHITE_PIECE;
|
||||||
|
const int BoardWidget::BLACK_PIECE;
|
||||||
|
const bool BoardWidget::WHITE_PLAYER;
|
||||||
|
const bool BoardWidget::BLACK_PLAYER;
|
||||||
|
|
||||||
|
BoardWidget::BoardWidget(QWidget *parent) : QWidget(parent) {
|
||||||
|
setFixedSize(WIDGET_SIZE);
|
||||||
|
setMouseTracking(true);
|
||||||
|
|
||||||
|
initBoard();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::paintEvent(QPaintEvent *event) {
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.fillRect(0, 0, width(), height(), Qt::gray); //背景颜色
|
||||||
|
|
||||||
|
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||||
|
painter.drawText(CLU_NUM_START + QPoint(i * CELL_SIZE.width(), 0),
|
||||||
|
QString::number(i + 1));
|
||||||
|
}
|
||||||
|
for (int i = 0; i < BOARD_HEIGHT; i++) {
|
||||||
|
painter.drawText(ROW_NUM_START + QPoint(0, i * CELL_SIZE.height()),
|
||||||
|
QString::number(i + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < BOARD_WIDTH - 1; i++) //绘制棋盘格子
|
||||||
|
{
|
||||||
|
for (int j = 0; j < BOARD_HEIGHT - 1; j++) {
|
||||||
|
painter.drawRect(QRect(
|
||||||
|
START_POS + QPoint(i * CELL_SIZE.width(), j * CELL_SIZE.height()),
|
||||||
|
CELL_SIZE));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
painter.setPen(Qt::red); //落子提示
|
||||||
|
QPoint poses[12] = {trackPos + QPoint(0, 8), trackPos,
|
||||||
|
trackPos + QPoint(8, 0), trackPos + QPoint(17, 0),
|
||||||
|
trackPos + QPoint(25, 0), trackPos + QPoint(25, 8),
|
||||||
|
trackPos + QPoint(25, 17), trackPos + QPoint(25, 25),
|
||||||
|
trackPos + QPoint(17, 25), trackPos + QPoint(8, 25),
|
||||||
|
trackPos + QPoint(0, 25), trackPos + QPoint(0, 17)};
|
||||||
|
painter.drawPolyline(poses, 3);
|
||||||
|
painter.drawPolyline(poses + 3, 3);
|
||||||
|
painter.drawPolyline(poses + 6, 3);
|
||||||
|
painter.drawPolyline(poses + 9, 3);
|
||||||
|
|
||||||
|
painter.setPen(Qt::NoPen);
|
||||||
|
for (int i = 0; i < BOARD_WIDTH; i++) //绘制棋子
|
||||||
|
{
|
||||||
|
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||||
|
if (board[i][j] != NO_PIECE) {
|
||||||
|
QColor color = (board[i][j] == WHITE_PIECE) ? Qt::white : Qt::black;
|
||||||
|
painter.setBrush(QBrush(color));
|
||||||
|
painter.drawEllipse(
|
||||||
|
START_POS.x() - CELL_SIZE.width() / 2 + i * CELL_SIZE.width(),
|
||||||
|
START_POS.y() - CELL_SIZE.height() / 2 + j * CELL_SIZE.height(),
|
||||||
|
CELL_SIZE.width(), CELL_SIZE.height());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
painter.setPen(Qt::red);
|
||||||
|
if (lastPos.x() != -1) {
|
||||||
|
QPoint drawPos = START_POS + QPoint(lastPos.x() * CELL_SIZE.width(),
|
||||||
|
lastPos.y() * CELL_SIZE.height());
|
||||||
|
painter.drawLine(drawPos + QPoint(0, 5), drawPos + QPoint(0, -5));
|
||||||
|
painter.drawLine(drawPos + QPoint(5, 0), drawPos + QPoint(-5, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (QPoint pos : winPoses) {
|
||||||
|
QPoint drawPos = START_POS + QPoint(pos.x() * CELL_SIZE.width(),
|
||||||
|
pos.y() * CELL_SIZE.height());
|
||||||
|
painter.drawLine(drawPos + QPoint(0, 5), drawPos + QPoint(0, -5));
|
||||||
|
painter.drawLine(drawPos + QPoint(5, 0), drawPos + QPoint(-5, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::mouseReleaseEvent(QMouseEvent *event) {
|
||||||
|
if (!endGame) {
|
||||||
|
QPoint pos = event->pos() - START_POS;
|
||||||
|
int x = pos.x();
|
||||||
|
int y = pos.y();
|
||||||
|
int pieceX = x / CELL_SIZE.width();
|
||||||
|
int pieceY = y / CELL_SIZE.height();
|
||||||
|
int offsetX = x % CELL_SIZE.width();
|
||||||
|
int offsetY = y % CELL_SIZE.height();
|
||||||
|
if (offsetX > CELL_SIZE.width() / 2) {
|
||||||
|
pieceX++;
|
||||||
|
}
|
||||||
|
if (offsetY > CELL_SIZE.height() / 2) {
|
||||||
|
pieceY++;
|
||||||
|
}
|
||||||
|
downPiece(pieceX, pieceY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::mouseMoveEvent(QMouseEvent *event) {
|
||||||
|
QPoint pos = event->pos() - START_POS +
|
||||||
|
QPoint(CELL_SIZE.width() / 2, CELL_SIZE.height() / 2);
|
||||||
|
int x = pos.x();
|
||||||
|
int y = pos.y();
|
||||||
|
//超过范围
|
||||||
|
if (x < 0 || x >= CELL_SIZE.width() * BOARD_WIDTH || y < 0 ||
|
||||||
|
y >= CELL_SIZE.height() * BOARD_HEIGHT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int offsetX = x % CELL_SIZE.width();
|
||||||
|
int offsetY = y % CELL_SIZE.height();
|
||||||
|
setTrackPos(QPoint(x - offsetX, y - offsetY) + START_POS -
|
||||||
|
QPoint(CELL_SIZE.width() / 2, CELL_SIZE.height() / 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::initBoard() {
|
||||||
|
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||||
|
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||||
|
board[i][j] = NO_PIECE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
trackPos = QPoint(28, 28);
|
||||||
|
lastPos = QPoint(-1, -1);
|
||||||
|
endGame = false;
|
||||||
|
winPoses.clear();
|
||||||
|
nextPlayer = BLACK_PLAYER;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::downPiece(int x, int y) {
|
||||||
|
if (x >= 0 && x < BOARD_WIDTH && y >= 0 && y < BOARD_HEIGHT &&
|
||||||
|
board[x][y] == NO_PIECE) {
|
||||||
|
board[x][y] = (nextPlayer == WHITE_PLAYER) ? WHITE_PIECE : BLACK_PIECE;
|
||||||
|
nextPlayer = !nextPlayer;
|
||||||
|
lastPos = QPoint(x, y);
|
||||||
|
checkWinner();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::checkWinner() {
|
||||||
|
bool fullPieces = true;
|
||||||
|
for (int i = 0; i < BOARD_WIDTH; i++) {
|
||||||
|
for (int j = 0; j < BOARD_HEIGHT; j++) {
|
||||||
|
if (board[i][j] == NO_PIECE) {
|
||||||
|
fullPieces = false;
|
||||||
|
}
|
||||||
|
if (board[i][j] != NO_PIECE && isFivePieceFrom(i, j)) {
|
||||||
|
bool winner =
|
||||||
|
(board[i][j] == WHITE_PIECE) ? WHITE_PLAYER : BLACK_PLAYER;
|
||||||
|
endGame = true;
|
||||||
|
emit gameOver(winner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fullPieces) {
|
||||||
|
endGame = true;
|
||||||
|
emit gameOver(2); //代表和棋
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BoardWidget::isFivePieceFrom(int x, int y) {
|
||||||
|
return isVFivePieceFrom(x, y) || isHFivePieceFrom(x, y) ||
|
||||||
|
isFSFivePieceFrom(x, y) || isBSFivePieceFrom(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BoardWidget::isVFivePieceFrom(int x, int y) {
|
||||||
|
int piece = board[x][y];
|
||||||
|
for (int i = 1; i < 5; i++) {
|
||||||
|
if (y + i >= BOARD_HEIGHT || board[x][y + i] != piece) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
winPoses.clear();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
winPoses.append(QPoint(x, y + i));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BoardWidget::isHFivePieceFrom(int x, int y) {
|
||||||
|
int piece = board[x][y];
|
||||||
|
for (int i = 1; i < 5; i++) {
|
||||||
|
if (x + i >= BOARD_WIDTH || board[x + i][y] != piece) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
winPoses.clear();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
winPoses.append(QPoint(x + i, y));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BoardWidget::isFSFivePieceFrom(int x, int y) {
|
||||||
|
int piece = board[x][y];
|
||||||
|
for (int i = 1; i < 5; i++) {
|
||||||
|
if (x + i >= BOARD_WIDTH || y - i < 0 || board[x + i][y - i] != piece) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
winPoses.clear();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
winPoses.append(QPoint(x + i, y - i));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool BoardWidget::isBSFivePieceFrom(int x, int y) {
|
||||||
|
int piece = board[x][y];
|
||||||
|
for (int i = 1; i < 5; i++) {
|
||||||
|
if (x + i >= BOARD_WIDTH || y + i >= BOARD_HEIGHT ||
|
||||||
|
board[x + i][y + i] != piece) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
winPoses.clear();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
winPoses.append(QPoint(x + i, y + i));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoardWidget::setTrackPos(const QPoint &value) {
|
||||||
|
trackPos = value;
|
||||||
|
update();
|
||||||
|
}
|
59
SoftwareDesign/Lab5/lab5_2_gomoku/boardwidget.h
Normal file
59
SoftwareDesign/Lab5/lab5_2_gomoku/boardwidget.h
Normal file
|
@ -0,0 +1,59 @@
|
||||||
|
#ifndef BOARDWIDGET_H
|
||||||
|
#define BOARDWIDGET_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QVector>
|
||||||
|
#include <QPoint>
|
||||||
|
|
||||||
|
class BoardWidget : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit BoardWidget(QWidget *parent = nullptr);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event);
|
||||||
|
void mouseReleaseEvent(QMouseEvent *event);
|
||||||
|
void mouseMoveEvent(QMouseEvent *event);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void downPiece(int x, int y);
|
||||||
|
void checkWinner();
|
||||||
|
bool isFivePieceFrom(int x, int y); //判断从(x, y)处开始,是否有五个同色棋子在一条线上
|
||||||
|
bool isVFivePieceFrom(int x, int y); //判断从(x, y)处开始,向下是否有五个同色棋子
|
||||||
|
bool isHFivePieceFrom(int x, int y); //判断从(x, y)处开始,向下是否有五个同色棋子
|
||||||
|
bool isFSFivePieceFrom(int x, int y); //判断从(x, y)处开始,右上方向是否有五个同色棋子
|
||||||
|
bool isBSFivePieceFrom(int x, int y); //判断(x, y)处开始, 右下方向是否有五个同色棋子
|
||||||
|
void setTrackPos(const QPoint &value);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void gameOver(int winner);
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void initBoard();
|
||||||
|
|
||||||
|
public:
|
||||||
|
static const QSize WIDGET_SIZE; //棋盘控件大小
|
||||||
|
static const QSize CELL_SIZE; //棋盘单元格大小
|
||||||
|
static const QPoint START_POS; //棋盘单元格绘制开始位置
|
||||||
|
static const QPoint ROW_NUM_START; //棋盘行号开始位置
|
||||||
|
static const QPoint CLU_NUM_START; //棋盘列号开始位置
|
||||||
|
static const int BOARD_WIDTH = 15; //棋盘列数
|
||||||
|
static const int BOARD_HEIGHT = 15; //棋盘行数
|
||||||
|
static const int NO_PIECE = 0; //棋子标志, 无子
|
||||||
|
static const int WHITE_PIECE = 1; //棋子标志, 白子
|
||||||
|
static const int BLACK_PIECE = 2; //棋子标志, 黑子
|
||||||
|
static const bool WHITE_PLAYER = true; //棋手标志, 白方
|
||||||
|
static const bool BLACK_PLAYER = false; //棋手标志, 黑方
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool endGame; //游戏结束标志
|
||||||
|
int board[BOARD_WIDTH][BOARD_HEIGHT]; //棋盘数据
|
||||||
|
int nextPlayer; //下一手棋手
|
||||||
|
QPoint lastPos; //上一步位置
|
||||||
|
QPoint trackPos; //鼠标在棋盘上的位置
|
||||||
|
QVector<QPoint> winPoses; //五个相连的棋子位置
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BOARDWIDGET_H
|
32
SoftwareDesign/Lab5/lab5_2_gomoku/gamewidget.cpp
Normal file
32
SoftwareDesign/Lab5/lab5_2_gomoku/gamewidget.cpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "gamewidget.h"
|
||||||
|
#include <QVBoxLayout>
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QMessageBox>
|
||||||
|
#include <QPushButton>
|
||||||
|
|
||||||
|
GameWidget::GameWidget(QWidget *parent)
|
||||||
|
: QWidget(parent)
|
||||||
|
{
|
||||||
|
setWindowTitle("五子棋");
|
||||||
|
boardWidget = new BoardWidget(this);
|
||||||
|
|
||||||
|
connect(boardWidget, &BoardWidget::gameOver, this, &GameWidget::showWinner);
|
||||||
|
}
|
||||||
|
|
||||||
|
GameWidget::~GameWidget()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameWidget::showWinner(int winner)
|
||||||
|
{
|
||||||
|
if (winner != 2)
|
||||||
|
{
|
||||||
|
QString playerName = (winner == BoardWidget::WHITE_PLAYER) ? "白方" : "黑方";
|
||||||
|
QMessageBox::information(this, "游戏结束", tr("恭喜%1获胜!!").arg(playerName), QMessageBox::Ok);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QMessageBox::information(this, "游戏结束", "和棋!", QMessageBox::Ok);
|
||||||
|
}
|
||||||
|
}
|
22
SoftwareDesign/Lab5/lab5_2_gomoku/gamewidget.h
Normal file
22
SoftwareDesign/Lab5/lab5_2_gomoku/gamewidget.h
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef GAMEWIDGET_H
|
||||||
|
#define GAMEWIDGET_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
#include "boardwidget.h"
|
||||||
|
|
||||||
|
class GameWidget : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
GameWidget(QWidget *parent = 0);
|
||||||
|
~GameWidget();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void showWinner(int winner);
|
||||||
|
|
||||||
|
private:
|
||||||
|
BoardWidget *boardWidget;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // GAMEWIDGET_H
|
33
SoftwareDesign/Lab5/lab5_2_gomoku/lab5_2_gomoku.pro
Normal file
33
SoftwareDesign/Lab5/lab5_2_gomoku/lab5_2_gomoku.pro
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#-------------------------------------------------
|
||||||
|
#
|
||||||
|
# Project created by QtCreator 2018-02-23T15:56:45
|
||||||
|
#
|
||||||
|
#-------------------------------------------------
|
||||||
|
|
||||||
|
QT += core gui
|
||||||
|
|
||||||
|
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||||
|
|
||||||
|
TARGET = lab5_2_gomoku
|
||||||
|
TEMPLATE = app
|
||||||
|
|
||||||
|
# The following define makes your compiler emit warnings if you use
|
||||||
|
# any feature of Qt which as been marked as deprecated (the exact warnings
|
||||||
|
# depend on your compiler). Please consult the documentation of the
|
||||||
|
# deprecated API in order to know how to port your code away from it.
|
||||||
|
DEFINES += QT_DEPRECATED_WARNINGS
|
||||||
|
|
||||||
|
# You can also make your code fail to compile if you use deprecated APIs.
|
||||||
|
# In order to do so, uncomment the following line.
|
||||||
|
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||||
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
|
|
||||||
|
SOURCES += \
|
||||||
|
main.cpp \
|
||||||
|
gamewidget.cpp \
|
||||||
|
boardwidget.cpp
|
||||||
|
|
||||||
|
HEADERS += \
|
||||||
|
gamewidget.h \
|
||||||
|
boardwidget.h
|
11
SoftwareDesign/Lab5/lab5_2_gomoku/main.cpp
Normal file
11
SoftwareDesign/Lab5/lab5_2_gomoku/main.cpp
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#include "gamewidget.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
GameWidget w;
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return a.exec();
|
||||||
|
}
|
Reference in a new issue