style: refactor

This commit is contained in:
CalciteM Team 2019-06-28 23:20:23 +08:00
parent 88fded38d8
commit 650c694ca5
9 changed files with 76 additions and 41 deletions

View File

@ -342,7 +342,7 @@ void GameController::flip()
if (currentRow == row - 1)
updateScence();
else
phaseChange(currentRow, true);
stageChange(currentRow, true);
ai1.setAi(chess_);
ai2.setAi(chess_);
@ -350,6 +350,7 @@ void GameController::flip()
if (isEngine1) {
ai1.start();
}
if (isEngine2) {
ai2.start();
}
@ -383,7 +384,7 @@ void GameController::mirror()
if (currentRow == row - 1)
updateScence();
else
phaseChange(currentRow, true);
stageChange(currentRow, true);
ai1.setAi(chess_);
ai2.setAi(chess_);
@ -423,7 +424,7 @@ void GameController::turnRight()
if (currentRow == row - 1)
updateScence();
else
phaseChange(currentRow, true);
stageChange(currentRow, true);
ai1.setAi(chess_);
ai2.setAi(chess_);
@ -819,7 +820,7 @@ bool GameController::command(const QString &cmd, bool update /*= true*/)
}
// 浏览历史局面通过command函数刷新局面显示
bool GameController::phaseChange(int row, bool forceUpdate)
bool GameController::stageChange(int row, bool forceUpdate)
{
// 如果row是当前浏览的棋谱行则不需要刷新
if (currentRow == row && !forceUpdate)
@ -841,6 +842,7 @@ bool GameController::phaseChange(int row, bool forceUpdate)
// 刷新棋局场景
updateScence(chessTemp);
return true;
}
@ -902,9 +904,12 @@ bool GameController::updateScence(NineChess &chess)
if (j == (NineChess::N_SEATS) * (NineChess::N_RINGS + 1)) {
// 判断是被吃掉的子,还是未安放的子
if (key & 0x10) {
pos = (key - 0x11 < n / 2 - chess.getPiecesInHandCount_1()) ? scene.pos_p2_g : scene.pos_p1;
} else
pos = (key - 0x21 < n / 2 - chess.getPiecesInHandCount_2()) ? scene.pos_p1_g : scene.pos_p2;
pos = (key - 0x11 < n / 2 - chess.getPiecesInHandCount_1()) ?
scene.pos_p2_g : scene.pos_p1;
} else {
pos = (key - 0x21 < n / 2 - chess.getPiecesInHandCount_2()) ?
scene.pos_p1_g : scene.pos_p2;
}
if (piece->pos() != pos) {
QPropertyAnimation *animation = new QPropertyAnimation(piece, "pos");
@ -915,10 +920,11 @@ bool GameController::updateScence(NineChess &chess)
animationGroup->addAnimation(animation);
}
}
piece->setSelected(false);
}
// 添加开局禁子点
// 添加摆棋阶段禁子点
if (chess.getRule()->hasForbiddenPoint && chess.getStage() == NineChess::GAME_PLACING) {
for (int j = NineChess::POS_BEGIN; j < NineChess::POS_END; j++) {
if (board[j] == 0x0F) {
@ -937,7 +943,7 @@ bool GameController::updateScence(NineChess &chess)
}
}
// 中局清除禁子点
// 走棋阶段清除禁子点
if (chess.getRule()->hasForbiddenPoint && chess.getStage() != NineChess::GAME_PLACING) {
while (n < pieceList.size()) {
delete pieceList.at(n);

View File

@ -135,7 +135,7 @@ public slots:
bool command(const QString &cmd, bool update = true);
// 历史局面及局面改变
bool phaseChange(int row, bool forceUpdate = false);
bool stageChange(int row, bool forceUpdate = false);
// 更新棋局显示,每步后执行才能刷新局面
bool updateScence();

View File

@ -1,13 +1,14 @@
#include "gamescene.h"
#include "pieceitem.h"
#include "boarditem.h"
#include "graphicsconst.h"
#include <QGraphicsItem>
#include <QGraphicsItem>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QDebug>
#include "gamescene.h"
#include "pieceitem.h"
#include "boarditem.h"
#include "graphicsconst.h"
GameScene::GameScene(QObject *parent) :
QGraphicsScene(parent),
board(nullptr),
@ -76,6 +77,7 @@ void GameScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
// 如果是棋盘
QGraphicsItem *item = itemAt(mouseEvent->scenePos(), QTransform());
if (!item || item->type() == BoardItem::Type) {
QPointF p = mouseEvent->scenePos();
p = board->nearestPosition(p);

View File

@ -85,7 +85,8 @@ void GameView::turnLeft()
void GameView::resizeEvent(QResizeEvent *event)
{
/* 不使用下面的形式了
#if 0
// 不使用下面的形式了
// 让场景适合视图
if (sceneRect().width() <= 0 || sceneRect().height() <= 0)
return;
@ -99,7 +100,7 @@ void GameView::resizeEvent(QResizeEvent *event)
// 缩放视图适合场景大小
scale(sx, sy);
//qDebug() << "scale :" << sx;
*/
#endif
// 使用如下形式,更简洁
QGraphicsView::resizeEvent(event);

View File

@ -2,11 +2,11 @@
#ifndef GRAPHICSCONST
#define GRAPHICSCONST
const int BOARD_SIZE = 600; // 棋盘大小
const int BOARD_MINISIZE = 150; // 最小宽高即1/4大小
const int PIECE_SIZE = 56; // 棋子大小
const int LINE_INTERVAL = 72; // 线间距
const int LINE_WEIGHT = 3; // 线宽
const short BOARD_SIZE = 600; // 棋盘大小
const short BOARD_MINISIZE = 150; // 最小宽高即1/4大小
const short PIECE_SIZE = 56; // 棋子大小
const short LINE_INTERVAL = 72; // 线间距
const short LINE_WEIGHT = 3; // 线宽
#endif // GRAPHICSCONST

View File

@ -148,6 +148,7 @@ const NineChess &NineChess::operator=(const NineChess &chess)
{
if (this == &chess)
return *this;
currentRule = chess.currentRule;
context = chess.context;
currentStep = chess.currentStep;
@ -512,14 +513,14 @@ bool NineChess::reset()
bool NineChess::start()
{
switch (context.stage) {
// 如果游戏已经开始则返回false
// 如果游戏已经开始则返回false
case GAME_PLACING:
case GAME_MOVING:
return false;
// 如果游戏结束,则重置游戏,进入未开始状态
// 如果游戏结束,则重置游戏,进入未开始状态
case GAME_OVER:
reset(); // 这里不要break;
// 如果游戏处于未开始状态
// 如果游戏处于未开始状态
case GAME_NOTSTARTED:
// 启动计时器
ftime(&startTimeb);
@ -610,6 +611,7 @@ bool NineChess::place(int c, int p, long time_p /* = -1*/)
// 如果落子位置在棋盘外、已有子点或禁点返回false
int pos = cp2pos(c, p);
if (!onBoard[pos] || board_[pos])
return false;
@ -704,13 +706,14 @@ bool NineChess::place(int c, int p, long time_p /* = -1*/)
(context.nPiecesOnBoard_1 > currentRule.nPiecesAtLeast || !currentRule.allowFlyWhenRemainThreePieces)) ||
(context.turn == PLAYER2 &&
(context.nPiecesOnBoard_2 > currentRule.nPiecesAtLeast || !currentRule.allowFlyWhenRemainThreePieces))) {
int i;
for (i = 0; i < 4; i++) {
if (pos == moveTable[currentPos][i])
break;
}
// 不在招法表中
// 不在招法表中
if (i == 4)
return false;
}
@ -1116,6 +1119,7 @@ bool NineChess::capture(int pos)
//setTip();
return true;
}
// 还有其余的子要去吗
if (context.nPiecesNeedRemove > 0) {
// 继续去子
@ -1175,6 +1179,7 @@ bool NineChess::capture(int pos)
}
}
}
//setTip();
return true;
}
@ -1206,6 +1211,7 @@ bool NineChess::choose(int pos)
return true;
}
return false;
}
@ -1260,6 +1266,7 @@ bool NineChess::giveup(Player loser)
return true;
}
}
return false;
}
@ -1569,17 +1576,20 @@ int NineChess::addMills(int pos)
}
}
}
return n;
}
bool NineChess::isAllInMills(char ch)
{
for (int i = N_SEATS; i < N_SEATS * (N_RINGS + 1); i++)
for (int i = POS_BEGIN; i < POS_END; i++) {
if (board_[i] & ch) {
if (!isInMills(i)) {
return false;
}
}
}
return true;
}
@ -1626,8 +1636,10 @@ bool NineChess::isAllSurrounded(char ch)
return true;
// 判断是否可以飞子
if ((context.turn == PLAYER1 && (context.nPiecesOnBoard_1 <= currentRule.nPiecesAtLeast && currentRule.allowFlyWhenRemainThreePieces)) ||
(context.turn == PLAYER2 && (context.nPiecesOnBoard_2 <= currentRule.nPiecesAtLeast && currentRule.allowFlyWhenRemainThreePieces))) {
if ((context.turn == PLAYER1 &&
(context.nPiecesOnBoard_1 <= currentRule.nPiecesAtLeast && currentRule.allowFlyWhenRemainThreePieces)) ||
(context.turn == PLAYER2 &&
(context.nPiecesOnBoard_2 <= currentRule.nPiecesAtLeast && currentRule.allowFlyWhenRemainThreePieces))) {
return false;
}

View File

@ -596,12 +596,12 @@ void NineChessWindow::on_actionRowChange()
}
// 更新局面
game->phaseChange(currentRow);
game->stageChange(currentRow);
#if 0
// 下面的代码全部取消改用QTimer的方式实现
// 更新局面
bool changed = game->phaseChange(currentRow);
bool changed = game->stageChange(currentRow);
// 处理自动播放时的动画
if (changed && game->isAnimation()) {
// 不使用processEvents函数进行非阻塞延时频繁调用占用CPU较多
@ -661,7 +661,7 @@ void NineChessWindow::onAutoRunTimeOut(QPrivateSignal signal)
}
// 更新局面
game->phaseChange(currentRow);
game->stageChange(currentRow);
} else {
ui.actionAutoRun_A->setChecked(false);
}

