#include "ChessBoard.h"
#include "ui_ChessBoard.h"
ChessBoard::ChessBoard(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ChessBoard)
{
init();
//计时器部分
m_timer = new QTimer; //初始化定时器
m_timeRecord = new QTime(0, 0, 0); //初始化时间
m_bIsStart = false; //初始为还未计时
connect(m_timer,SIGNAL(timeout()),this,SLOT(updateTime()));
this->setWindowIcon(QIcon(":/images/chess.jfif"));
QPixmap pixChessBoard=QPixmap(":/images/sky.jpg");
QPalette palette(this->palette());
palette.setBrush( QPalette::Window,QBrush(pixChessBoard));
// palette.setColor(QPalette::Background,Qt::white);
this->setPalette(palette);
ui->setupUi(this);
}
ChessBoard::~ChessBoard()
{
delete ui;
}
void ChessBoard::init()
{
for(int i = 0; i<32; i++)
m_ChessPieces[i].init(i);
m_ChessSteps.clear(); //重置步数
m_nSelectID = -1;
m_nCheckedID = -1;
m_bIsRed = true;
m_bIsOver = false;
m_bIsShowStep = true;
}
bool ChessBoard:: isRed(int id)
{
return m_ChessPieces[id].m_bRed;
}
void ChessBoard:: killStone(int id)
{
if(id== -1)
return;
m_ChessPieces[id].m_bDead= true;
}
void ChessBoard:: reliveStone(int id)
{
if(id== -1)
return;
m_ChessPieces[id].m_bDead= false;
}
void ChessBoard:: moveStone(int moveid, int row, int col)
{
m_ChessPieces[moveid].m_nRow= row;
m_ChessPieces[moveid].m_nCol= col;
m_bIsRed= !m_bIsRed; //换边
}
bool ChessBoard::sameColor(int moveId,int killId)
{
if(moveId== -1 || killId== -1)
return false;
return isRed(moveId)== isRed(killId);
}
bool ChessBoard::isDead(int id)
{
if(id == -1)
return true;
return m_ChessPieces[id].m_bDead;
}
int ChessBoard::getStoneId(int row, int col)
{
for(int i=0; i<32; ++i)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && !isDead(i))
return i;
}
return -1;
}
int ChessBoard::getStoneCountAtLine(int row1, int col1, int row2, int col2)
{
int ret = 0;
if(row1 != row2 && col1 != col2)
return -1;
if(row1 == row2 && col1 == col2)
return -1;
if(row1 == row2)
{
int min = col1 < col2 ? col1 : col2;
int max = col1 < col2 ? col2 : col1;
for(int col = min+1; col<max; ++col)
{
if(getStoneId(row1, col) != -1)
++ret;
}
}
else
{
int min = row1 < row2 ? row1 : row2;
int max = row1 < row2 ? row2 : row1;
for(int row = min+1; row<max; ++row)
{
if(getStoneId(row, col1) != -1)
++ret;
}
}
return ret;
}
void ChessBoard::whoWin() //谁胜谁负
{
if(m_ChessPieces[4].m_bDead && !m_ChessPieces[20].m_bDead)
{
reset();
winMessageBox("提示", "本局结束,红方胜利.");
}
if(!m_ChessPieces[4].m_bDead && m_ChessPieces[20].m_bDead)
{
reset();
winMessageBox("提示", "本局结束,黑方胜利.");
}
}
int ChessBoard:: relation(int row1,int col1,int row2,int col2)
{
// 原坐标(row1,col1)与目标坐标(row2,col2)的关系
return abs(row1-row2)*10+ abs(col1-col2);
}
//是否选中该枚棋子。pt为输入参数; row, col为输出参数
bool ChessBoard::isChecked(QPoint pt, int &row, int &col)
{
for(row = 0; row <= 9; row++)
{
for(col = 0; col <= 8; col++)
{
QPoint temp = center(row, col);
int x = temp.x()-pt.x();
int y = temp.y()-pt.y();
if(x*x+y*y < m_nR*m_nR)
return true;
}
}
return false;
}
//象棋的棋盘的坐标转换成界面坐标
QPoint ChessBoard::center(int row, int col)
{
QPoint rePoint;
//这里注意坐标的转换
rePoint.ry() = row*m_nD+m_nOffSet;
rePoint.rx() = col*m_nD+m_nOffSet;
return rePoint;
}
//重载:坐标转换
QPoint ChessBoard::center(int id)
{
return center(m_ChessPieces[id].m_nRow, m_ChessPieces[id].m_nCol);
}
void ChessBoard::paintEvent(QPaintEvent *)
{
QPainter painter(this);
int side = qMin(int((ui->centralwidget->width() - ui->verticalWidget->width()) / 0.9), ui->label->height());
painter.scale(side / 960.0, side / 960.0);
m_nOffSet = 60; //距离界面的边距
m_nD = 90; //间距为50px
m_nR = m_nD/2; //棋子半径为d/2
//绘画棋盘
//绘画10条横线
for(int i = 0; i <= 9; i++)
painter.drawLine(QPoint(m_nOffSet, m_nOffSet+i*m_nD), QPoint(m_nOffSet+8*m_nD, m_nOffSet+i*m_nD));
//绘画9条竖线
for(int i = 0; i <= 8; i++)
{
if(i==0 || i==8)
{
painter.drawLine(QPoint(m_nOffSet+i*m_nD, m_nOffSet), QPoint(m_nOffSet+i*m_nD, m_nOffSet+9*m_nD));
}
else
{
painter.drawLine(QPoint(m_nOffSet+i*m_nD, m_nOffSet), QPoint(m_nOffSet+i*m_nD, m_nOffSet+4*m_nD));
painter.drawLine(QPoint(m_nOffSet+i*m_nD, m_nOffSet+5*m_nD), QPoint(m_nOffSet+i*m_nD, m_nOffSet+9*m_nD));
}
}
//绘画4条斜线
painter.drawLine(QPoint(m_nOffSet+3*m_nD, m_nOffSet), QPoint(m_nOffSet+5*m_nD, m_nOffSet+2*m_nD));
painter.drawLine(QPoint(m_nOffSet+3*m_nD, m_nOffSet+2*m_nD), QPoint(m_nOffSet+5*m_nD, m_nOffSet));
painter.drawLine(QPoint(m_nOffSet+3*m_nD, m_nOffSet+7*m_nD), QPoint(m_nOffSet+5*m_nD, m_nOffSet+9*m_nD));
painter.drawLine(QPoint(m_nOffSet+3*m_nD, m_nOffSet+9*m_nD), QPoint(m_nOffSet+5*m_nD, m_nOffSet+7*m_nD));
QRect rect1(m_nOffSet+m_nD, m_nOffSet+4*m_nD, m_nD, m_nD);
QRect rect2(m_nOffSet+2*m_nD, m_nOffSet+4*m_nD, m_nD, m_nD);
QRect rect3(m_nOffSet+5*m_nD, m_nOffSet+4*m_nD, m_nD, m_nD);
QRect rect4(m_nOffSet+6*m_nD, m_nOffSet+4*m_nD, m_nD, m_nD);
painter.setFont(QFont("隶书", m_nR, 800));
painter.drawText(rect1, "楚", QTextOption(Qt::AlignCenter));
painter.drawText(rect2, "河", QTextOption(Qt::AlignCenter));
painter.drawText(rect3, "汉", QTextOption(Qt::AlignCenter));
painter.drawText(rect4, "界", QTextOption(Qt::AlignCenter));
//绘画棋子
for(int i = 0; i < 32; i++)
drawChessPieces(painter, i);
//绘制上次移动棋子的起止位置
if(m_bIsShowStep)
drawLastStep(painter,m_ChessSteps);
}
void ChessBoard::drawChessPieces(QPainter &painter, int id) //绘画单个具体的棋子
{
if(m_ChessPieces[id].m_bDead)
return;
QPoint temp = center(id);
QRect rect(temp.x()-m_nR, temp.y()-m_nR, m_nD, m_nD);
if(m_nSelectID == id)
painter.setBrush(QBrush(QColor(64,64,196, 80)));
else
painter.setBrush(QBrush(QColor(64,64,196, 10)));
painter.setPen(QColor(0, 0, 0));
painter.drawEllipse(center(id), m_nR, m_nR); //绘画圆形
painter.setFont(QFont("华文楷体", m_nR, 700));
if(id < 16)
painter.setPen(QColor(0, 0, 0));
else
painter.setPen(QColor(255, 0, 0));
painter.drawText(rect, m_ChessPieces[id].getnName(m_ChessPieces[id].m_bRed), QTextOption(Qt::AlignCenter)); //绘画圆形里面的汉字
}
void ChessBoard:: drawLastStep(QPainter &painter,QVector<ChessStep*>& steps)
{
if (this->m_ChessSteps.size() == 0) {
return;
}
QPoint stepFrom = center(steps.last()->m_nRowFrom,steps.last()->m_nColFrom);
QRect rectFrom(stepFrom.x()-m_nR, stepFrom.y()-m_nR, m_nD, m_nD);
painter.setBrush(QColor(0,0,0,50));
painter.setPen(Qt::black);
painter.drawRect(rectFrom);
QPoint stepTo = center(st
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
中国象棋是起源于中国的一种棋,属于二人对抗性游戏的一种,在中国有着悠久的历史。由于用具简单,趣味性强,成为流行极为广泛的棋艺活动。中国象棋是中国棋文化,也是中华民族的文化瑰宝,它源远流长,趣味浓厚,基本规则简明易懂。中国象棋在中国的群众中基础远远超过围棋,是普及最广的棋类项目,中国象棋已流传到十几个国家和地区。本项目主要目的为实现中国传统双人对弈游戏—中国象棋,实现了在单机或联网状态下,无论是单人还是多人,无论使用系统是否相同,均可以实现象棋游戏功能。开发全程使用 Qt Creator 4.3.0 (Community) 的 C/C++功能,基本不使用控件拖拽,并且尽量手写代码来实现相应的函数功能,以此来熟悉面向对象编程,提高手写代码能力。通过此项目,不仅自己将取得编程技术上的进步,更是对中国传统文化的一种宣扬和推广。
资源推荐
资源详情
资源评论
收起资源包目录
ChineseChess.zip (24个子文件)
ChineseChess
images
sky.jpg 159KB
chess.jfif 47KB
win.jfif 16KB
sky1.jpeg 34KB
back.jfif 11KB
5abc.png 393KB
ChessStep.h 478B
res.qrc 292B
ChooseMainWindow.cpp 3KB
ChessBoard.h 4KB
logindialog.ui 6KB
logindialog.h 401B
ChessPieces.h 545B
ChessBoard.cpp 19KB
logindialog.cpp 1KB
ChessBoard.ui 8KB
main.cpp 325B
ChineseChess.pro 519B
ChineseChess.pro.user 23KB
ChessPieces.cpp 2KB
MachineGame.cpp 8KB
ChessStep.cpp 124B
ChooseMainWindow.h 485B
MachineGame.h 1KB
共 24 条
- 1
资源评论
zhugenmi
- 粉丝: 145
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功