Skip to content

Commit c5ec33c

Browse files
committed
refined GameWidget
1 parent 9c4be6a commit c5ec33c

File tree

9 files changed

+66
-74
lines changed

9 files changed

+66
-74
lines changed

DialogBox/messagebox.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void MessageBox::mousePressEvent(QMouseEvent *event) {clickToClose();}
3434

3535
bool MessageBox::clickToClose()
3636
{
37-
if(pendingClose && clock() - baseTime <= 3000) return false;
37+
if(pendingClose && clock() - baseTime <= 5000) return false;
3838
timeUpClose();
3939
return true;
4040
}

DialogBox/messagebox.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* > void set(QString text, int tim, bool pendingClos)
2020
* > text : 显示的文字
2121
* > tim : 超时自动关闭,设置为 0 时, 必须要手动点击消息框才可关闭
22-
* > pendingClos : 强制 3s 内不能被关闭
22+
* > pendingClos : 强制 5s 内不能被关闭
2323
* > bool clickToClose()
2424
* > 模拟鼠标点击关闭,受 pendingClose 影响
2525
* > void timeUpClose()

Object/bot.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Bot::~Bot()
2323

2424
void Bot::init()
2525
{
26+
searchStartTime = 0;
2627
ItemVector().swap(chooseVec);
2728
dfsBoardTime = 0;
2829
eps = 0.03;
@@ -281,4 +282,7 @@ void Bot::makeRandomMove()
281282
while(!judge->CheckVaild(x, y));
282283
judge->PlaceAPiece(x, y);
283284
}
284-
285+
qint64 Bot::getStartTime()
286+
{
287+
return searchStartTime;
288+
}

Object/bot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class Bot : public QThread
2424
~Bot();
2525
void init();
2626
void run(); // run 函数是 bot 作为独立线程的入口, 通过 bot->start() 来启动这个独立线程, run 函数本身相当于 makeAMove
27+
qint64 getStartTime();
2728

2829
signals:
2930
void timeout();

Object/judge.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void Judge::init()
6464
ItemVector().swap(savedStep);
6565
blockCnt = 0;
6666
curPlayer = 0;
67+
playerRole = 0;
6768
loadState = 0;
6869
hasSentREA = hasSentTIM = hasSentGIV = hasSentSUI = false;
6970
log(Level::Debug, "Judge initialized");

Widget/gamewidget.cpp

Lines changed: 54 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
9494
connect(timerForPlayer,&QTimer::timeout, this, &GameWidget::playerTimeout_OFFL);
9595
connect(timerForBar,&QTimer::timeout,this,&GameWidget::updateBar);
9696
connect(judge, &Judge::GIVEUP_OP, this, &GameWidget::remoteResign);
97-
connect(judge, &Judge::TIMEOUT_END_OP, this, [&](){gameLose(1);});
9897
connect(judge, &Judge::SUICIDE_END_OP, this, [&](){
99-
gameWin(1);
100-
clickToCloseMB(true);
10198
sendMessage(3);
99+
endGame('W');
102100
});
103101
connect(judge, &Judge::CHAT_OP, this, [&](NetworkData d){
104102
QString s = "<" + judge->oppoOL + "> : \n" + d.data1;
@@ -107,9 +105,27 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
107105
connect(judge, &Judge::MOVE_OP, this, [&](){
108106
if(autoControl->isToggled()) autoPlayer->start();
109107
});
108+
connect(judge, &Judge::TIMEOUT_END_OP, this, [&](){
109+
if(autoPlayer->getStartTime() == 0)
110+
{
111+
judge->log(Level::Error, "auto player haven't been started yet!");
112+
return;
113+
}
114+
if(QDateTime::currentMSecsSinceEpoch() - autoPlayer->getStartTime() > BOT_TIMEOUT * 1000)
115+
{
116+
playerTimeout_OL();
117+
judge->log(Level::Info, "auto player run for "+QString::number(QDateTime::currentMSecsSinceEpoch()-autoPlayer->getStartTime())
118+
+"ms, TIMEOUT_END confirmed");
119+
}
120+
else
121+
{
122+
judge->log(Level::Error, "auto player run for "+QString::number(QDateTime::currentMSecsSinceEpoch()-autoPlayer->getStartTime())
123+
+"ms but opponent claims TIMEOUT_END");
124+
}
125+
});
110126
connect(autoControl, &SwitchControl::toggled, this, [&](bool checked){
111127
if(checked && judge->curPlayer == 1) autoPlayer->start();
112-
if(!checked) autoPlayer->terminate();
128+
if(!checked && autoPlayer->isRunning()) autoPlayer->terminate();
113129
});
114130
connect(autoPlayer, &QThread::finished, this, [&](){
115131
if(!autoControl->isToggled()) return;
@@ -122,7 +138,7 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
122138
if(autoControl->isToggled()){
123139
autoControl->setDisabled(true);
124140
autoControl->setToggled(false);
125-
autoPlayer->terminate();
141+
if(autoPlayer->isRunning()) autoPlayer->terminate();
126142
}
127143
});
128144
connect(bot, &Bot::timeout, this, &GameWidget::botTimeout);
@@ -158,7 +174,6 @@ void GameWidget::mousePressEvent(QMouseEvent *event)
158174
return ;
159175
}
160176

161-
162177
emit mousePress();
163178
if(judge->curPlayer == -1) return; // 判断游戏结束
164179
if(!judge->runMode && bot->isRunning()) return; // 等待 bot 落子
@@ -216,6 +231,18 @@ void GameWidget::firstMove(int player)
216231
if(judge->runMode == 1) judge->curPlayer ^= 1;
217232
}
218233
}
234+
void GameWidget::endGame(char loadState)
235+
{
236+
stopTimer();
237+
238+
autoControl->setToggled(false);
239+
if(autoPlayer->isRunning()) autoPlayer->terminate();
240+
if(bot->isRunning()) bot->terminate();
241+
242+
judge->curPlayerBak = judge->curPlayer;
243+
judge->curPlayer = -1;
244+
judge->loadState = loadState;
245+
}
219246
void GameWidget::closeEvent(QCloseEvent* event)
220247
{
221248
stopTimer();
@@ -227,10 +254,8 @@ void GameWidget::closeEvent(QCloseEvent* event)
227254
mouse_disabled = 0;
228255

229256
autoControl->setToggled(false);
230-
autoPlayer->terminate();
231-
bot->terminate();
232-
bot->init();
233-
autoPlayer->init();
257+
if(autoPlayer->isRunning()) autoPlayer->terminate();
258+
if(bot->isRunning()) bot->terminate();
234259
judge->init();
235260
}
236261