View File

@ -70,20 +70,27 @@ void PieceItem::paint(QPainter *painter,
Q_UNUSED(option)
Q_UNUSED(widget)
// 空模型不画棋子
// 空模型不画棋子
switch (model_) {
case blackPiece:
// 如果模型为黑色,则画黑色棋子
if (model_ == blackPiece)
painter->drawPixmap(-size / 2, -size / 2, size, size,
QPixmap(":/image/resources/image/black_piece.png"));
painter->drawPixmap(-size / 2, -size / 2, size, size,
QPixmap(":/image/resources/image/black_piece.png"));
break;
case whitePiece:
// 如果模型为白色,则画白色棋子
else if (model_ == whitePiece)
painter->drawPixmap(-size / 2, -size / 2, size, size,
QPixmap(":/image/resources/image/white_piece.png"));
painter->drawPixmap(-size / 2, -size / 2, size, size,
QPixmap(":/image/resources/image/white_piece.png"));
break;
}
// 如果模型要求显示序号
if (showNum) {
// 如果模型为黑色,用白色笔画序号
painter->setPen(QColor(255, 255, 255));
if (model_ == blackPiece)
painter->setPen(QColor(255, 255, 255));
// 如果模型为白色,用白色笔画序号
if (model_ == whitePiece)
@ -96,7 +103,8 @@ void PieceItem::paint(QPainter *painter,
painter->setFont(font);
// 画序号,默认中间位置偏下,需微调
painter->drawText(boundingRect().adjusted(0, 0, 0, -size / 12), Qt::AlignCenter, QString::number(num));
painter->drawText(boundingRect().adjusted(0, 0, 0, -size / 12),
Qt::AlignCenter, QString::number(num));
}

View File

@ -16,9 +16,13 @@ class PieceItem : public QObject, public QGraphicsItem
public:
explicit PieceItem(QGraphicsItem *parent = nullptr);
~PieceItem();
QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget = nullptr);
@ -70,8 +74,10 @@ public:
void setDeleted(bool deleted = true)
{
this->deleted_ = deleted;
if (deleted)
this->model_ = noPiece;
update(boundingRect());
}