当前位置: 首页 > news >正文

基于QT和C++实现的中国象棋

一,源码

board.h

#ifndef BOARD_H
#define BOARD_H#include <QWidget>
#include "Stone.h"class Board : public QWidget
{Q_OBJECT
public:explicit Board(QWidget *parent = 0);bool _bRedTurn; // 红方先走int _currentPlayer; // 当前玩家,1为红方,-1为黑方Stone _s[32];int _r; /* 棋子的半径 */int _selectid;/* 返回象棋棋盘行列对应的像素坐标 */QPoint center(int row, int col);QPoint center(int id);bool getRowCol(QPoint pt, int& row, int& col);void drawStone(QPainter& painter, int id);void paintEvent(QPaintEvent *);void mouseReleaseEvent(QMouseEvent *);bool canMove(int moveid, int row, int col, int killid);bool canMoveJIANG(int moveid, int row, int col, int killid);bool canMoveSHI(int moveid, int row, int col, int killid);bool canMoveXIANG(int moveid, int row, int col, int killid);bool canMoveCHE(int moveid, int row, int col, int killid);bool canMoveMA(int moveid, int row, int col, int killid);bool canMovePAO(int moveid, int row, int col, int killid);bool canMoveBING(int moveid, int row, int col, int killid);bool isStoneAt(int row, int col);void saveGameState();
signals:public slots:};#endif // BOARD_H

Stone.h

#ifndef STONE_H
#define STONE_H#include <QString>class Stone
{
public:Stone();~Stone();int getRow()const{return _row;}int getCol()const{return _col;}enum TYPE{JIANG, CHE, PAO, MA, BING, SHI, XIANG};int _row;int _col;TYPE _type;int _id;bool _dead;bool _red;void init(int id){struct {int row, col;Stone::TYPE type;} pos[16] = {{0, 0, Stone::CHE},{0, 1, Stone::MA},{0, 2, Stone::XIANG},{0, 3, Stone::SHI},{0, 4, Stone::JIANG},{0, 5, Stone::SHI},{0, 6, Stone::XIANG},{0, 7, Stone::MA},{0, 8, Stone::CHE},{2, 1, Stone::PAO},{2, 7, Stone::PAO},{3, 0, Stone::BING},{3, 2, Stone::BING},{3, 4, Stone::BING},{3, 6, Stone::BING},{3, 8, Stone::BING},};_id = id;_dead = false;_red = id<16;if(id < 16){_row = pos[id].row;_col = pos[id].col;_type = pos[id].type;}else{_row = 9-pos[id-16].row;_col = 8-pos[id-16].col;_type = pos[id-16].type;}}QString getText(){switch(this->_type){case CHE:return "车";case MA:return "马";case PAO:return "炮";case BING:return "兵";case JIANG:return "将";case SHI:return "士";case XIANG:return "相";}return "错误";}
};#endif // STONE_H

board.cpp

