#include "board.h"
board::board(QWidget *parent, bool mainRed) : QWidget(parent), myRed(mainRed)
{
this->setGeometry(0, 30, 1400, 920);
_id = 0;
d = 82;
r = d/2;
tag = true;
saveFirstPoint = QPoint(0,0);
saveSecondPoint = QPoint(0,0);
for(int i = 1; i <= 32; i++)
{
s[i].init(i, myRed);
}
this->show();
}
void board::paintEvent(QPaintEvent *)
{
QPainter * painter = new QPainter(this);
QPen pen;
pen.setWidth(3); //设置画笔粗细
painter->setPen(pen);
//画10条横线
for (int i = 1; i <= 10; i++)
painter->drawLine(QPoint(d, d*i), QPoint(d*9, d*i));//9条竖线
//画两边界竖线
painter->drawLine(QPoint(d, d), QPoint(d, d*10));
painter->drawLine(QPoint(d*9, d), QPoint(d*9, d*10));
//画中间7条竖线
for (int i = 2; i < 9; i++) {
painter->drawLine(QPoint(d*i, d), QPoint(d*i, d*5));
painter->drawLine(QPoint(d*i, d*6), QPoint(d*i, d*10));
}
//画九宫斜线
painter->drawLine(QPoint(d*4, d), QPoint(d*6, d*3));
painter->drawLine(QPoint(d*4, d*3), QPoint(d*6, d));
painter->drawLine(QPoint(d*4, d*8), QPoint(d*6, d*10));
painter->drawLine(QPoint(d*4, d*10), QPoint(d*6, d*8));
// 绘制棋子
QPoint p;
for(int i = 1; i <= 32; i++)
{
if(!s[i].dead)
{
p = center(s[i].id);
if (s[i].red)
{
if(i == _id)
{
painter->setBrush(QBrush(QColor(255,0,0)));
painter->setFont(QFont("宋体", r * 0.9, 3));
painter->drawEllipse(p, r, r);
painter->drawText(QRect(p.x() - r, p.y() - r, d, d), s[i].getText(), QTextOption(Qt::AlignCenter));
}
else
{
painter->setBrush(QBrush("#F10C45"));
painter->setFont(QFont("宋体", r * 0.7, 3));
painter->drawEllipse(p, r, r);
painter->drawText(QRect(p.x() - r, p.y() - r, d, d), s[i].getText(), QTextOption(Qt::AlignCenter));
}
}
else
{
if(i == _id)
{
painter->setBrush(QBrush(QColor(205,205,205)));
painter->setFont(QFont("宋体", r * 0.9, 3));
painter->drawEllipse(center(s[i].id), r, r);
painter->drawText(QRect(p.x() - r, p.y() - r, d, d), s[i].getText(), QTextOption(Qt::AlignCenter));
}
else
{
painter->setBrush(QBrush(QColor(255,255,255)));
painter->setFont(QFont("宋体", r * 0.7, 3));
painter->drawEllipse(center(s[i].id), r, r);
painter->drawText(QRect(p.x() - r, p.y() - r, d, d), s[i].getText(), QTextOption(Qt::AlignCenter));
}
}
}
}
painter->end();
}
void board::mouseReleaseEvent(QMouseEvent *ev)
{
QPoint pt = ev->pos();
if(tag)
moveChess(pt);
}
void board::moveChess(QPoint pt)
{
int row = 0;
int col = 0;
bool bRet = false;
saveFirstPoint = saveSecondPoint;
bRet = getRowCol(pt, row, col); // 返回point
updateId(row, col);
if(bRet)
{
if(_id)
{
saveSecondPoint = QPoint(s[_id].row, s[_id].col);
}
else
{
saveSecondPoint = QPoint(row, col);
}
}
else
{
saveSecondPoint = QPoint(0,0);
}
int temp = pointToId(saveFirstPoint);
int temp2 = pointToId(saveSecondPoint);
if(temp && s[temp].red == myRed && temp != -1)
{
if(temp2 == 0)
{
if(rule(temp))
{
s[temp].row = saveSecondPoint.x();
s[temp].col = saveSecondPoint.y();
saveSecondPoint = QPoint(0,0);
qDebug() << "移动" << temp << temp2;
emit EmitMoveChess(temp, s[temp].row ,s[temp].col);
}
}else if(temp2 != temp && s[temp2].red != myRed && temp2 != -1)
{
if(rule(temp))
{
s[temp2].dead = true;
s[temp].row = saveSecondPoint.x();
s[temp].col = saveSecondPoint.y();
saveSecondPoint = QPoint(0,0);
qDebug() << "吃棋" << temp << temp2;
emit EmitMoveChess(temp, s[temp].row ,s[temp].col);
}
}
}
update();
}
bool board::getRowCol(QPoint pt, int &row, int &col)
{
for(row = 1; row <= 10; row++)
{
for(col = 1; col <= 9; 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;
}
void board::updateId(int row, int col)
{
for(int i = 1; i <= 32; i++)
{
if(s[i].row == row && s[i].col == col && s[i].dead == false)
{
_id = i;
break;
}
else
{
_id = 0;
}
}
}
int board::pointToId(QPoint p)
{
int i = 1;
if(p == QPoint(0,0)) return -1;
for(; i <= 32; i++)
{
if(s[i].row == p.x() && s[i].col == p.y() && s[i].dead == false)
{
break;
}
}
if(i > 32) return 0;
else return i;
}
<<<<<<< HEAD
void board::drawingBoard(bool tmp)
{
if(tmp)
{
for(int i = 1; i <= 32; i++)
{
s[i].red = !s[i].red;
}
myRed = !myRed;
update();
}
}
void board::rivalMobile(QString str)
{
int pawnID = str.mid(0,2).toUInt();
int x = str.mid(2,2).toUInt();
int y = str.mid(4,2).toUInt();
for(int i = 1 ; i <= 32; i++)
{
if(s[i].row == x && s[i].col == y) s[i].dead = true;
}
qDebug() << "棋子移动" << str;
s[pawnID].row = x;
s[pawnID].col = y;
tag = !tag;
update();
if(s[21].dead == true) emit gameOver("11");
=======
void board::setMainred(bool b)
{
myRed = b;
>>>>>>> da6ad27a8e86736717b61087ca6698836ff847d9
}
QPoint board::center(int row, int col)
{
return QPoint(col * d, row * d);
}
QPoint board::center(int id)
{
return center(s[id].row, s[id].col);
}
bool board::rule(int FirstPointId)
{
switch(s[FirstPointId].type)
{
case Stone::KING: return king();
case Stone::CHE:return che();
case Stone::MA:return ma();
case Stone::PAO:return pao();
case Stone::XIANG:return xiang();
case Stone::SHI:return shi();
case Stone::BING:return bing();
default:
return false;
}
}
bool board::king()
{
int num = 1;
if(! ((saveSecondPoint.x() == saveFirstPoint.x()+num && saveSecondPoint.y() == saveFirstPoint.y()) ||
(saveSecondPoint.x() == saveFirstPoint.x()-num && saveSecondPoint.y() == saveFirstPoint.y()) ||
(saveSecondPoint.y() == saveFirstPoint.y()+num && saveSecondPoint.x() == saveFirstPoint.x()) ||
(saveSecondPoint.y() == saveFirstPoint.y()-num && saveSecondPoint.x() == saveFirstPoint.x())
))
{
qDebug() << "不在四周!(" << saveFirstPoint.x() << saveFirstPoint.y() << ")(" << saveSecondPoint.x() << saveSecondPoint.y() << ")";
return false;
}
if((saveSecondPoint.x() < 8 || saveSecondPoint.x() > 10) || (saveSecondPoint.y() < 4 || saveSecondPoint.y() > 6))
{
qDebug() << "不在圈内!";
qDebug() << saveSecondPoint.x() << saveSecondPoint.y();
return false;
}
return true;
}
bool board::che()
{
if(saveSecondPoint.x() != saveFirstPoint.x() && saveSecondPoint.y() == saveFirstPoint.y())
{
int i = 1;
for(; i <= 32; i++)
{
if(s[i].dead == true || (s[i].row == saveFirstPoint.x() && s[i].col == saveFirstPoint.y()))
{
墨痕_777
- 粉丝: 4750
- 资源: 776
最新资源
- 毕业设计-ASP.NET学生成绩管理系统(论文+系统).rar
- 毕业设计-asp.net选课系统毕业设计全套参考资料.rar
- 毕业设计-asp.net学生成绩管理系统(源代码+论文).rar
- 毕业设计-ASP.NET学生信息管理系统(源代码+论文).rar
- 毕业设计-asp.net学生管理系统(源代码+论文).rar
- EC20的 QMI-WWAN 驱动源文件 T113亲测有效
- 毕业设计-asp.net学生信息系统【适合课程设计】.rar
- 毕业设计-ASP.NET一个简单的媒体播放器的设计与实现(源代码+论文).rar
- 毕业设计-asp.net一种多商家网络商店的设计与实现(源代码+论文).rar
- 毕业设计-asp.net银行大厅自助信息系统的开发与实现(源代码+论文).rar
- 毕业设计-asp.net仪器设备管理系统设计与实现(源代码+论文).rar
- 食品冷链行业物流与供应链数字化转型白皮书.pdf
- 中国银行-全球经济金融展望报告2025年年报-总第61期.pdf
- 毕业设计-asp.net医药进销存系统.rar
- 工业供应链数字化白皮书+(2024).pdf
- 量子计算与人工智能:无声的革命.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