@@ -471,34 +496,6 @@ void GameWidget::drawDemo(QPainter &painter) // 绘画 FYH
471496
}
472497

473498
// 判胜负与计时器相关
474-
void GameWidget::gameLose(int type)
475-
{
476-
stopTimer();
477-
if(type) sendMessage(1);
478-
else sendMessage(3);
479-
480-
autoControl->setToggled(false);
481-
autoPlayer->terminate();
482-
bot->terminate();
483-
484-
judge->curPlayerBak = judge->curPlayer;
485-
judge->curPlayer = -1;
486-
judge->loadState = 'T';
487-
}
488-
void GameWidget::gameWin(int type)
489-
{
490-
stopTimer();
491-
if(type) sendMessage(0);
492-
else sendMessage(4);
493-
494-
autoControl->setToggled(false);
495-
autoPlayer->terminate();
496-
bot->terminate();
497-
498-
judge->curPlayerBak = judge->curPlayer;
499-
judge->curPlayer = -1;
500-
judge->loadState = 'W';
501-
}
502499
void GameWidget::stopTimer() {
503500
timerForPlayer->stop();
504501
timerForBar->stop();
@@ -513,15 +510,29 @@ void GameWidget::startTimer() {
513510
basetime=clock();
514511
}
515512
}
516-
void GameWidget::botTimeout() {if(judge->curPlayer >= 0) gameWin(0);}
517-
void GameWidget::playerTimeout_OFFL() {if(judge->curPlayer >= 0) gameLose(0);}
513+
void GameWidget::botTimeout()
514+
{
515+
sendMessage(4);
516+
endGame('W');
517+
}
518+
void GameWidget::playerTimeout_OFFL()
519+
{
520+
sendMessage(3);
521+
endGame('T');
522+
}
518523
void GameWidget::playerTimeout_OL()
519524
{
520525
// 发送 TIMEOUT_END_OP
521526
if(!judge->curPlayer) // 当前是等待响应的一方,那么对手超时己方胜利
522527
{
523528
judge->send(NetworkData(OPCODE::TIMEOUT_END_OP, judge->usrnameOL, "Sorry you timeout!"));
524-
gameWin(1);
529+
sendMessage(0);
530+
endGame('W');
531+
}
532+
else
533+
{
534+
sendMessage(1);
535+
endGame('T');
525536
}
526537
}
527538

