Stone.h
#ifndef STONE_H
#define STONE_H
#include <QString>
class Stone
{
public:
Stone();
//定义棋子的所有类型
enum TYPE{JIANG,CHE,PAO,MA,BING,SHI,XIANG};
//棋子所处的行
int _row;
//棋子所处的列
int _col;
//棋子的id
int _id;
//棋子是否已死
bool _dead;
//棋子是否为红子
bool _red;
//棋子类型
TYPE _type;
//初始化棋子
void init(int id);
//获取棋子的类型名
QString getText();
};
#endif // STONE_H
Stone.cpp
#include "Stone.h"
Stone::Stone()
{
}
void Stone::init(int id)
{
// 总共有16种棋子
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 Stone::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 "Wrong";
}
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);
Stone _s[32]; // 32个棋子
int _r; // r棋子的半径,格子一半的宽度
int _selectid; // 被选中的棋子
bool _bRedTurn; // 是否轮到红方走
// 根据行和列获取棋子的id
int getStoneId(int row, int col);
//计算即将行走的棋子与某一坐标之间有几颗棋子
int num_of_Stone(int moveid,int row,int col);
//输入行列坐标判断该坐标上有没有棋子
bool beStone(int row,int col);
// 判断棋子的颜色是否相同
bool sameColor(int moveid,int killid);
// 由点击坐标转化为棋子的行列编号
bool getRowCol(QPoint pt, int &row, int &col);
bool canSelect(int id);
//最基本的能不能走的判断判断
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 r