实现功能:棋子初始化及走棋规则

棋子类:

 #ifndef STONE_H
#define STONE_H #include <QString> class Stone
{
public:
Stone();
~Stone(); enum TYPE{JIANG, CHE, PAO, MA, BING, SHI, XIANG}; int _row; //棋子所在行
int _col; //棋子所在列
TYPE _type; //棋子类型(JIANG,CHE...) int _id; //棋子id(0-31)
bool _dead; //棋子是否死亡
bool _red; //是否为红方 void init(int id) //初始化棋子信息
{
struct {
int row, col;
Stone::TYPE type;
} pos[] = {
{, , Stone::CHE},
{, , Stone::MA},
{, , Stone::XIANG},
{, , Stone::SHI},
{, , Stone::JIANG},
{, , Stone::SHI},
{, , Stone::XIANG},
{, , Stone::MA},
{, , Stone::CHE}, {, , Stone::PAO},
{, , Stone::PAO},
{, , Stone::BING},
{, , Stone::BING},
{, , Stone::BING},
{, , Stone::BING},
{, , Stone::BING},
}; _id = id;
_dead = false;
_red = id<; if(id < )
{
_row = pos[id].row;
_col = pos[id].col;
_type = pos[id].type;
}
else
{
_row = -pos[id-].row;
_col = -pos[id-].col;
_type = pos[id-].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

棋盘类:

 #ifndef BOARD_H
#define BOARD_H #include <QWidget>
#include "Stone.h" class Board : public QWidget
{
Q_OBJECT
public:
explicit Board(QWidget *parent = ); Stone _s[]; //32枚棋子
int _r; //棋子的半径
int _selectid; //记录选中的棋子id
bool _bRedTurn; //记录是否轮到红方走棋 /* 返回象棋棋盘行列对应的像素坐标 */
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 *); /* 根据要移动的棋子moveid,将要移动到的位置(row,col),将被吃掉的棋子killid判断能否走棋 */
bool canMove(int moveid, int row, int col, int killid);
/* 将 */
bool canMove1(int moveid, int row, int col, int killid);
/* 车 */
bool canMove2(int moveid, int row, int col, int killid);
bool canMove3(int moveid, int row, int col, int killid);
bool canMove4(int moveid, int row, int col, int killid);
bool canMove5(int moveid, int row, int col, int killid);
bool canMove6(int moveid, int row, int col, int killid);
bool canMove7(int moveid, int row, int col, int killid); signals: public slots: }; #endif // BOARD_H

判断走棋函数:

 bool Board::canMove(int moveid, int row, int col, int killid)
{
if(_s[moveid]._red == _s[killid]._red)//moveid和killid颜色相同)
{
//换选择
_selectid = killid;
update(); return false;
} switch(_s[moveid]._type)
{
case Stone::JIANG:
return canMove1(moveid, row, col, killid);
break;
case Stone::SHI:
return canMove2(moveid, row, col, killid);
break;
case Stone::XIANG:
return canMove3(moveid, row, col, killid);
break;
case Stone::CHE:
return canMove4(moveid, row, col, killid);
break;
case Stone::MA:
return canMove5(moveid, row, col, killid);
break;
case Stone::PAO:
return canMove6(moveid, row, col, killid);
break;
case Stone::BING:
return canMove7(moveid, row, col, killid);
break;
} return true;
}

“将” 的走棋规则说明:

 bool Board::canMove1(int moveid, int row, int col, int killid)
{
/*
1.首先目标位置在九宫内
2.移动的步长是一个格子
*/
if(_s[moveid]._red)
{
if(row > )return false;
}
else
{
if(row < )return false;
} if(col < ) return false;
if(col > ) return false; int dr = _s[moveid]._row - row;
int dc = _s[moveid]._col - col;
int d = abs(dr)* + abs(dc); // 12, 21 22 10, 1
if(d == || d == )
return true; return false;
}

判断 “将” 要移动的位置与原来位置相差一步的处理:

 int dr = _s[moveid]._row - row;
int dc = _s[moveid]._col - col;
int d = abs(dr)* + abs(dc); // 12, 21 22 10, 1
if(d == )
return true;

鼠标点击释放事件:

 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 = -;
for(i=;i<;++i)
{
if(_s[i]._row == row && _s[i]._col == col && _s[i]._dead== false)
{
break;
}
} if(i<)
{
clickid = i;
} if(_selectid == -)
{
if(clickid != -)
{
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 != -)
{
_s[clickid]._dead = true;
}
_selectid = -;
_bRedTurn = !_bRedTurn;
update();
}
}
}

效果图:

