#include "mainplaywidget.h"
#include "ui_mainplaywidget.h"
#include <QPainter>
#include <QPoint>
#include <QDebug>
#include <QMessageBox>
#include <QMouseEvent>
#include <QTimer>
#include <QTime>
//#pragma execution_character_set("utf-8")
MainPlayWidget::MainPlayWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::MainPlayWidget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
InitParams();
m_timer = new QTimer(this); //初始化定时器
m_timeRecord = new QTime(0, 0, 0); //初始化时间
connect(m_timer,SIGNAL(timeout()),this,SLOT(updateTime()));
m_timer->start(1000);
}
MainPlayWidget::~MainPlayWidget()
{
delete ui;
}
void MainPlayWidget::SetWindowTitle(const QString &strTitle)
{
ui->label_logotext->setText(strTitle);
}
void MainPlayWidget::InitParams()
{
for(int i = 0; i<32; i++)
{
m_ChessPieces[i].init(i);
}
m_nSelectID = -1;
m_nCheckedID = -1;
m_nTcpServer = 1;
m_nSide = 1;
m_bIsOver = false;
QPalette palet;
palet.setColor(QPalette::WindowText,Qt::green);
ui->label_fightstatus->setPalette(palet);
ui->label_fightstatus->setText("正在对弈.....");
}
void MainPlayWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::RightButton)
{
//移到左上角
move(event->globalPos() - m_point);
}
}
QPoint MainPlayWidget::Transform(int row, int col)
{
QPoint rePoint;
//这里注意坐标的转换
rePoint.ry() = row*m_nDistance+m_nVOffSet;
rePoint.rx() = col*m_nDistance+m_nHOffSet;
return rePoint;
}
QPoint MainPlayWidget::Transform(int id)
{
return Transform(m_ChessPieces[id].m_nRow, m_ChessPieces[id].m_nCol);
}
QPoint MainPlayWidget::GetRealPostion(const QPoint& pt)
{
double dwScale = rect().width()/800.0;
double dhScale = rect().height()/800.0;
double dScale = qMin(dwScale,dhScale);
QPoint realPt;
realPt.setX(pt.x() / dScale);
realPt.setY(pt.y() / dScale);
return realPt;
}
bool MainPlayWidget::GetSelectStatus(const QPoint &pt, int &row, int &col)
{
for(row = 0; row <= 9; row++)
{
for(col = 0; col <= 8; col++)
{
QPoint temp = Transform(row, col);
int x = temp.x()-pt.x();
int y = temp.y()-pt.y();
// 棋子圆形范围内即为选中
if(x*x+y*y < m_nRadio*m_nRadio)
{
return true;
}
}
}
return false;
}
bool MainPlayWidget::GetDeadStatus(int id)
{
if(id == -1)
{
return true;
}
return m_ChessPieces[id].m_bDead;
}
int MainPlayWidget::GetChessId(int row, int col)
{
for(int i = 0; i < 32; ++i)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && !GetDeadStatus(i))
{
return i;
}
}
return -1;
}
int MainPlayWidget::getChessCountAtLine(int row1, int col1, int row2, int col2)
{
int nCount = 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(GetChessId(row1, col) != -1)
{
++nCount;
}
}
}
else
{
int min = row1 < row2 ? row1 : row2;
int max = row1 < row2 ? row2 : row1;
for(int row = min+1; row<max; ++row)
{
if(GetChessId(row, col1) != -1)
{
++nCount;
}
}
}
return nCount;
}
void MainPlayWidget::mousePressEvent(QMouseEvent* ev)
{
// 右键移动窗口使用
if(ev->button() == Qt::RightButton)
{
//求坐标差值:当前点击坐标-窗口左上角坐标
m_point = ev->globalPos() - this->frameGeometry().topLeft();
}
//只响应鼠标左键的单击操作 防止游戏结束重复弹框
if(ev->button() != Qt::LeftButton || ev->type() != QEvent::Type::MouseButtonPress)
{
return;
}
QPoint pt = ev->pos();
pt = GetRealPostion(pt);
//将pt转化成棋盘的像行列值,判断这个行列值上面有没有棋子
int row = -1;
int col = -1;
//未选中棋子不做处理
if(!GetSelectStatus(pt, row, col))
{
return;
}
if(m_bIsOver)
{
QMessageBox message(QMessageBox::Information, "提示", "本局已结束,请重新开始.");
message.setIconPixmap(QPixmap(":/images/win.jpg"));
message.setFont(QFont("华文行楷",16,QFont::Bold));
message.exec();
return;
}
//判断是哪一个棋子被选中,记录棋子ID
m_nCheckedID = -1;
for(int i = 0; i <= 31; i++)
{
if(m_ChessPieces[i].m_nRow == row && m_ChessPieces[i].m_nCol == col && m_ChessPieces[i].m_bDead == false)
{
m_nCheckedID = i;
break;
}
}
clickPieces(m_nCheckedID, row, col);
update();
GetGameOverStatus();
}
void MainPlayWidget::clickPieces(int checkedID, int &row, int &col)
{
m_nCheckedID = checkedID;
if(m_nSelectID == -1)
{
if(m_nCheckedID != -1)
{
if(m_nSide == m_ChessPieces[m_nCheckedID].m_nSide)
{
m_nSelectID = m_nCheckedID;
}
}
}
else//移动棋子或换一个同色其中进行选中
{
if(canMove(m_nSelectID, m_nCheckedID, row, col ))
{
//m_nSelectID为第一次点击选中的棋子,
//m_nCheckedID为第二次点击||被杀的棋子ID||准备选中棋子下子的地方
m_ChessPieces[m_nSelectID].m_nRow = row;
m_ChessPieces[m_nSelectID].m_nCol = col;
if(m_nCheckedID != -1)
{
m_ChessPieces[m_nCheckedID].m_bDead = true;
}
m_nSelectID = -1;
m_nSide = abs(m_nSide-1);
if(m_nSide == 1)
{
ui->label_bluestatus->setText("选手状态:操作完成");
ui->label_redstatus->setText("选手状态:正在思考...");
}
else
{
ui->label_redstatus->setText("选手状态:操作完成");
ui->label_bluestatus->setText("选手状态:正在思考...");
}
}
}
}
bool MainPlayWidget::canMove(int moveId, int killId, int row, int col)
{
// 在同色棋子之间选择:处于思考状态,在考虑选哪个棋子进行移动
if(killId != -1)
{
if(m_ChessPieces[moveId].m_nSide == m_ChessPieces[killId].m_nSide)
{
m_nSelectID = killId;
return false;
}
}
//移动选中棋子至空白处或吃掉对方棋子
switch (m_ChessPieces[moveId].m_emType)
{
case ChessPieces::JIANG:
return canMoveJIANG(moveId, row, col);
case ChessPieces::SHI:
return canMoveSHI(moveId, row, col);
case ChessPieces::XIANG:
return canMoveXIANG(moveId, row, col);
case ChessPieces::MA:
return canMoveMA(moveId, row, col);
case ChessPieces::CHE:
return canMoveCHE(moveId, row, col);
case ChessPieces::PAO:
return canMovePAO(moveId, killId, row, col);
case ChessPieces::BING:
return canMoveBING(moveId, row, col);
}
return true;
}
bool MainPlayWidget::canMoveJIANG(int moveId, int row, int col)
{
if(m_ChessPieces[moveId].m_nSide == 1) //红 将
{
if(row < 7 || col < 3 || co
没有合适的资源?快使用搜索试试~ 我知道了~
Qt版本中国象棋:实现《双人对战》、《人机对战》、《网络对战》三种模式。
共34个文件
cpp:9个
h:8个
png:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 9 下载量 39 浏览量
2022-07-04
16:07:22
上传
评论 2
收藏 22.18MB ZIP 举报
温馨提示
纯Qt版本中国象棋,利用QPainter和QSS实现象棋界面绘制,包含《双人对战》、《人机对战》、《网络对战》三种对战模式。其中网络对战使用QTcpServer和QTcpSocket来实现网络连接和数据传输;人机对战模式则采用的穷举评估算法来设计电脑的棋力,可达到一般人水平。 作为Qt入门学习或作为学生作品均可使用。 其中包含Qt源码、Qt可执行软件包。 详情参考:https://blog.csdn.net/m0_37251750/article/details/125602137?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22125602137%22%2C%22source%22%3A%22m0_37251750%22%7D&ctrtid=Q4fXi
资源推荐
资源详情
资源评论
收起资源包目录
中国象棋.zip (34个子文件)
01.png 80KB
体验软件包.zip 20.67MB
02.png 92KB
ChineseChessGame
gamemodeoptions.cpp 2KB
NetworkGame.cpp 4KB
images
chess.svg 15KB
welcome.png 511KB
mainclose.png 36KB
background.png 357KB
mainUI.png 491KB
win.jpg 2KB
close.png 12KB
gamemodeoptions.ui 4KB
ChessStep.h 481B
chess.ico 17KB
gamemodeoptions.h 749B
mainplaywidget.ui 7KB
NetworkGame.h 879B
src.qrc 344B
mainplaywidget.h 3KB
netsetdialog.ui 4KB
ChessPieces.h 578B
ChineseChessGame.pro.user 22KB
ChineseChessGame.pro 2KB
main.cpp 383B
choosegamemode.cpp 2KB
choosegamemode.h 504B
netsetdialog.cpp 2KB
netsetdialog.h 772B
ChessPieces.cpp 2KB
MachineGame.cpp 12KB
mainplaywidget.cpp 22KB
ChessStep.cpp 173B
MachineGame.h 1KB
共 34 条
- 1
欧特克_Glodon
- 粉丝: 4w+
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 王姿.html
- 51单片机学习(1)-软件keil下载
- 历届(第1-21届)希望杯数学竞赛初一试题及答案(最新整理).doc全国数学邀请赛(264页资料)
- 水滴.psd
- TokenPocket_V2.1.2_release.apk
- Apache-druid-kafka-rce.yaml
- 基于C#的ASP.NET数据库原理及应用技术课程指导平台的开发
- 基于ROS的智能车轨迹跟踪算法的仿真与设计源码运用PID跟踪算法.zip.zip
- Bug Bounty Tip - i春秋Self-XSS变废为宝的奇思妙想
- 1991-2015年全国初中化学竞赛复赛试题汇编(212页)(24年竞赛复赛真题).docx天原杯
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页