From 4212e5b5d2a76765cee8b211459ca8a9b2f250c4 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Fri, 30 Aug 2019 00:15:54 +0800 Subject: [PATCH] =?UTF-8?q?time:=20=E4=BF=AE=E6=AD=A3=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E5=90=8D=E4=BB=A5=E9=85=8D=E5=90=88=E5=B0=86=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E7=94=B1=E6=AF=AB=E7=A7=92=E6=94=B9=E4=B8=BA=E7=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NineChess/src/gamecontroller.cpp | 4 +-- NineChess/src/ninechess.cpp | 60 +++++++++++++------------------- NineChess/src/ninechess.h | 17 +++++---- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/NineChess/src/gamecontroller.cpp b/NineChess/src/gamecontroller.cpp index 840d9616..b0409c4e 100644 --- a/NineChess/src/gamecontroller.cpp +++ b/NineChess/src/gamecontroller.cpp @@ -523,7 +523,7 @@ void GameController::timerEvent(QTimerEvent *event) static QTime qt1, qt2; // 玩家的已用时间 - chess_.getElapsedTimeMS(remainingTime1, remainingTime2); + chess_.getElapsedTime(remainingTime1, remainingTime2); // 如果规则要求计时,则time1和time2表示倒计时 if (timeLimit > 0) { @@ -925,7 +925,7 @@ bool GameController::stageChange(int row, bool forceUpdate) } // 下面这步关键,会让悔棋者承担时间损失 - chessTemp.setStartTimeb(chess_.getStartTimeb()); + chessTemp.setStartTime(chess_.getStartTimeb()); // 刷新棋局场景 updateScence(chessTemp); diff --git a/NineChess/src/ninechess.cpp b/NineChess/src/ninechess.cpp index 4f1ca524..59eca780 100644 --- a/NineChess/src/ninechess.cpp +++ b/NineChess/src/ninechess.cpp @@ -164,10 +164,10 @@ NineChess::NineChess(const NineChess &chess) board_ = context.board; currentPos = chess.currentPos; winner = chess.winner; - startTimeb = chess.startTimeb; - currentTimeb = chess.currentTimeb; - elapsedMS_1 = chess.elapsedMS_1; - elapsedMS_2 = chess.elapsedMS_2; + startTime = chess.startTime; + currentTime = chess.currentTime; + elapsedSeconds_1 = chess.elapsedSeconds_1; + elapsedSeconds_2 = chess.elapsedSeconds_2; move_ = chess.move_; memcpy(cmdline, chess.cmdline, sizeof(cmdline)); cmdlist = chess.cmdlist; @@ -186,10 +186,10 @@ NineChess &NineChess::operator=(const NineChess &chess) board_ = context.board; currentPos = chess.currentPos; winner = chess.winner; - startTimeb = chess.startTimeb; - currentTimeb = chess.currentTimeb; - elapsedMS_1 = chess.elapsedMS_1; - elapsedMS_2 = chess.elapsedMS_2; + startTime = chess.startTime; + currentTime = chess.currentTime; + elapsedSeconds_1 = chess.elapsedSeconds_1; + elapsedSeconds_2 = chess.elapsedSeconds_2; move_ = chess.move_; memcpy(cmdline, chess.cmdline, sizeof(cmdline)); cmdlist = chess.cmdlist; @@ -468,7 +468,7 @@ bool NineChess::setContext(const struct Rule *rule, step_t maxStepsLedToDraw, in currentPos = 0; // 用时置零 - elapsedMS_1 = elapsedMS_2 = 0; + elapsedSeconds_1 = elapsedSeconds_2 = 0; // 提示 setTips(); @@ -505,7 +505,7 @@ void NineChess::getContext(struct Rule &rule, step_t &step, int &flags, bool NineChess::reset() { - if (context.stage == GAME_NOTSTARTED && elapsedMS_1 == elapsedMS_2 == 0) + if (context.stage == GAME_NOTSTARTED && elapsedSeconds_1 == elapsedSeconds_2 == 0) return true; // 步数归零 @@ -543,7 +543,7 @@ bool NineChess::reset() currentPos = 0; // 用时置零 - elapsedMS_1 = elapsedMS_2 = 0; + elapsedSeconds_1 = elapsedSeconds_2 = 0; #if ((defined HASH_MAP_ENABLE) || (defined BOOK_LEARNING) || (defined THREEFOLD_REPETITION)) // 哈希归零 @@ -587,7 +587,7 @@ bool NineChess::start() // 如果游戏处于未开始状态 case GAME_NOTSTARTED: // 启动计时器 - startTimeb = time(NULL); + startTime = time(NULL); // 进入开局状态 context.stage = GAME_PLACING; return true; @@ -1178,8 +1178,8 @@ bool NineChess::command(int move) inline int NineChess::update(int time_p /*= -1*/) { int ret = -1; - time_t *player_ms = (context.turn == PLAYER1 ? &elapsedMS_1 : &elapsedMS_2); - time_t playerNext_ms = (context.turn == PLAYER1 ? elapsedMS_2 : elapsedMS_1); + time_t *player_ms = (context.turn == PLAYER1 ? &elapsedSeconds_1 : &elapsedSeconds_2); + time_t playerNext_ms = (context.turn == PLAYER1 ? elapsedSeconds_2 : elapsedSeconds_1); // 根据局面调整计时器 @@ -1187,27 +1187,15 @@ inline int NineChess::update(int time_p /*= -1*/) return -1; } -#if 1 - currentTimeb = time(NULL); + currentTime = time(NULL); // 更新时间 if (time_p >= *player_ms) { *player_ms = ret = time_p; - time_t t = elapsedMS_1 + elapsedMS_2; - - startTimeb = currentTimeb - t; -#if 0 - startTimeb.millitm = currentTimeb.millitm - (t % 1000); - - if (t % 1000 > currentTimeb.millitm) { - startTimeb.time--; - startTimeb.millitm += 1000; - } -#endif + startTime = currentTime - (elapsedSeconds_1 + elapsedSeconds_2); } else { - *player_ms = ret = currentTimeb - startTimeb - playerNext_ms; + *player_ms = ret = currentTime - startTime - playerNext_ms; } -#endif // 有限时要求则判断胜负 if (currentRule.maxTimeLedToLose > 0) { @@ -1240,15 +1228,15 @@ bool NineChess::win(bool forceDraw) // 这里不能update更新时间,否则会形成循环嵌套 // 如果玩家1超时 - if (elapsedMS_1 > currentRule.maxTimeLedToLose * 60) { - elapsedMS_1 = currentRule.maxTimeLedToLose * 60; + if (elapsedSeconds_1 > currentRule.maxTimeLedToLose * 60) { + elapsedSeconds_1 = currentRule.maxTimeLedToLose * 60; winner = PLAYER2; tips = "玩家1超时判负。"; sprintf(cmdline, "Time over. Player2 win!"); } // 如果玩家2超时 - else if (elapsedMS_2 > currentRule.maxTimeLedToLose * 60) { - elapsedMS_2 = currentRule.maxTimeLedToLose * 60; + else if (elapsedSeconds_2 > currentRule.maxTimeLedToLose * 60) { + elapsedSeconds_2 = currentRule.maxTimeLedToLose * 60; winner = PLAYER1; tips = "玩家2超时判负。"; sprintf(cmdline, "Time over. Player1 win!"); @@ -1643,12 +1631,12 @@ enum NineChess::Player NineChess::getWhosPiece(int c, int p) return NOBODY; } -void NineChess::getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms) +void NineChess::getElapsedTime(time_t &p1_ms, time_t &p2_ms) { update(); - p1_ms = elapsedMS_1; - p2_ms = elapsedMS_2; + p1_ms = elapsedSeconds_1; + p2_ms = elapsedSeconds_2; } void NineChess::mirror(bool cmdChange /*= true*/) diff --git a/NineChess/src/ninechess.h b/NineChess/src/ninechess.h index bbc4c8a2..bf70a1cc 100644 --- a/NineChess/src/ninechess.h +++ b/NineChess/src/ninechess.h @@ -22,7 +22,6 @@ #ifndef NINECHESS #define NINECHESS -//#include #include #include #include @@ -385,7 +384,7 @@ public: } // 玩家1和玩家2的用时 - void getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms); + void getElapsedTime(time_t &p1_ms, time_t &p2_ms); // 获取棋局的字符提示 const string getTips() const @@ -411,13 +410,13 @@ public: // 获取开局时间 time_t getStartTimeb() const { - return startTimeb; + return startTime; } // 重新设置开局时间 - void setStartTimeb(int stimeb) + void setStartTime(int stimeb) { - startTimeb = stimeb; + startTime = stimeb; } // 玩家1剩余未放置子数 @@ -561,16 +560,16 @@ private: int moveStep {}; // 游戏起始时间 - time_t startTimeb {}; + time_t startTime {}; // 当前游戏时间 - time_t currentTimeb {}; + time_t currentTime {}; // 玩家1用时(秒) - time_t elapsedMS_1 {}; + time_t elapsedSeconds_1 {}; // 玩家2用时(秒) - time_t elapsedMS_2 {}; + time_t elapsedSeconds_2 {}; /* 当前着法,AI会用到,如下表示 0x 00 00