Qt版本中国象棋开发(三)的更多相关文章

  1. Qt版本中国象棋开发(四)

    内容:走法产生 中国象棋基础搜索AI, 极大值,极小值剪枝搜索, 静态估值函数 理论基础: (一)人机博弈走法产生: 先遍历某一方的所有棋子,再遍历整个棋盘,得到每个棋子的所有走棋情况(效率不高,可以 ...

  2. Qt版本中国象棋开发(一)

    开发目的:实现象棋人机对战简单AI,网络对战,移植到android中. 开发平台:windows10 + Qt5.4 for android 开发语言:C++ 开发过程:1.棋盘绘制: 方法一:重写  ...

  3. Qt版本中国象棋开发(二)

    实现功能:棋盘绘制 核心函数: void paintEvent(QPaintEvent *); //QWidget自带的虚函数,重写后使用 QPainter 类来绘制图形 QPainter paint ...

  4. 基于QT的中国象棋

    基于QT的中国象棋,可实现人人对战,人机对战,联网对战,可显示棋谱,可悔棋. 还有一些小毛病,我之后会找空把这个DEMO重新修改一下上传 链接:https://pan.baidu.com/s/1-eM ...

  5. Cocos2d-X开发中国象棋《三》開始场景的实现

    在前面两节(第一节.第二节)中介绍了中国象棋的功能和project文件.在这篇博客中将介绍中国象棋的開始场景的实现 在写代码前先理清一下实现開始场景的思路: 1.打开游戏后进入開始场景,场景上显示一个 ...

  6. Qt绘制中国象棋棋盘

    这里主要用的是#include <QPainter>里面的paintEvent void Board::paintEvent(QPaintEvent*) { QPainter painte ...

  7. Cocos2d-X开发中国象棋《二》project文件概述

    我在上一篇博客中介绍了象棋的功能.在接下来的博客中将向大家介绍使用Cocos2d-X怎样一步一步开发中国象棋 开发工具: Cocos2d-X2.2.3 VS2012 项目的文件夹: Classes:存 ...

  8. Python开发中国象棋实战(附源码)

        Pygame 做的中国象棋,一直以来喜欢下象棋,写了 python 就拿来做一个试试,水平有限,电脑走法水平低,需要在下次版本中更新电脑走法,希望源码能帮助大家更好的学习 python.总共分 ...

  9. cocos2d-x游戏开发系列教程-中国象棋02-main函数和欢迎页面

    之前两个博客讲述了象棋的规格和工程文件之后,我们继续深入的从代码开始学习cocos2dx 首先从程序入口main函数开始 main函数 int APIENTRY _tWinMain(HINSTANCE ...

随机推荐

  1. NodeJS反向代理websocket

    如需转载请标明出处:http://blog.csdn.net/itas109QQ技术交流群:129518033 文章目录NodeJS反向代理websocket@[toc]前言代码相关问题:1.http ...

  2. 算法竞赛进阶指南--hamilton路径

    // hamilton路径 int f[1 << 20][20]; int hamilton(int n, int weight[20][20]) { memset(f, 0x3f, si ...

  3. CF1335F Robots on a Grid

    比较简单的倍增 但还是看了题解才会 题意 给出一个 \(n\times m\) 的网格,每个格子有颜色,\(0\) 黑 \(1\) 白,每个格子还有一个方向,表示这个格子上的机器人会向那个方向走,并保 ...

  4. 更安全的rm命令,保护重要数据

    更安全的rm命令,保护重要数据 网上流传的安全的rm,几乎都是提供一个rm的"垃圾"回收站,在服务器环境上来说,这实非良方. 我想,提供一个安全的rm去保护一些重要的文件或目录不被 ...

  5. bootstrap栅格系统的使用

    bootstrap栅格系统的使用 bootstrap栅格系统的使用,主要分为四种方式 1.列组合  col-md-* 2.列偏移 col-md-offset-* 3.列嵌套  大列组合包含着小组合 4 ...

  6. 系统基础优化 vim

    系统基础优化 vim 1系统基础优化 (CPU-lscpu 内存-free 磁盘-df 负载-w/uptime) 1.1 系统基础优化 准备工作:如何查看系统的信息 (1)cat /etc/redha ...

  7. 【Hadoop离线基础总结】MapReduce自定义InputFormat和OutputFormat案例

    MapReduce自定义InputFormat和OutputFormat案例 自定义InputFormat 合并小文件 需求 无论hdfs还是mapreduce,存放小文件会占用元数据信息,白白浪费内 ...

  8. SAP ME01创建货源清单

    1业务说明 此文档使用函数:ME_DIRECT_INPUT_SOURCE_LIST创建货源清单 2前台实现 事务代码:ME01 输入抬头信息 保存即可 3代码实现 3.1调用函数 定义参数 字段 调用 ...

  9. FOC:在MCU上检验Clark和Park坐标变换是否正确

    文章目录 前言 程序 头文件 clark 变换 C实现 park c 变换实现 仿真 前言 仿真简单,可以参考仿真的结果,但是实际中将代码移植到MCU,会出现一些新的问题,所以需要对坐标变换部分算法进 ...

  10. zabbix-agent客户端安装与配置

    zabbix-agent客户端安装与配置 下载abbix-agent客户端源码软件包 解压agent源码包,并且切换到解压目录. [root@localhost ~]# tar -zxf zabbix ...