@@ -94,11 +94,9 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
94
94
connect (timerForPlayer,&QTimer::timeout, this , &GameWidget::playerTimeout_OFFL);
95
95
connect (timerForBar,&QTimer::timeout,this ,&GameWidget::updateBar);
96
96
connect (judge, &Judge::GIVEUP_OP, this , &GameWidget::remoteResign);
97
- connect (judge, &Judge::TIMEOUT_END_OP, this , [&](){gameLose (1 );});
98
97
connect (judge, &Judge::SUICIDE_END_OP, this , [&](){
99
- gameWin (1 );
100
- clickToCloseMB (true );
101
98
sendMessage (3 );
99
+ endGame (' W' );
102
100
});
103
101
connect (judge, &Judge::CHAT_OP, this , [&](NetworkData d){
104
102
QString s = " <" + judge->oppoOL + " > : \n " + d.data1 ;
@@ -107,9 +105,27 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
107
105
connect (judge, &Judge::MOVE_OP, this , [&](){
108
106
if (autoControl->isToggled ()) autoPlayer->start ();
109
107
});
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
+ });
110
126
connect (autoControl, &SwitchControl::toggled, this , [&](bool checked){
111
127
if (checked && judge->curPlayer == 1 ) autoPlayer->start ();
112
- if (!checked) autoPlayer->terminate ();
128
+ if (!checked && autoPlayer-> isRunning () ) autoPlayer->terminate ();
113
129
});
114
130
connect (autoPlayer, &QThread::finished, this , [&](){
115
131
if (!autoControl->isToggled ()) return ;
@@ -122,7 +138,7 @@ GameWidget::GameWidget(Judge *j, QWidget *parent) :
122
138
if (autoControl->isToggled ()){
123
139
autoControl->setDisabled (true );
124
140
autoControl->setToggled (false );
125
- autoPlayer->terminate ();
141
+ if (autoPlayer-> isRunning ()) autoPlayer->terminate ();
126
142
}
127
143
});
128
144
connect (bot, &Bot::timeout, this , &GameWidget::botTimeout);
@@ -158,7 +174,6 @@ void GameWidget::mousePressEvent(QMouseEvent *event)
158
174
return ;
159
175
}
160
176
161
-
162
177
emit mousePress ();
163
178
if (judge->curPlayer == -1 ) return ; // 判断游戏结束
164
179
if (!judge->runMode && bot->isRunning ()) return ; // 等待 bot 落子
@@ -216,6 +231,18 @@ void GameWidget::firstMove(int player)
216
231
if (judge->runMode == 1 ) judge->curPlayer ^= 1 ;
217
232
}
218
233
}
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
+ }
219
246
void GameWidget::closeEvent (QCloseEvent* event)
220
247
{
221
248
stopTimer ();
@@ -227,10 +254,8 @@ void GameWidget::closeEvent(QCloseEvent* event)
227
254
mouse_disabled = 0 ;
228
255
229
256
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 ();
234
259
judge->init ();
235
260
}
236
261
@@ -471,34 +496,6 @@ void GameWidget::drawDemo(QPainter &painter) // 绘画 FYH
471
496
}
472
497
473
498
// 判胜负与计时器相关
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
- }
502
499
void GameWidget::stopTimer () {
503
500
timerForPlayer->stop ();
504
501
timerForBar->stop ();
@@ -513,15 +510,29 @@ void GameWidget::startTimer() {
513
510
basetime=clock ();
514
511
}
515
512
}
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
+ }
518
523
void GameWidget::playerTimeout_OL ()
519
524
{
520
525
// 发送 TIMEOUT_END_OP
521
526
if (!judge->curPlayer ) // 当前是等待响应的一方,那么对手超时己方胜利
522
527
{
523
528
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' );
525
536
}
526
537
}
527
538
@@ -695,16 +706,8 @@ void GameWidget::goOFFL()
695
706
}
696
707
void GameWidget::remoteResign ()
697
708
{
698
- stopTimer ();
699
709
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' );
708
711
}
709
712
710
713
void GameWidget::on_try ()
@@ -739,17 +742,8 @@ void GameWidget::on_restartButton_clicked_OFFL()
739
742
void GameWidget::on_resignButton_clicked_OFFL ()
740
743
{
741
744
if (judge->curPlayer == -1 ) return ;
742
-
743
- stopTimer ();
744
745
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' );
753
747
}
754
748
void GameWidget::on_restartButton_clicked_OL ()
755
749
{
@@ -849,12 +843,6 @@ void GameWidget::on_loadButton_clicked()
849
843
850
844
if (strState) // 是否为终局
851
845
{
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();
858
846
judge->init ();
859
847
ui->saveButton ->setEnabled (0 );
860
848
turn_on_review ();
0 commit comments