Loading repository data…
Loading repository data…
OlyMarco / repository
Feature-complete Gobang (Five-in-a-Row) desktop game with Qt 5.12 & C++11. AI scoring algorithm, local/LAN multiplayer, cross-platform support.
这是一款功能完整的桌面五子棋应用程序,展示了现代C++与Qt框架的综合应用。项目基于 Qt 5.12 (C++11) 开发,实现了人机对战、本地双人、局域网联机三种游戏模式。AI采用评分表算### Controls:
方法一: Qt Creator
1. 打开 Qt Creator
2. File → Open File or Project
3. 选择 GoBang_by_Temmie/GoBang_by_Temmie.pro
4. 配置 Kit (编译器和Qt版本)
5. 点击 Build 或按 Ctrl+B
6. 点击 Run 或按 Ctrl+R
方法二: 命令行编译
# 生成 Makefile
qmake GoBang_by_Temmie.pro
# Windows (MSVC)
nmake
# Windows (MinGW) / Linux / macOS
make
# 运行
./GoBang_by_Temmie # Linux/macOS
GoBang_by_Temmie.exe # Windows
📦 下载 Release 中的预编译 .exe 文件,解压后直接运行。
适合学习以下技术点:
This is a feature-complete desktop Gobang (Five-in-a-Row) application demonstrating comprehensive use of modern C++ and Qt framework. Built with Qt 5.12 (C++11), it implements three game modes: AI battle, local multiplayer, and LAN multiplayer. The AI uses a scoring table algorithm with 8-direction pattern recognition for intelligent moves, providing challenging gameplay without deep search algorithms.
├── BoardParams # 棋盘参数与常量定义
├── GameCore # 游戏核心逻辑与状态管理
├── ScoreEngine # AI评分引擎
├── GameWindow # 游戏主窗口与棋盘渲染
├── LanBattle # 局域网客户端
└── NetServer # 网络服务器
评分规则表 (Scoring Rules):
| 棋型 | 连子数 | 空位情况 | 进攻分 | 防守分 | 描述 |
|---|---|---|---|---|---|
| 活五 | 4+ | - | 10100 | 10000 | 必胜棋型 |
| 活四 | 3 | 两端空 | 110 | 100 | 下一步成五 |
| 死四 | 3 | 一端空 | 60 | 55 | 需要封堵 |
| 活三 | 2 | 两端空 | 50 | 40 | 潜在威胁 |
| 死三 | 2 | 一端空 | 25 | 30 | 有限威胁 |
| 活二 | 1 | - | 10 | 10 | 初步形成 |
算法流程:
代码示例:
// 核心评分逻辑 (scoreEngine.cpp)
scoreMapVec = engine.calculateScoreEngine(gameMapVec, scoreMapVec);
// 遍历八方向,计算玩家威胁和AI优势
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++)
// 延伸4格,识别棋型
1. BoardParams - 参数配置层
const int ChessBoardSize = 15; // 15×15标准棋盘
const int ChessBlockSize = 40; // 格子大小
enum GameType { LOCAL_PERSON, NET_PERSON, BOT };
enum GameStatus { PLAYING, WIN, LOSE, DRAW, FREE };
2. GameCore - 核心逻辑层
std::vector<std::vector<int>> gameMapVecuser 结构体封装玩家信息3. ScoreEngine - AI引擎层
4. GameWindow - 视图层
QMainWindowpaintEvent 自定义绘图mouseMoveEvent/mouseReleaseEvent 处理交互5. 网络层 (LanBattle + NetServer)
// 服务器端
QTcpServer *m_s;
QVector<UserConnection*> Tcp_List; // 连接管理
// 客户端
QTcpSocket *tcp;
// 数据序列化传输
用户点击 → GameWindow::mouseReleaseEvent
↓
GameCore::actionByPerson/AI/Net
↓
updateGameMap (更新棋盘状态)
↓
isWin / isDraw (胜负判断)
↓
paintEvent (重绘界面)
网络模式数据流:
Client1 → LanBattle → TCP → NetServer → TCP → LanBattle → Client2
可以下载已发布的 .exe 文件,直接运行。
通过Qt打开源程序目录下的 GoBang_by_Temmie.pro 文件,直接编译运行。
我学习了Qt平台的基础操作,如何进行多文件编程、网络编程,并加深了对C++类编程的理解。通过实现AI评分算法,掌握了游戏AI的基本思路;通过网络模块开发,理解了TCP通信和客户端-服务器架构;通过自定义绘图,学会了Qt的绘图系统。
I learned fundamental Qt operations, multi-file programming, and network programming, deepening my understanding of C++ OOP. By implementing the AI scoring algorithm, I grasped the basics of game AI; through network module development, I understood TCP communication and client-server architecture; through custom drawing, I mastered Qt's painting system.
| 模块 | 用途 | 关键类 |
|---|---|---|
| Qt Core | 核心功能、信号槽 | QObject, QString, QVector |
| Qt Widgets | GUI界面 | QMainWindow, QLabel, QPainter |
| Qt Network | 网络通信 | QTcpSocket, QTcpServer |
| Qt Gui | 事件处理 | QMouseEvent, QPaintEvent |
std::vector, std::stack, std::stringnullptr, auto, enum classuser 数据封装GoBang_by_Temmie/
├── main.cpp # 程序入口
├── mainwindow.{h,cpp,ui} # 主窗口(模式选择)
├── icon.{qrc,rc} # 图标资源
├── logo.ico # 应用图标
├── BoardParams/ # 参数模块
│ ├── boardParams.h # 常量、枚举定义
│ └── boardParams.cpp
├── GameCore/ # 核心逻辑
│ ├── gameCore.h # 游戏状态、规则
│ └── gameCore.cpp # 落子、判断逻辑
├── ScoreEngine/ # AI引擎
│ ├── scoreEngine.h # 评分接口
│ └── scoreEngine.cpp # 评分算法实现
├── GameWindow/ # 游戏窗口
│ ├── gameWindow.h # 界面、交互
│ └── gameWindow.cpp # 绘图、事件处理
├── LanBattle/ # 联网客户端
│ ├── lanBattle.{h,cpp,ui} # 连接服务器
│ └── ... # TCP通信
├── NetServer/ # 网络服务器
│ ├── netServer.{h,cpp,ui} # 服务器管理
│ └── userConnection.{h,cpp} # 连接管理
└── GoBang_by_Temmie.pro # 项目配置文件
本项目采用 MIT License 开源协议。
详见 LICENSE 文件。
感谢 Qt 社区提供的优秀框架和文档支持!
Thanks to the Qt community for the excellent framework and documentation support!
⭐ 如果这个项目对您有帮助,请给个 Star!
⭐ If this project helps you, please give it a Star!
Made with ❤️ by Temmie | 2023