refactor: 对各文件代码格式重构减小缩进层级

This commit is contained in:
Calcitem Team 2019-08-16 01:22:37 +08:00 committed by CalciteM Team
parent a6432a20ba
commit 6c8ba38fa6
8 changed files with 224 additions and 185 deletions

View File

@ -19,8 +19,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <QDebug>
#include <QTimer>
#include <QDebug>
#include "aithread.h"
AiThread::AiThread(int id, QObject *parent) :
@ -42,8 +43,7 @@ AiThread::AiThread(int id, QObject *parent) :
connect(&timer, &QTimer::timeout, this, &AiThread::act, Qt::QueuedConnection);
// 网络
if (id == 1)
{
if (id == 1) {
server = new Server(nullptr, 30001);
uint16_t clientPort = server->getPort() == 30001 ? 30002 : 30001;
client = new Client(nullptr, clientPort);
@ -126,6 +126,7 @@ void AiThread::run()
mutex.unlock();
if (ai_ab.alphaBetaPruning(aiDepth) == 3) {
// 三次重复局面和
qDebug() << "Draw\n";
strCommand = "draw";
QTimer::singleShot(EMIT_COMMAND_DELAY, this, &AiThread::emitCommand);
@ -149,6 +150,7 @@ void AiThread::run()
}
mutex.unlock();
}
qDebug() << "Thread" << id << "quit";
}

View File

@ -34,26 +34,34 @@ BoardItem::BoardItem(QGraphicsItem *parent) : QGraphicsItem(),
setPos(0, 0);
// 初始化24个落子点
for (int i = 0; i < N_RINGS; i++) {
for (int r = 0; r < N_RINGS; r++) {
// 内圈的12点钟方向为第一个位置按顺时针方向排序
// 然后是中圈和外圈
qreal a = (i + 1) * LINE_INTERVAL;
position[i * N_SEATS + 0].rx() = 0;
position[i * N_SEATS + 0].ry() = -a;
position[i * N_SEATS + 1].rx() = a;
position[i * N_SEATS + 1].ry() = -a;
position[i * N_SEATS + 2].rx() = a;
position[i * N_SEATS + 2].ry() = 0;
position[i * N_SEATS + 3].rx() = a;
position[i * N_SEATS + 3].ry() = a;
position[i * N_SEATS + 4].rx() = 0;
position[i * N_SEATS + 4].ry() = a;
position[i * N_SEATS + 5].rx() = -a;
position[i * N_SEATS + 5].ry() = a;
position[i * N_SEATS + 6].rx() = -a;
position[i * N_SEATS + 6].ry() = 0;
position[i * N_SEATS + 7].rx() = -a;
position[i * N_SEATS + 7].ry() = -a;
qreal a = (r + 1) * LINE_INTERVAL;
position[r * N_SEATS + 0].rx() = 0;
position[r * N_SEATS + 0].ry() = -a;
position[r * N_SEATS + 1].rx() = a;
position[r * N_SEATS + 1].ry() = -a;
position[r * N_SEATS + 2].rx() = a;
position[r * N_SEATS + 2].ry() = 0;
position[r * N_SEATS + 3].rx() = a;
position[r * N_SEATS + 3].ry() = a;
position[r * N_SEATS + 4].rx() = 0;
position[r * N_SEATS + 4].ry() = a;
position[r * N_SEATS + 5].rx() = -a;
position[r * N_SEATS + 5].ry() = a;
position[r * N_SEATS + 6].rx() = -a;
position[r * N_SEATS + 6].ry() = 0;
position[r * N_SEATS + 7].rx() = -a;
position[r * N_SEATS + 7].ry() = -a;
}
}
@ -70,6 +78,7 @@ QPainterPath BoardItem::shape() const
{
QPainterPath path;
path.addRect(boundingRect());
return path;
}
@ -106,17 +115,15 @@ void BoardItem::paint(QPainter *painter,
}
// 画4条纵横线
painter->drawLine(position[0], position[(N_RINGS - 1) * N_SEATS]);
painter->drawLine(position[2], position[(N_RINGS - 1) * N_SEATS + 2]);
painter->drawLine(position[4], position[(N_RINGS - 1) * N_SEATS + 4]);
painter->drawLine(position[6], position[(N_RINGS - 1) * N_SEATS + 6]);
for (int i = 0; i < N_SEATS; i += 2) {
painter->drawLine(position[i], position[(N_RINGS - 1) * N_SEATS + i]);
}
if (hasObliqueLine) {
// 画4条斜线
painter->drawLine(position[1], position[(N_RINGS - 1) * N_SEATS + 1]);
painter->drawLine(position[3], position[(N_RINGS - 1) * N_SEATS + 3]);
painter->drawLine(position[5], position[(N_RINGS - 1) * N_SEATS + 5]);
painter->drawLine(position[7], position[(N_RINGS - 1) * N_SEATS + 7]);
for (int i = 1; i < N_SEATS; i += 2) {
painter->drawLine(position[i], position[(N_RINGS - 1) * N_SEATS + i]);
}
}
#ifdef DRAW_SEAT_NUMBER
@ -129,7 +136,7 @@ void BoardItem::paint(QPainter *painter,
font.setLetterSpacing(QFont::AbsoluteSpacing, 0);
painter->setFont(font);
for (int i = 0; i < 8; i++) {
for (int i = 0; i < N_SEATS; i++) {
char cSeat = '1' + i;
QString strSeat(cSeat);
painter->drawText(position[(N_RINGS - 1) * N_SEATS + i], strSeat);
@ -150,6 +157,7 @@ QPointF BoardItem::nearestPosition(QPointF const pos)
break;
}
}
return nearestPos;
}