#include "Board.h"
#include <QPainter>
#include <QMouseEvent>
#include <QMessageBox>
#include<QTextStream>
Board::Board(QWidget *parent) :QWidget(parent){for(int i=0; i<32; ++i){_s[i].init(i);}_selectid = -1;_bRedTurn = true;
}void Board::paintEvent(QPaintEvent*)
{QPainter painter(this);int d = 40;_r = d / 2;// 画10横线for (int i = 1; i <= 10; ++i){painter.drawLine(QPoint(d, i * d), QPoint(9 * d, i * d));}// 画9竖线for (int i = 1; i <= 9; ++i){if (i == 1 || i == 9)painter.drawLine(QPoint(i * d, d), QPoint(i * d, 10 * d));else{painter.drawLine(QPoint(i * d, d), QPoint(i * d, 5 * d));painter.drawLine(QPoint(i * d, 6 * d), QPoint(i * d, 10 * d));}}// 九宫格painter.drawLine(QPoint(4 * d, 1 * d), QPoint(6 * d, 3 * d));painter.drawLine(QPoint(6 * d, 1 * d), QPoint(4 * d, 3 * d));painter.drawLine(QPoint(4 * d, 8 * d), QPoint(6 * d, 10 * d));painter.drawLine(QPoint(6 * d, 8 * d), QPoint(4 * d, 10 * d));// 绘制32个棋子for (int i = 0; i < 32; ++i){drawStone(painter, i);}
}QPoint Board::center(int row, int col)
{QPoint ret;ret.rx() = (col + 1) * _r * 2;ret.ry() = (row + 1) * _r * 2;return ret;
}QPoint Board::center(int id)
{return center(_s[id]._row, _s[id]._col);
}void Board::drawStone(QPainter& painter, int id)
{if (_s[id]._dead)return;QPoint c = center(id);QRect rect = QRect(c.x() - _r, c.y() - _r, _r * 2, _r * 2);if (id == _selectid)painter.setBrush(QBrush(Qt::gray));elsepainter.setBrush(QBrush(Qt::yellow));painter.setPen(Qt::black);painter.drawEllipse(center(id), _r, _r);if (_s[id]._red)painter.setPen(Qt::red);painter.setFont(QFont("system", _r, 700));painter.drawText(rect, _s[id].getText(), QTextOption(Qt::AlignCenter));
}bool Board::getRowCol(QPoint pt, int& row, int& col)
{for (row = 0; row <= 9; row++){for (col = 0; col <= 8; col++){QPoint c = center(row, col);int dx = c.x() - pt.x();int dy = c.y() - pt.y();int dist = dx * dx + dy * dy;if (dist < _r * _r)return true;}}return false;
}bool Board::isStoneAt(int row, int col)
{for (int i = 0; i < 32; ++i){if (_s[i]._row == row && _s[i]._col == col && !_s[i]._dead){return true;}}return false;
}bool Board::canMoveXIANG(int moveid, int row, int col, int killid)
{int fromRow = _s[moveid]._row;int fromCol = _s[moveid]._col;int toRow = row;int toCol = col;if (_s[moveid]._red && toRow > 4) return false; // 红方象不能过河if (!_s[moveid]._red && toRow < 5) return false; // 黑方象不能过河int rowDir = toRow - fromRow;int colDir = toCol - fromCol;if (abs(rowDir) != 2 || abs(colDir) != 2) return false;int checkRow = fromRow + rowDir / 2;int checkCol = fromCol + colDir / 2;if (isStoneAt(checkRow, checkCol)) return false; // 路径上有棋子if (killid != -1 && _s[killid]._red == _s[moveid]._red) return false;return true;
}bool Board::canMoveCHE(int moveid, int row, int col, int killid)
{int fromRow = _s[moveid]._row;int fromCol = _s[moveid]._col;int toRow = row;int toCol = col;if (fromRow != toRow && fromCol != toCol) return false;// 计算移动的方向int rowDir = fromRow < toRow ? 1 : (fromRow > toRow ? -1 : 0);int colDir = fromCol < toCol ? 1 : (fromCol > toCol ? -1 : 0);int checkRow = fromRow + rowDir;int checkCol = fromCol + colDir;while (checkRow != toRow || checkCol != toCol){if (isStoneAt(checkRow, checkCol)) return false; // 路径上有棋子checkRow += rowDir;checkCol += colDir;}// 如果有棋子被吃,检查是否是对方的棋子if (killid != -1 && _s[killid]._red == _s[moveid]._red) return false;return true;
}
bool Board::canMoveMA(int moveid, int row, int col, int killid)
{int fromRow = _s[moveid]._row;int fromCol = _s[moveid]._col;int toRow = row;int toCol = col;int rowDiff = toRow - fromRow;int colDiff = toCol - fromCol;// 检查移动是否是“日”字形状if ((abs(rowDiff) == 2 && abs(colDiff) == 1) || (abs(rowDiff) == 1 && abs(colDiff) == 2)){// 检查是否有棋子蹩马腿int legRow = fromRow + (rowDiff > 0 ? (rowDiff / 2) : -(abs(rowDiff) / 2));int legCol = fromCol + (colDiff > 0 ? (colDiff / 2) : -(abs(colDiff) / 2));if (!isStoneAt(legRow, legCol)) // 如果没有棋子蹩马腿{// 如果有棋子被吃,检查是否是对方的棋子if (killid != -1 && _s[killid]._red != _s[moveid]._red){return true; // 可以吃掉对方的棋子}else if (killid == -1){return true;}}}return false; // 不满足移动条件
}bool Board::canMovePAO(int moveid, int row, int col, int killid)
{int fromRow = _s[moveid]._row;int fromCol = _s[moveid]._col;int toRow = row;int toCol = col;if (fromRow != toRow && fromCol != toCol) return false;int rowDir = fromRow < toRow ? 1 : (fromRow > toRow ? -1 : 0);int colDir = fromCol < toCol ? 1 : (fromCol > toCol ? -1 : 0);// 检查移动路径上是否有其他棋子int checkRow = fromRow + rowDir;int checkCol = fromCol + colDir;int jumpCount = 0;while (checkRow != toRow || checkCol != toCol){if (isStoneAt(checkRow, checkCol)) jumpCount++;checkRow += rowDir;checkCol += colDir;}if (killid != -1){if (_s[killid]._red == _s[moveid]._red) return false;if (jumpCount != 1) return false; // 必须跳过一个棋子}else{if (jumpCount != 0) return false; // 如果没有吃子,不能跳过棋子}return true;
}bool Board::canMoveBING(int moveid, int row, int col, int killid)
{int fromRow = _s[moveid]._row;int fromCol = _s[moveid]._col;int toRow = row;int toCol = col;if (fromRow != toRow && fromCol != toCol) return false;int rowDiff = toRow - fromRow;int colDiff = toCol - fromCol;// 检查是否过河if (_s[moveid]._red && toRow >= 5) // 红方兵过河{if (abs(rowDiff) != 1) return false; // 过河后只能直走一格}else if (!_s[moveid]._red && toRow <= 4) // 黑方兵过河{if (abs(rowDiff) != 1) return false; // 过河后只能直走一格}else // 未过河{if (abs(rowDiff) != 1) return false; // 未过河只能直走一格}if (killid != -1 && _s[killid]._red == _s[moveid]._red) return false;}bool Board::canMoveJIANG(int moveid, int row, int col, int killid)
{if (_s[moveid]._red){if (row > 2)return false;}else{if (row < 7)return false;}if (col < 3) return false;if (col > 5) return false;int rowDir = _s[moveid]._row - row;int colDir = _s[moveid]._col - col;int d = abs(rowDir) * 10 + abs(colDir);if (d == 1 || d == 10)return true;return false;
}bool Board::canMoveSHI(int moveid, int row, int col, int killid)
{if (_s[moveid]._red){if (row > 2)return false;}else{if (row < 7)return false;}if (col < 3) return false;if (col > 5) return false;int rowDir = _s[moveid]._row - row;int colDir = _s[moveid]._col - col;int d = abs(rowDir) * 10 + abs(colDir);if (d == 11)return true;return false;}bool Board::canMove(int moveid, int row, int col, int killid)
{// 如果移动的棋子和被吃的棋子颜色相同,则切换选择if (_s[moveid]._red == _s[killid]._red && killid != -1){_selectid = killid;update();return false;}// 判断是否是主将被吃if (_s[killid]._type == Stone::JIANG && _s[killid]._red != _s[moveid]._red){QString winner = _s[moveid]._red ? "红方" : "黑方";QMessageBox::information(this, "胜利", "恭喜," + winner + "赢了!");return false; // 停止游戏}// 根据棋子类型调用相应的移动规则函数switch (_s[moveid]._type){case Stone::JIANG:return canMoveJIANG(moveid, row, col, killid);break;case Stone::SHI:return canMoveSHI(moveid, row, col, killid);break;case Stone::XIANG:return canMoveXIANG(moveid, row, col, killid);break;case Stone::CHE:return canMoveCHE(moveid, row, col, killid);break;case Stone::MA:return canMoveMA(moveid, row, col, killid);break;case Stone::PAO:return canMovePAO(moveid, row, col, killid);break;case Stone::BING:return canMoveBING(moveid, row, col, killid);break;default:return false;}return true;
}void Board::saveGameState() {// 假设你有一个数组来存储棋盘的状态,比如棋子的类型和位置QString fileName = "game_state.txt";QFile file("game.txt");if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {QMessageBox::critical(this, "保存棋局状态", "无法打开文件进行保存!");return;}QTextStream out(&file);for (int i = 0; i < 32; ++i) {out << _s[i].getText() << "," << _s[i].getRow() << "," << _s[i].getCol() << "\n";}file.close();QMessageBox::information(this, "保存棋局状态", "棋局状态已成功保存!");
}void Board::mouseReleaseEvent(QMouseEvent *ev)
{QPoint pt = ev->pos();// 将pt转化成象棋的行列值// 判断这个行列值上面有没有棋子int row, col;bool bRet = getRowCol(pt, row, col);if(bRet == false) // 点到棋盘外return;int i;int clickid = -1;for(i=0;i<32;++i){if(_s[i]._row == row && _s[i]._col == col && _s[i]._dead== false){clickid = i;break;}}if(_selectid == -1){if(clickid != -1){if(_bRedTurn == _s[clickid]._red){_selectid = clickid;update();}}}else{if(canMove(_selectid, row, col, clickid)){/*走棋*/_s[_selectid]._row = row;_s[_selectid]._col = col;if(clickid != -1){_s[clickid]._dead = true;}_selectid = -1;_bRedTurn = !_bRedTurn;update();}else{// 不能移动棋子,给出提示QMessageBox::information(this, "提示", "该棋子不能移动到指定的位置!");// 如果点击的是另一个相同颜色的棋子,则切换选择if(clickid != -1 && _s[clickid]._red == _s[_selectid]._red){_selectid = clickid;update();}else{_selectid = -1; // 取消选择update();}}}}

main.cpp


#include <QApplication>
#include "Board.h"int main(int argc, char* argv[])
{QApplication app(argc, argv);Board board;board.show();return app.exec();
}

Stone.cpp

#include "Stone.h"Stone::Stone()
{}Stone::~Stone()
{}

二,具体分析

棋盘的绘制,棋子的绘制,以及选中棋子进行一系列操作都使用QT的库函数,所以在创建

board类时我们需要选择基于Qwidge作为父类

board.cpp是实现绘制棋盘,棋子,以及棋子运行逻辑的文件

Stone.h包含了棋子类

三,运行展示

相关文章:

  • Cascadia Code 字体
  • 【面试八股文】谈一谈你对TCP和UDP的区别是怎么理解的?
  • 小甲鱼——字典
  • 【并发编程实战】内存模型--解决可见性和有序性的利器
  • LoRA用于高效微调的基本原理
  • 【C语言】回调函数 和 部分库函数的用法以及模拟实现
  • 深入理解 MySQL 查询分析工具 EXPLAIN 的使用
  • 【ARMv8/ARMv9 硬件加速系列 4 -- 加解密 Cryptographic Extension 介绍】
  • 通过摄像头检测步频
  • 【C语言】数组参数和指针参数详解
  • MOS参数详解
  • nginx ws长连接配置
  • web端即时通信技术
  • Python for循环中的引用传递和值传递
  • Redis 面试热点(二)
  • 【刷算法】求1+2+3+...+n
  • Android系统模拟器绘制实现概述
  • Android组件 - 收藏集 - 掘金
  • css系列之关于字体的事
  • ES学习笔记(12)--Symbol
  • Git初体验
  • javascript 总结(常用工具类的封装)
  • jquery ajax学习笔记
  • k个最大的数及变种小结
  • PHP 的 SAPI 是个什么东西
  • rabbitmq延迟消息示例
  • vue-cli在webpack的配置文件探究
  • Vue全家桶实现一个Web App
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 关于Android中设置闹钟的相对比较完善的解决方案
  • 马上搞懂 GeoJSON
  • 使用API自动生成工具优化前端工作流
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 小试R空间处理新库sf
  • ​​快速排序(四)——挖坑法,前后指针法与非递归
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​Linux·i2c驱动架构​
  • ​无人机石油管道巡检方案新亮点:灵活准确又高效
  • #pragma 指令
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (3)选择元素——(17)练习(Exercises)
  • (9)目标检测_SSD的原理
  • (bean配置类的注解开发)学习Spring的第十三天
  • (C语言)逆序输出字符串
  • (附源码)spring boot基于Java的电影院售票与管理系统毕业设计 011449
  • (附源码)springboot宠物管理系统 毕业设计 121654
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (一)Kafka 安全之使用 SASL 进行身份验证 —— JAAS 配置、SASL 配置
  • (原創) 如何使用ISO C++讀寫BMP圖檔? (C/C++) (Image Processing)
  • (转)C#开发微信门户及应用(1)--开始使用微信接口
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转)Sublime Text3配置Lua运行环境