@@ -695,16 +706,8 @@ void GameWidget::goOFFL()
695706
}
696707
void GameWidget::remoteResign()
697708
{
698-
stopTimer();
699709
sendMessage(6);
700-
701-
autoControl->setToggled(false);
702-
autoPlayer->terminate();
703-
bot->terminate();
704-
705-
judge->curPlayerBak = judge->curPlayer;
706-
judge->curPlayer = -1;
707-
judge->loadState = 'W';
710+
endGame('W');
708711
}
709712

710713
void GameWidget::on_try()
@@ -739,17 +742,8 @@ void GameWidget::on_restartButton_clicked_OFFL()
739742
void GameWidget::on_resignButton_clicked_OFFL()
740743
{
741744
if(judge->curPlayer == -1) return;
742-
743-
stopTimer();
744745
sendMessage(5);
745-
746-
autoControl->setToggled(false);
747-
autoPlayer->terminate();
748-
bot->terminate();
749-
750-
judge->curPlayerBak = judge->curPlayer;
751-
judge->curPlayer = -1;
752-
judge->loadState = 'G';
746+
endGame('G');
753747
}
754748
void GameWidget::on_restartButton_clicked_OL()
755749
{
@@ -849,12 +843,6 @@ void GameWidget::on_loadButton_clicked()
849843

850844
if(strState) // 是否为终局
851845
{
852-
// if(judge->loadState == 'W')
853-
// gameWin(judge->runMode);
854-
// if(judge->loadState == 'L')
855-
// gameLose(judge->runMode);
856-
// if(judge->loadState == 'G')
857-
// on_resignButton_clicked_OFFL();
858846
judge->init();
859847
ui->saveButton->setEnabled(0);
860848
turn_on_review();

Widget/gamewidget.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class GameWidget : public QWidget
4646
public slots:
4747
void startGame(int player); // 进入 gameWidget 时首先调用 startGame
4848
void firstMove(int player);
49+
void endGame(char loadState);
4950
void startTimer(); // 当对局开始时,无论谁先手(机器下第一个棋子认为不需要时间),都打开玩家的计时器
5051
void stopTimer();
5152
void updateCB(); // 更新棋盘
@@ -66,7 +67,6 @@ private slots:
6667
void botTimeout(); // bot超时的槽函数,用于链接计时器
6768
void clickToCloseMB(bool force = false); // 再次点击棋盘时关闭消息弹窗
6869
void updateBar(); // 更新倒计时进度条
69-
7070
void remoteResign(); // 联机认输
7171

7272
void on_try();
@@ -83,8 +83,6 @@ private slots:
8383
void setColorForBar(); // 计时器的颜色与当前执棋颜色一致
8484

8585
void mousePressEvent(QMouseEvent *event) override; // 监听鼠标坐标
86-
void gameLose(int type = 0); // 输掉游戏(0->PVE 1->PVP)
87-
void gameWin(int type = 0); // 赢了游戏(0->PVE 1->PVP)
8886

8987
void dataToString(); // 编码 (playerRole:0->w/2->b) (runMode:0->PVE/PVP/server/client)
9088
void stringToData(); // 解码

Widget/startwidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ StartWidget::StartWidget(Judge *j, QWidget *parent) :
4646
connect(ui->startAsBlack, &QPushButton::clicked, this, &StartWidget::on_startAsBlack_clicked_OFFL);
4747
connect(ui->startAsWhite, &QPushButton::clicked, this, &StartWidget::on_startAsWhite_clicked_OFFL);
4848
connect(confirmD, &OptionDialog::OK, this, [&](){
49-
if(judge->curPlayer == -1) // on GameWidget
49+
if(judge->playerRole != 0) // on GameWidget
5050
emit switchLayer(0);
5151
if(oppoRole == 1) {sendStartAsWhite(true); on_startAsWhite_clicked_OFFL();}
5252
else {sendStartAsBlack(true); on_startAsBlack_clicked_OFFL();}

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main(int argc, char *argv[])
2222

2323
// 信号槽
2424
QObject::connect(startWidget, &StartWidget::switchLayer, stackLayout, &QStackedLayout::setCurrentIndex);
25-
QObject::connect(startWidget, &StartWidget::switchLayer, gameWidget, [&](int layer){if(layer == 0) gameWidget->close();});
25+
QObject::connect(startWidget, &StartWidget::switchLayer, gameWidget, [&](int layer){if(layer == 0){gameWidget->close();}});
2626
QObject::connect(startWidget, &StartWidget::startAs, judge, &Judge::setPlayerRole);
2727
QObject::connect(startWidget, &StartWidget::startAs, gameWidget, &GameWidget::startGame);
2828
QObject::connect(startWidget->settingDialog, &SettingDialog::goOL, gameWidget, &GameWidget::goOL);

0 commit comments

Comments
 (0)