diff --git a/NineChess/src/gamecontroller.cpp b/NineChess/src/gamecontroller.cpp index af97deb2..840d9616 100644 --- a/NineChess/src/gamecontroller.cpp +++ b/NineChess/src/gamecontroller.cpp @@ -203,7 +203,7 @@ void GameController::gameReset() remainingTime1 = remainingTime2 = 0; } else { // 将玩家的剩余时间置为限定时间 - remainingTime1 = remainingTime2 = timeLimit * 60000; + remainingTime1 = remainingTime2 = timeLimit * 60; } // 更新棋谱 @@ -213,7 +213,7 @@ void GameController::gameReset() currentRow = 0; // 发出信号通知主窗口更新LCD显示 - QTime qtime = QTime(0, 0, 0, 0).addMSecs(remainingTime1); + QTime qtime = QTime(0, 0, 0, 0).addSecs(remainingTime1); emit time1Changed(qtime.toString("hh:mm:ss")); emit time2Changed(qtime.toString("hh:mm:ss")); @@ -528,12 +528,12 @@ void GameController::timerEvent(QTimerEvent *event) // 如果规则要求计时,则time1和time2表示倒计时 if (timeLimit > 0) { // 玩家的剩余时间 - remainingTime1 = timeLimit * 60000 - remainingTime1; - remainingTime2 = timeLimit * 60000 - remainingTime2; + remainingTime1 = timeLimit * 60 - remainingTime1; + remainingTime2 = timeLimit * 60 - remainingTime2; } - qt1 = QTime(0, 0, 0, 0).addMSecs(remainingTime1); - qt2 = QTime(0, 0, 0, 0).addMSecs(remainingTime2); + qt1 = QTime(0, 0, 0, 0).addSecs(remainingTime1); + qt2 = QTime(0, 0, 0, 0).addSecs(remainingTime2); emit time1Changed(qt1.toString("hh:mm:ss")); emit time2Changed(qt2.toString("hh:mm:ss")); diff --git a/NineChess/src/gamecontroller.h b/NineChess/src/gamecontroller.h index d5b94b66..fc3e9710 100644 --- a/NineChess/src/gamecontroller.h +++ b/NineChess/src/gamecontroller.h @@ -239,11 +239,11 @@ private: // 规则限步数 NineChess::step_t stepsLimit; - // 玩家1剩余时间(毫秒) - int remainingTime1; + // 玩家1剩余时间(秒) + time_t remainingTime1; - // 玩家2剩余时间(毫秒) - int remainingTime2; + // 玩家2剩余时间(秒) + time_t remainingTime2; // 用于主窗口状态栏显示的字符串 QString message; diff --git a/NineChess/src/ninechess.cpp b/NineChess/src/ninechess.cpp index d7250525..4f1ca524 100644 --- a/NineChess/src/ninechess.cpp +++ b/NineChess/src/ninechess.cpp @@ -587,7 +587,7 @@ bool NineChess::start() // 如果游戏处于未开始状态 case GAME_NOTSTARTED: // 启动计时器 - ftime(&startTimeb); + startTimeb = time(NULL); // 进入开局状态 context.stage = GAME_PLACING; return true; @@ -710,7 +710,7 @@ bool NineChess::place(int pos, int time_p, int8_t cp) if (cp) { player_ms = update(time_p); sprintf(cmdline, "(%1u,%1u) %02u:%02u.%03u", - c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); + c, p, player_ms / 60, (player_ms % 60), 0); cmdlist.emplace_back(string(cmdline)); currentStep++; } @@ -792,7 +792,7 @@ bool NineChess::place(int pos, int time_p, int8_t cp) if (cp) { player_ms = update(time_p); sprintf(cmdline, "(%1u,%1u)->(%1u,%1u) %02u:%02u.%03u", currentPos / N_SEATS, currentPos % N_SEATS + 1, - c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); + c, p, player_ms / 60, (player_ms % 60), 0); cmdlist.emplace_back(string(cmdline)); currentStep++; moveStep++; @@ -919,7 +919,7 @@ bool NineChess::capture(int pos, int time_p, int8_t cp) if (cp) { player_ms = update(time_p); - sprintf(cmdline, "-(%1u,%1u) %02u:%02u.%03u", c, p, player_ms / 60000, (player_ms % 60000) / 1000, player_ms % 1000); + sprintf(cmdline, "-(%1u,%1u) %02u:%02u.%03u", c, p, player_ms / 60, (player_ms % 60), 0); cmdlist.emplace_back(string(cmdline)); currentStep++; moveStep = 0; @@ -1094,7 +1094,7 @@ bool NineChess::command(const char *cmd) if (args >= 4) { if (args == 7) { if (mm >= 0 && ss >= 0 && mss >= 0) - tm = mm * 60000 + ss * 1000 + mss; + tm = mm * 60 + ss * mss; } if (choose(c1, p1)) { @@ -1109,7 +1109,7 @@ bool NineChess::command(const char *cmd) if (args >= 2) { if (args == 5) { if (mm >= 0 && ss >= 0 && mss >= 0) - tm = mm * 60000 + ss * 1000 + mss; + tm = mm * 60 + ss + mss; } return _capture(c1, p1, tm); } @@ -1119,7 +1119,7 @@ bool NineChess::command(const char *cmd) if (args >= 2) { if (args == 5) { if (mm >= 0 && ss >= 0 && mss >= 0) - tm = mm * 60000 + ss * 1000 + mss; + tm = mm * 60 + ss + mss; } return _place(c1, p1, tm); } @@ -1178,8 +1178,8 @@ bool NineChess::command(int move) inline int NineChess::update(int time_p /*= -1*/) { int ret = -1; - int *player_ms = (context.turn == PLAYER1 ? &elapsedMS_1 : &elapsedMS_2); - int playerNext_ms = (context.turn == PLAYER1 ? elapsedMS_2 : elapsedMS_1); + time_t *player_ms = (context.turn == PLAYER1 ? &elapsedMS_1 : &elapsedMS_2); + time_t playerNext_ms = (context.turn == PLAYER1 ? elapsedMS_2 : elapsedMS_1); // 根据局面调整计时器 @@ -1187,24 +1187,27 @@ inline int NineChess::update(int time_p /*= -1*/) return -1; } - ftime(¤tTimeb); +#if 1 + currentTimeb = time(NULL); // 更新时间 if (time_p >= *player_ms) { *player_ms = ret = time_p; - int t = elapsedMS_1 + elapsedMS_2; + time_t t = elapsedMS_1 + elapsedMS_2; - startTimeb.time = currentTimeb.time - (t / 1000); + startTimeb = currentTimeb - t; +#if 0 startTimeb.millitm = currentTimeb.millitm - (t % 1000); if (t % 1000 > currentTimeb.millitm) { startTimeb.time--; startTimeb.millitm += 1000; } +#endif } else { - *player_ms = ret = static_cast(currentTimeb.time - startTimeb.time) * 1000 - + (currentTimeb.millitm - startTimeb.millitm) - playerNext_ms; + *player_ms = ret = currentTimeb - startTimeb - playerNext_ms; } +#endif // 有限时要求则判断胜负 if (currentRule.maxTimeLedToLose > 0) { @@ -1237,15 +1240,15 @@ bool NineChess::win(bool forceDraw) // 这里不能update更新时间,否则会形成循环嵌套 // 如果玩家1超时 - if (elapsedMS_1 > currentRule.maxTimeLedToLose * 60000) { - elapsedMS_1 = currentRule.maxTimeLedToLose * 60000; + if (elapsedMS_1 > currentRule.maxTimeLedToLose * 60) { + elapsedMS_1 = currentRule.maxTimeLedToLose * 60; winner = PLAYER2; tips = "玩家1超时判负。"; sprintf(cmdline, "Time over. Player2 win!"); } // 如果玩家2超时 - else if (elapsedMS_2 > currentRule.maxTimeLedToLose * 60000) { - elapsedMS_2 = currentRule.maxTimeLedToLose * 60000; + else if (elapsedMS_2 > currentRule.maxTimeLedToLose * 60) { + elapsedMS_2 = currentRule.maxTimeLedToLose * 60; winner = PLAYER1; tips = "玩家2超时判负。"; sprintf(cmdline, "Time over. Player1 win!"); @@ -1640,7 +1643,7 @@ enum NineChess::Player NineChess::getWhosPiece(int c, int p) return NOBODY; } -void NineChess::getElapsedTimeMS(int &p1_ms, int &p2_ms) +void NineChess::getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms) { update(); diff --git a/NineChess/src/ninechess.h b/NineChess/src/ninechess.h index 4d97586b..bbc4c8a2 100644 --- a/NineChess/src/ninechess.h +++ b/NineChess/src/ninechess.h @@ -22,7 +22,7 @@ #ifndef NINECHESS #define NINECHESS -#include +//#include #include #include #include @@ -385,7 +385,7 @@ public: } // 玩家1和玩家2的用时 - void getElapsedTimeMS(int &p1_ms, int &p2_ms); + void getElapsedTimeMS(time_t &p1_ms, time_t &p2_ms); // 获取棋局的字符提示 const string getTips() const @@ -409,13 +409,13 @@ public: } // 获取开局时间 - timeb getStartTimeb() const + time_t getStartTimeb() const { return startTimeb; } // 重新设置开局时间 - void setStartTimeb(timeb stimeb) + void setStartTimeb(int stimeb) { startTimeb = stimeb; } @@ -561,16 +561,16 @@ private: int moveStep {}; // 游戏起始时间 - timeb startTimeb {}; + time_t startTimeb {}; // 当前游戏时间 - timeb currentTimeb {}; + time_t currentTimeb {}; - // 玩家1用时(毫秒) - int elapsedMS_1 {}; + // 玩家1用时(秒) + time_t elapsedMS_1 {}; - // 玩家2用时(毫秒) - int elapsedMS_2 {}; + // 玩家2用时(秒) + time_t elapsedMS_2 {}; /* 当前着法,AI会用到,如下表示 0x 00 00