From 676a5fe461082219f7080f3357ea559e5cbc30d2 Mon Sep 17 00:00:00 2001 From: ce-amtic Date: Tue, 30 May 2023 11:29:14 +0800 Subject: [PATCH] fix: final debug --- DialogBox/reviewdialog.cpp | 2 +- DialogBox/settingdialog.cpp | 17 +-- DialogBox/settingdialog.h | 1 + Object/bot.cpp | 10 +- Object/bot.h | 3 + Object/judge.cpp | 10 +- Widget/gamewidget.cpp | 109 ++++++++------- Widget/gamewidget.h | 3 +- mynogo.pro.user | 261 ------------------------------------ 9 files changed, 88 insertions(+), 328 deletions(-) delete mode 100644 mynogo.pro.user diff --git a/DialogBox/reviewdialog.cpp b/DialogBox/reviewdialog.cpp index b293c72..8b23968 100644 --- a/DialogBox/reviewdialog.cpp +++ b/DialogBox/reviewdialog.cpp @@ -13,7 +13,7 @@ ReviewDialog::ReviewDialog(Judge *_judge, QWidget *parent) : ui->setupUi(this); - QRegularExpression regx("^[0-9]{1,}$"); + QRegularExpression regx("[0-9]{1,3}$"); QValidator *validator = new QRegularExpressionValidator(regx, ui->stepnum); ui->stepnum->setValidator(validator); connect(ui->stepnum, &QLineEdit::returnPressed, this, &ReviewDialog::on_input_entered); diff --git a/DialogBox/settingdialog.cpp b/DialogBox/settingdialog.cpp index d0cc51d..080208c 100644 --- a/DialogBox/settingdialog.cpp +++ b/DialogBox/settingdialog.cpp @@ -88,10 +88,9 @@ SettingDialog::SettingDialog(Judge *j, QWidget *parent) : ui->switchBtn->setDisabled(true); ui->usrnameInput->setDisabled(true); - QRegularExpression regx("^[0-9a-zA-Z_]{1,}$"); + QRegularExpression regx("[0-9a-zA-Z_]{1,18}$"); QValidator *validator = new QRegularExpressionValidator(regx, ui->usrnameInput); ui->usrnameInput->setValidator(validator); - ui->usrnameInput->setMaxLength(18); connect(ui->gamemodeCB, &QComboBox::activated, this, &SettingDialog::getRunMode); connect(ui->chessbdCB, &QComboBox::activated, this, &SettingDialog::getChessBd); @@ -124,11 +123,17 @@ void SettingDialog::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing, true); // 抗锯齿 - - // painter.setPen(QPen(QColor(0xf9f8ef), Strength, Qt::SolidLine)); painter.setBrush(QColor(BG_COLOR)); painter.drawRect(0, 0, WIDTH, HEIGHT); } +void SettingDialog::closeEvent(QCloseEvent *event) +{ + judge->IP = ui->IPinput->text(); + judge->PORT = ui->PORTinput->text().toInt(); + if(ui->usrnameInput->text() != "") judge->usrnameOL = ui->usrnameInput->text(); + else ui->usrnameInput->setText(judge->usrnameOL); + getChessBd(ui->chessbdCB->currentIndex()); +} void SettingDialog::getRunMode(int mode) { @@ -151,10 +156,6 @@ void SettingDialog::getChessBd(int size) // 按钮行为 void SettingDialog::on_closeBtn_clicked() { - judge->IP = ui->IPinput->text(); - judge->PORT = ui->PORTinput->text().toInt(); - judge->usrnameOL = ui->usrnameInput->text(); - getChessBd(ui->chessbdCB->currentIndex()); this->close(); } void SettingDialog::on_restartBtn_clicked() diff --git a/DialogBox/settingdialog.h b/DialogBox/settingdialog.h index 43065b2..768eea8 100644 --- a/DialogBox/settingdialog.h +++ b/DialogBox/settingdialog.h @@ -43,6 +43,7 @@ public slots: private: void paintEvent(QPaintEvent *event) override; + void closeEvent(QCloseEvent* event) override; Ui::SettingDialog *ui; int WIDTH, HEIGHT; diff --git a/Object/bot.cpp b/Object/bot.cpp index 5ed5ead..de8e51d 100644 --- a/Object/bot.cpp +++ b/Object/bot.cpp @@ -12,11 +12,12 @@ Bot::Bot(Judge *j, QObject *parent) : QThread(parent), judge(j) { - // 初始化 + token = QRandomGenerator::global()->bounded(1<<16); } Bot::~Bot() { + judge->log(Level::Debug, "Bot"+QString::number(token)+" destructed"); // delete this; } @@ -26,6 +27,7 @@ void Bot::init() eps = 0.03; alpha = 0.39; beta = 0.61; + judge->log(Level::Debug, "Bot"+QString::number(token)+" initialized"); } bool Bot::IsInBoard(int x, int y) @@ -196,13 +198,14 @@ double Bot::alphaBeta(double a, double b, int depth) } void Bot::run() { - if(judge->CHESSBOARD_SIZE != 9) return makeRandomMove(); + readFromJudge(); searchStartTime = clock(); // 计时器 srand(time(0)); chooseVec.clear(); - readFromJudge(); pointChecked = 0; + if(CHESSBOARD_SIZE != 9) return makeRandomMove(); + for(int x = 0; x < CHESSBOARD_SIZE; x++) for(int y = 0; y < CHESSBOARD_SIZE; y++) { @@ -257,6 +260,7 @@ void Bot::makeRandomMove() } x = rand() % CHESSBOARD_SIZE; y = rand() % CHESSBOARD_SIZE; + qDebug() << x << ' ' << y; } while(!judge->CheckVaild(x, y)); judge->PlaceAPiece(x, y); diff --git a/Object/bot.h b/Object/bot.h index 44ff882..085ea77 100644 --- a/Object/bot.h +++ b/Object/bot.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "Object/judge.h" /* @@ -61,6 +62,8 @@ public slots: int maxDep; ItemVector chooseVec, pointVec[5]; + + quint16 token; }; #endif // BOT_H diff --git a/Object/judge.cpp b/Object/judge.cpp index d1555d7..8ee2634 100644 --- a/Object/judge.cpp +++ b/Object/judge.cpp @@ -24,9 +24,9 @@ Judge::Judge(QObject *parent) : PORT = 16677; server = new NetworkServer(this); socket = new NetworkSocket(new QTcpSocket(), this); - srand(time(0) + clock()); - // usrnameOL = QString("OnlinePlayer") + QString::number(QRandomGenerator::global()->bounded(89999) + 10000); + static QRegularExpression regx("[^0-9a-zA-Z_]"); usrnameOL = QStandardPaths::writableLocation(QStandardPaths::HomeLocation).section("/",-1,-1); + usrnameOL.replace(regx, "_"); \ QObject::connect(server, &NetworkServer::receive, this, &Judge::recDataFromClient); QObject::connect(socket, &NetworkSocket::receive, this, [&](NetworkData d){ @@ -44,12 +44,11 @@ Judge::Judge(QObject *parent) : }); init(); - log(Level::Debug, "database constructed"); } Judge::~Judge() { - log(Level::Debug, "database destructed"); + log(Level::Debug, "Judge destructed"); // delete this; } @@ -67,7 +66,7 @@ void Judge::init() curPlayer = 0; loadState = 0; hasSentREA = hasSentTIM = hasSentGIV = hasSentSUI = false; - log(Level::Debug, "database initialized"); + log(Level::Debug, "Judge initialized"); } int Judge::GridPoint(int x, int y) {return board[x][y];} @@ -278,6 +277,7 @@ void Judge::send(NetworkData d) case OPCODE::GIVEUP_END_OP: hasSentGIV = true; break; case OPCODE::TIMEOUT_END_OP: hasSentTIM = true; break; case OPCODE::SUICIDE_END_OP: hasSentSUI = true; break; + default: break; } } bool Judge::isConnected() diff --git a/Widget/gamewidget.cpp b/Widget/gamewidget.cpp index ccea85d..245bf38 100644 --- a/Widget/gamewidget.cpp +++ b/Widget/gamewidget.cpp @@ -198,30 +198,58 @@ void GameWidget::mousePressEvent(QMouseEvent *event) } else sendMessage(2); } -void GameWidget::startGame(int player){ +void GameWidget::startGame(int player) +{ if(judge->runMode == 1) autoControl->setDisabled(true); else autoControl->setDisabled(false); firstMove(player); startTimer(); updateCB(); } -void GameWidget::firstMove(int player){ +void GameWidget::firstMove(int player) +{ if(player == -1) // bot 先手 { if(judge->runMode == 0) bot->start(); if(judge->runMode == 1) judge->curPlayer ^= 1; } } +void GameWidget::closeEvent(QCloseEvent* event) +{ + stopTimer(); + clickToCloseMB(true); + dataToString(); + judge->log(Level::Info, "GameWidget closed: "+QString(dataStr)); -void GameWidget::updateCB(){ - ui->stepLabel->setText(QString::number(judge->getStep().size())); - repaint(); + ui->saveButton->setEnabled(1); + mouse_disabled = 0; + + autoControl->setToggled(false); + autoPlayer->terminate(); + bot->terminate(); + bot->init(); + autoPlayer->init(); + judge->init(); } -void GameWidget::clickToCloseMB(bool force){ + +void GameWidget::clickToCloseMB(bool force) +{ if(force) mess->timeUpClose(); else mess->clickToClose(); } - +void GameWidget::updateCB() +{ + ui->stepLabel->setText(QString::number(judge->getStep().size())); + repaint(); +} +void GameWidget::updateBar() +{ + double value; + value=(clock()-basetime); + if(!bot->isRunning()) ui->TimeBar->setValue(1000-value/PLAYER_TIMEOUT); + else ui->TimeBar->setValue(1000-value/BOT_TIMEOUT); + repaint(); +} void GameWidget::setColorForBar() { if(judge->curPlayer == -1) return; @@ -248,14 +276,6 @@ void GameWidget::setColorForBar() ui->stepLabel->setStyleSheet(CUS_BLACK_PRIMARY); } } -void GameWidget::updateBar() -{ - double value; - value=(clock()-basetime); - if(!bot->isRunning()) ui->TimeBar->setValue(1000-value/PLAYER_TIMEOUT); - else ui->TimeBar->setValue(1000-value/BOT_TIMEOUT); - repaint(); -} void GameWidget::paintEvent(QPaintEvent *event) { @@ -409,7 +429,6 @@ void GameWidget::drawDemo(QPainter &painter) // 绘画 FYH { if(judge->GridPoint(i, j)) return; } - srand(time(0)); int fyhBoard[50][50] = {}; int Len = (judge->CHESSBOARD_SIZE - 1) / 2; @@ -454,27 +473,31 @@ void GameWidget::drawDemo(QPainter &painter) // 绘画 FYH // 判胜负与计时器相关 void GameWidget::gameLose(int type) { - judge->curPlayerBak = judge->curPlayer; + stopTimer(); if(type) sendMessage(1); else sendMessage(3); + + autoControl->setToggled(false); + autoPlayer->terminate(); + bot->terminate(); + + judge->curPlayerBak = judge->curPlayer; judge->curPlayer = -1; - // qDebug()<<"buglose"; judge->loadState = 'T'; - - dataToString(); - judge->log(Level::Info, "game lose: "+QString(dataStr)); } void GameWidget::gameWin(int type) { - judge->curPlayerBak = judge->curPlayer; + stopTimer(); if(type) sendMessage(0); else sendMessage(4); + + autoControl->setToggled(false); + autoPlayer->terminate(); + bot->terminate(); + + judge->curPlayerBak = judge->curPlayer; judge->curPlayer = -1; - // qDebug()<<"bugwin"; judge->loadState = 'W'; - - dataToString(); - judge->log(Level::Info, "game win: "+QString(dataStr)); } void GameWidget::stopTimer() { timerForPlayer->stop(); @@ -672,13 +695,16 @@ void GameWidget::goOFFL() } void GameWidget::remoteResign() { + stopTimer(); sendMessage(6); + + autoControl->setToggled(false); + autoPlayer->terminate(); + bot->terminate(); + judge->curPlayerBak = judge->curPlayer; judge->curPlayer = -1; - stopTimer(); - - dataToString(); - judge->log(Level::Info, "game win: "+QString(dataStr)); + judge->loadState = 'W'; } void GameWidget::on_try() @@ -694,6 +720,7 @@ void GameWidget::off_try() void GameWidget::on_sendButton_clicked() { QString data = ui->chatInput->text(); + if(data == "") return; QString s = "<" + judge->usrnameOL + "> : \n" + data; ui->chatHistory->appendPlainText(s); ui->chatInput->setText(""); @@ -706,39 +733,23 @@ void GameWidget::on_restartButton_clicked_OFFL() reviewDialog->init(); reviewDialog->close(); } - ui->saveButton->setEnabled(1); - mouse_disabled = 0; - clickToCloseMB(true); this->close(); - - autoControl->setToggled(false); - autoPlayer->terminate(); - bot->terminate(); - bot->init(); - autoPlayer->init(); - - stopTimer(); emit restartSingal(0); - judge->init(); } void GameWidget::on_resignButton_clicked_OFFL() { if(judge->curPlayer == -1) return; + stopTimer(); + sendMessage(5); + autoControl->setToggled(false); autoPlayer->terminate(); bot->terminate(); - bot->init(); - autoPlayer->init(); - sendMessage(5); judge->curPlayerBak = judge->curPlayer; judge->curPlayer = -1; judge->loadState = 'G'; - stopTimer(); - - dataToString(); - judge->log(Level::Info, "game lose: "+QString(dataStr)); } void GameWidget::on_restartButton_clicked_OL() { diff --git a/Widget/gamewidget.h b/Widget/gamewidget.h index 0d651b9..afd94fb 100644 --- a/Widget/gamewidget.h +++ b/Widget/gamewidget.h @@ -74,6 +74,7 @@ private slots: private: void paintEvent(QPaintEvent *event) override; // 界面绘制 + void closeEvent(QCloseEvent *event) override; // 关闭事件 void drawChessboard(QPainter &painter); void drawBlack(QPainter &painter, double px, double py); void drawWhite(QPainter &painer, double px, double py); @@ -85,7 +86,7 @@ private slots: void gameLose(int type = 0); // 输掉游戏(0->PVE 1->PVP) void gameWin(int type = 0); // 赢了游戏(0->PVE 1->PVP) - void dataToString(); // 编码 + void dataToString(); // 编码 (playerRole:0->w/2->b) (runMode:0->PVE/PVP/server/client) void stringToData(); // 解码 /* diff --git a/mynogo.pro.user b/mynogo.pro.user deleted file mode 100644 index 23bbc68..0000000 --- a/mynogo.pro.user +++ /dev/null @@ -1,261 +0,0 @@ - - - - - - EnvironmentId - {f63d1684-5b88-47a4-abf1-eb8ef167a4da} - - - ProjectExplorer.Project.ActiveTarget - 0 - - - ProjectExplorer.Project.EditorSettings - - true - false - true - - Cpp - - CppGlobal - - - - QmlJS - - QmlJSGlobal - - - 2 - UTF-8 - false - 4 - false - 80 - true - true - 1 - false - true - false - 0 - true - true - 0 - 8 - true - false - 1 - true - true - true - *.md, *.MD, Makefile - false - true - true - - - - ProjectExplorer.Project.PluginSettings - - - true - false - true - true - true - true - - - 0 - true - - true - true - Builtin.DefaultTidyAndClazy - 6 - - - - true - - - - - ProjectExplorer.Project.Target.0 - - Desktop - Desktop Qt 6.3.2 MinGW 64-bit - Desktop Qt 6.3.2 MinGW 64-bit - qt.qt6.632.win64_mingw_kit - 0 - 0 - 0 - - 0 - E:\NOGO\..\build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Debug - E:/build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Debug - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - 构建 - 构建 - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - - - E:\NOGO\..\build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Release - E:/build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Release - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - 构建 - 构建 - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Release - Qt4ProjectManager.Qt4BuildConfiguration - 0 - 0 - - - 0 - E:\NOGO\..\build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Profile - E:/build-mynogo-Desktop_Qt_6_3_2_MinGW_64_bit-Profile - - - true - QtProjectManager.QMakeBuildStep - false - - - - true - Qt4ProjectManager.MakeStep - - 2 - 构建 - 构建 - ProjectExplorer.BuildSteps.Build - - - - true - Qt4ProjectManager.MakeStep - clean - - 1 - Clean - Clean - ProjectExplorer.BuildSteps.Clean - - 2 - false - - false - - Profile - Qt4ProjectManager.Qt4BuildConfiguration - 0 - 0 - 0 - - 3 - - - 0 - 部署 - 部署 - ProjectExplorer.BuildSteps.Deploy - - 1 - - false - ProjectExplorer.DefaultDeployConfiguration - - 1 - - true - true - true - - 2 - - ProjectExplorer.CustomExecutableRunConfiguration - - false - true - false - true - - 1 - - - - ProjectExplorer.Project.TargetCount - 1 - - - ProjectExplorer.Project.Updater.FileVersion - 22 - - - Version - 22 - -