View File

@ -129,6 +129,7 @@ bool GameScene::pos2cp(QPointF pos, int &c, int &p)
void GameScene::setDiagonal(bool arg /*= true*/)
{
if (board)
if (board) {
board->setDiagonal(arg);
}
}

View File

@ -19,10 +19,11 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "gameview.h"
#include <QMatrix>
#include <QDebug>
#include "gameview.h"
GameView::GameView(QWidget *parent) :
QGraphicsView(parent)
{

View File

@ -19,10 +19,11 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "ninechesswindow.h"
#include <QtWidgets/QApplication>
#include <QDesktopWidget>
#include "ninechesswindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

View File

@ -289,14 +289,17 @@ void NineChessAi_ab::generateLegalMoves(Node *node, move_t bestMove)
if (chessTemp.context.stage & (NineChess::GAME_PLACING | NineChess::GAME_NOTSTARTED)) {
for (int i = 0; i < MOVE_PRIORITY_TABLE_SIZE; i++) {
pos = movePriorityTable[i];
if (!chessTemp.board_[pos]) {
if (chessTemp.context.stage != NineChess::GAME_NOTSTARTED || node != rootNode) {
addNode(node, 0, pos, bestMove, chessTemp.context.turn);
} else {
// 若为先手,则抢占星位
if (NineChess::isStarPoint(pos)) {
addNode(node, INF_VALUE, pos, bestMove, chessTemp.context.turn);
}
if (chessTemp.board_[pos]) {
continue;
}
if (chessTemp.context.stage != NineChess::GAME_NOTSTARTED || node != rootNode) {
addNode(node, 0, pos, bestMove, chessTemp.context.turn);
} else {
// 若为先手,则抢占星位
if (NineChess::isStarPoint(pos)) {
addNode(node, INF_VALUE, pos, bestMove, chessTemp.context.turn);
}
}
}
@ -313,8 +316,10 @@ void NineChessAi_ab::generateLegalMoves(Node *node, move_t bestMove)
for (int i = 0; i < MOVE_PRIORITY_TABLE_SIZE; i++) {
#endif // MOVE_PRIORITY_TABLE_SUPPORT
oldPos = movePriorityTable[i];
if (!chessTemp.choose(oldPos))
if (!chessTemp.choose(oldPos)) {
continue;
}
if ((chessTemp.context.turn == NineChess::PLAYER1 &&
(chessTemp.context.nPiecesOnBoard_1 > chessTemp.currentRule.nPiecesAtLeast || !chessTemp.currentRule.allowFlyWhenRemainThreePieces)) ||
@ -352,14 +357,15 @@ void NineChessAi_ab::generateLegalMoves(Node *node, move_t bestMove)
addNode(node, 0, -pos, bestMove, chessTemp.context.turn);
}
}
} else {
// 不是全成三的情况
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
pos = movePriorityTable[i];
if (chessTemp.board_[pos] & opponent) {
if (chessTemp.getRule()->allowRemoveMill || !chessTemp.isInMills(pos)) {
addNode(node, 0, -pos, bestMove, chessTemp.context.turn);
}
break;
}
// 不是全成三的情况
for (int i = MOVE_PRIORITY_TABLE_SIZE - 1; i >= 0; i--) {
pos = movePriorityTable[i];
if (chessTemp.board_[pos] & opponent) {
if (chessTemp.getRule()->allowRemoveMill || !chessTemp.isInMills(pos)) {
addNode(node, 0, -pos, bestMove, chessTemp.context.turn);
}
}
}
@ -416,17 +422,21 @@ void NineChessAi_ab::sortLegalMoves(Node *node)
void NineChessAi_ab::deleteTree(Node *node)
{
// 递归删除节点树
if (node) {
for (auto i : node->children) {
deleteTree(i);
}
node->children.clear();
#ifdef MEMORY_POOL
pool.deleteElement(node);
#else
delete(node);
#endif
if (node == nullptr) {
return;
}
for (auto i : node->children) {
deleteTree(i);
}
node->children.clear();
#ifdef MEMORY_POOL
pool.deleteElement(node);
#else
delete(node);
#endif
}
void NineChessAi_ab::setChess(const NineChess &chess)
@ -613,29 +623,27 @@ int NineChessAi_ab::evaluate(Node *node)
#ifdef DEBUG_AB_TREE
node->result = -3;
#endif
}
else {
} else {
value = 0;
}
}
// 走棋阶段被闷判断
if (chessContext->action == NineChess::ACTION_CHOOSE && chessTemp.isAllSurrounded(chessContext->turn)) {
// 规则要求被“闷”判负,则对手获胜
if (chessTemp.currentRule.isLoseWhenNoWay) {
if (chessContext->action == NineChess::ACTION_CHOOSE &&
chessTemp.isAllSurrounded(chessContext->turn) &&
chessTemp.currentRule.isLoseWhenNoWay) {
// 规则要求被“闷”判负,则对手获胜
if (chessContext->turn == NineChess::PLAYER1) {
value -= 10000;
#ifdef DEBUG_AB_TREE
node->result = -2;
#endif
}
else {
} else {
value += 10000;
#ifdef DEBUG_AB_TREE
node->result = 2;
#endif
}
}
}
// 剩余棋子个数判断
@ -644,8 +652,7 @@ int NineChessAi_ab::evaluate(Node *node)
#ifdef DEBUG_AB_TREE
node->result = -1;
#endif
}
else if (chessContext->nPiecesOnBoard_2 < chessTemp.currentRule.nPiecesAtLeast) {
} else if (chessContext->nPiecesOnBoard_2 < chessTemp.currentRule.nPiecesAtLeast) {
value += 10000;
#ifdef DEBUG_AB_TREE
node->result = 1;
@ -749,7 +756,7 @@ int NineChessAi_ab::alphaBetaPruning(depth_t depth)
if (chess_.getStage() == NineChess::GAME_PLACING) {
positions.clear();
}
#endif
#endif // THREEFOLD_REPETITION
#ifdef MOVE_PRIORITY_TABLE_SUPPORT
#ifdef RANDOM_MOVE
@ -870,10 +877,11 @@ int NineChessAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t beta,
node->value = evaluate(node);
// 为争取速胜value 值 +- 深度
if (node->value > 0)
if (node->value > 0) {
node->value += depth;
else
} else {
node->value -= depth;
}
#ifdef DEBUG_AB_TREE
node->isLeaf = true;
@ -893,10 +901,11 @@ int NineChessAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t beta,
node->value = evaluate(node);
// 为争取速胜value 值 +- 深度 (有必要?)
if (chessContext->turn == NineChess::PLAYER1)
if (chessContext->turn == NineChess::PLAYER1) {
node->value += depth;
else
} else {
node->value -= depth;
}
#ifdef DEBUG_AB_TREE
if (requiredQuit) {
@ -996,8 +1005,7 @@ int NineChessAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t beta,
beta = std::min(value, beta);
#if 0
if (value < beta)
{
if (value < beta) {
#ifdef HASH_MAP_ENABLE
hashf = hashfBETA;
#endif
@ -1026,9 +1034,10 @@ int NineChessAi_ab::alphaBetaPruning(depth_t depth, value_t alpha, value_t beta,
// 删除“孙子”节点,防止层数较深的时候节点树太大
#ifndef DONOT_DELETE_TREE
for (auto child : node->children) {
for (auto grandChild : child->children)
deleteTree(grandChild); // (9%)
child->children.clear(); // (3%)
for (auto grandChild : child->children) {
deleteTree(grandChild);
}
child->children.clear();
}
#endif // DONOT_DELETE_TREE
@ -1051,8 +1060,9 @@ const char* NineChessAi_ab::bestMove()
vector<Node*> bestMoves;
size_t bestMovesSize = 0;
if ((rootNode->children).size() == 0)
if ((rootNode->children).size() == 0) {
return "error!";
}
qDebug() << "31 ----- 24 ----- 25";
qDebug() << "| \\ | / |";
@ -1075,10 +1085,11 @@ const char* NineChessAi_ab::bestMove()
#ifdef SORT_CONSIDER_PRUNED
&& !child->pruned
#endif
)
) {
qDebug("[%.2d] %d\t%s\t%d *", i, child->move, move2string(child->move), child->value);
else
} else {
qDebug("[%.2d] %d\t%s\t%d", i, child->move, move2string(child->move), child->value);
}
i++;
}
@ -1152,10 +1163,12 @@ NineChessAi_ab::value_t NineChessAi_ab::probeHash(NineChess::hash_t hash,
if (hashValue.type == hashfEXACT) {
return hashValue.value;
}
if ((hashValue.type == hashfALPHA) && // 最多是 hashValue.value
(hashValue.value <= alpha)) {
return alpha;
}
if ((hashValue.type == hashfBETA) && // 至少是 hashValue.value
(hashValue.value >= beta)) {
return beta;
@ -1175,7 +1188,7 @@ bool NineChessAi_ab::findHash(NineChess::hash_t hash, HashValue &hashValue)
if (iter != hashmap.end())
return iter;
// 变换局面,查找hash (废弃)
// 变换局面,查找 hash (废弃)
chessTempShift = chessTemp;
for (int i = 0; i < 2; i++) {
if (i)

View File

@ -443,9 +443,7 @@ void NineChessWindow::on_actionNew_N_triggered()
file.setFileName(path);
// 打开文件,只写方式打开
bool isok = file.open(QFileDevice::WriteOnly | QFileDevice::Text);
if (isok) {
if (file.open(QFileDevice::WriteOnly | QFileDevice::Text)) {
// 写文件
QTextStream textStream(&file);
for (QString cmd : strlist->stringList())
@ -469,52 +467,56 @@ void NineChessWindow::on_actionNew_N_triggered()
void NineChessWindow::on_actionOpen_O_triggered()
{
QString path = QFileDialog::getOpenFileName(this, tr("打开棋谱文件"), QDir::currentPath(), "TXT(*.txt)");
if (path.isEmpty() == false) {
if (file.isOpen())
file.close();
// 文件对象
file.setFileName(path);
// 不支持1MB以上的文件
if (file.size() > 0x100000) {
// 定义新对话框
QMessageBox msgBox(QMessageBox::Warning,
tr("文件过大"), tr("不支持1MB以上文件"), QMessageBox::Ok);
msgBox.exec();
return;
}
// 打开文件,只读方式打开
bool isok = file.open(QFileDevice::ReadOnly | QFileDevice::Text);
if (isok) {
// 取消AI设定
ui.actionEngine1_T->setChecked(false);
ui.actionEngine2_R->setChecked(false);
// 读文件
QTextStream textStream(&file);
QString cmd;
cmd = textStream.readLine();
// 读取并显示棋谱时,不必刷新棋局场景
if (!(game->command(cmd, false))) {
// 定义新对话框
QMessageBox msgBox(QMessageBox::Warning, tr("文件错误"), tr("不是正确的棋谱文件"), QMessageBox::Ok);
msgBox.exec();
return;
}
while (!textStream.atEnd()) {
cmd = textStream.readLine();
game->command(cmd, false);
}
// 最后刷新棋局场景
game->updateScence();
}
if (path.isEmpty()) {
return;
}
if (file.isOpen()) {
file.close();
}
// 文件对象
file.setFileName(path);
// 不支持 1MB 以上的文件
if (file.size() > 0x100000) {
// 定义新对话框
QMessageBox msgBox(QMessageBox::Warning,
tr("文件过大"), tr("不支持 1MB 以上文件"), QMessageBox::Ok);
msgBox.exec();
return;
}
// 打开文件,只读方式打开
if (!(file.open(QFileDevice::ReadOnly | QFileDevice::Text))) {
return;
}
// 取消AI设定
ui.actionEngine1_T->setChecked(false);
ui.actionEngine2_R->setChecked(false);
// 读文件
QTextStream textStream(&file);
QString cmd;
cmd = textStream.readLine();
// 读取并显示棋谱时,不必刷新棋局场景
if (!(game->command(cmd, false))) {
// 定义新对话框
QMessageBox msgBox(QMessageBox::Warning, tr("文件错误"), tr("不是正确的棋谱文件"), QMessageBox::Ok);
msgBox.exec();
return;
}
while (!textStream.atEnd()) {
cmd = textStream.readLine();
game->command(cmd, false);
}
// 最后刷新棋局场景
game->updateScence();
}
void NineChessWindow::on_actionSave_S_triggered()
@ -523,9 +525,7 @@ void NineChessWindow::on_actionSave_S_triggered()
file.close();
// 打开文件,只写方式打开
bool isok = file.open(QFileDevice::WriteOnly | QFileDevice::Text);
if (isok) {
if (file.open(QFileDevice::WriteOnly | QFileDevice::Text)) {
// 写文件
QTextStream textStream(&file);
QStringListModel *strlist = qobject_cast<QStringListModel *>(ui.listView->model());
@ -533,9 +533,11 @@ void NineChessWindow::on_actionSave_S_triggered()
textStream << cmd << endl;
file.flush();
}
} else {
on_actionSaveAs_A_triggered();
return;
}
on_actionSaveAs_A_triggered();
}
void NineChessWindow::on_actionSaveAs_A_triggered()
@ -544,25 +546,31 @@ void NineChessWindow::on_actionSaveAs_A_triggered()
tr("打开棋谱文件"),
QDir::currentPath() + tr("棋谱_") + QString::number(QDateTime::currentDateTime().toTime_t()) + ".txt", "TXT(*.txt)");
if (path.isEmpty() == false) {
if (file.isOpen())
file.close();
// 文件对象
file.setFileName(path);
// 打开文件,只写方式打开
bool isok = file.open(QFileDevice::WriteOnly | QFileDevice::Text);
if (isok) {
// 写文件
QTextStream textStream(&file);
QStringListModel* strlist = qobject_cast<QStringListModel*>(ui.listView->model());
for (QString cmd : strlist->stringList())
textStream << cmd << endl;
file.flush();
}
if (path.isEmpty()) {
return;
}
if (file.isOpen()) {
file.close();
}
// 文件对象
file.setFileName(path);
// 打开文件,只写方式打开
if (!(file.open(QFileDevice::WriteOnly | QFileDevice::Text))) {
return;
}
// 写文件
QTextStream textStream(&file);
QStringListModel* strlist = qobject_cast<QStringListModel*>(ui.listView->model());
for (QString cmd : strlist->stringList()) {
textStream << cmd << endl;
}
file.flush();
}
void NineChessWindow::on_actionEdit_E_toggled(bool arg1)
@ -599,6 +607,7 @@ void NineChessWindow::on_actionRowChange()
int currentRow = ui.listView->currentIndex().row();
QObject *const obsender = sender();
if (obsender != nullptr) {
if (obsender == ui.actionBegin_S) {
ui.listView->setCurrentIndex(model->index(0, 0));
@ -683,39 +692,40 @@ void NineChessWindow::onAutoRunTimeOut(QPrivateSignal signal)
}
// 执行“下一招”
if (currentRow < rows - 1) {
if (currentRow < rows - 1) {
ui.listView->setCurrentIndex(ui.listView->model()->index(currentRow + 1, 0));
}
currentRow = ui.listView->currentIndex().row();
// 更新动作状态
if (currentRow <= 0) {
ui.actionBegin_S->setEnabled(false);
ui.actionPrevious_B->setEnabled(false);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
} else if (currentRow >= rows - 1) {
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(false);
ui.actionEnd_E->setEnabled(false);
ui.actionAutoRun_A->setEnabled(false);
} else {
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
}
// 更新局面
game->stageChange(currentRow);
} else {
if (currentRow >= rows - 1) {
ui.actionAutoRun_A->setChecked(false);
return;
}
if (currentRow < rows - 1) {
ui.listView->setCurrentIndex(ui.listView->model()->index(currentRow + 1, 0));
}
currentRow = ui.listView->currentIndex().row();
// 更新动作状态
if (currentRow <= 0) {
ui.actionBegin_S->setEnabled(false);
ui.actionPrevious_B->setEnabled(false);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
} else if (currentRow >= rows - 1) {
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(false);
ui.actionEnd_E->setEnabled(false);
ui.actionAutoRun_A->setEnabled(false);
} else {
ui.actionBegin_S->setEnabled(true);
ui.actionPrevious_B->setEnabled(true);
ui.actionNext_F->setEnabled(true);
ui.actionEnd_E->setEnabled(true);
ui.actionAutoRun_A->setEnabled(true);
}
// 更新局面
game->stageChange(currentRow);
}
// 自动运行

View File

@ -36,6 +36,7 @@ PieceItem::PieceItem(QGraphicsItem *parent) :
setFlags(ItemIsSelectable
// | ItemIsMovable
);
// 设置缓存模式
setCacheMode(DeviceCoordinateCache);
@ -136,6 +137,7 @@ void PieceItem::paint(QPainter *painter,
QPen pen(chooseLineColor, chooseLineWeight, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin);
painter->setPen(pen);
int xy = (size - chooseLineWeight) / 2;
painter->drawLine(-xy, -xy, -xy, -xy / 2);
painter->drawLine(-xy, -xy, -xy / 2, -xy);
painter->drawLine(xy, -xy, xy, -xy / 2);
@ -150,6 +152,7 @@ void PieceItem::paint(QPainter *painter,
if (deleted_) {
QPen pen(removeLineColor, removeLineWeight, Qt::SolidLine, Qt::SquareCap, Qt::BevelJoin);
painter->setPen(pen);
painter->drawLine(-size / 3, -size / 3, size / 3, size / 3);
painter->drawLine(size / 3, -size / 3, -size / 3, size / 3);
}