Skip to content

Commit

Permalink
fix: final debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ce-amtic committed May 30, 2023
1 parent ffd22ad commit 676a5fe
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 328 deletions.
2 changes: 1 addition & 1 deletion DialogBox/reviewdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions DialogBox/settingdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions DialogBox/settingdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public slots:

private:
void paintEvent(QPaintEvent *event) override;
void closeEvent(QCloseEvent* event) override;

Ui::SettingDialog *ui;
int WIDTH, HEIGHT;
Expand Down
10 changes: 7 additions & 3 deletions Object/bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions Object/bot.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QObject>
#include <QDebug>
#include <QThread>
#include <QRandomGenerator>
#include "Object/judge.h"

/*
Expand Down Expand Up @@ -61,6 +62,8 @@ public slots:

int maxDep;
ItemVector chooseVec, pointVec[5];

quint16 token;
};

#endif // BOT_H
10 changes: 5 additions & 5 deletions Object/judge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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;
}

Expand All @@ -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];}
Expand Down Expand Up @@ -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()
Expand Down
109 changes: 60 additions & 49 deletions Widget/gamewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()
Expand All @@ -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("");
Expand All @@ -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()
{
Expand Down
3 changes: 2 additions & 1 deletion Widget/gamewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(); // 解码

/*
Expand Down
Loading

0 comments on commit 676a5fe

Please sign in to comment.