refactor: 减少 time_p 传参

This commit is contained in:
Calcitem 2019-09-26 00:41:46 +08:00
parent 919d990517
commit 77881f817c
2 changed files with 21 additions and 19 deletions

View File

@ -311,7 +311,7 @@ bool Game::start()
} }
} }
bool Game::place(int location, int time_p, int8_t updateCmdlist) bool Game::place(int location, int8_t updateCmdlist)
{ {
// 如果局面为“结局”返回false // 如果局面为“结局”返回false
if (position.phase == PHASE_GAMEOVER) if (position.phase == PHASE_GAMEOVER)
@ -354,7 +354,7 @@ bool Game::place(int location, int time_p, int8_t updateCmdlist)
move_ = static_cast<move_t>(location); move_ = static_cast<move_t>(location);
if (updateCmdlist) { if (updateCmdlist) {
player_ms = update(time_p); player_ms = update();
sprintf(cmdline, "(%1u,%1u) %02u:%02u", sprintf(cmdline, "(%1u,%1u) %02u:%02u",
r, s, player_ms / 60, player_ms % 60); r, s, player_ms / 60, player_ms % 60);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
@ -434,7 +434,7 @@ bool Game::place(int location, int time_p, int8_t updateCmdlist)
move_ = static_cast<move_t>((currentLocation << 8) + location); move_ = static_cast<move_t>((currentLocation << 8) + location);
if (updateCmdlist) { if (updateCmdlist) {
player_ms = update(time_p); player_ms = update();
sprintf(cmdline, "(%1u,%1u)->(%1u,%1u) %02u:%02u", currentLocation / Board::N_SEATS, currentLocation % Board::N_SEATS + 1, sprintf(cmdline, "(%1u,%1u)->(%1u,%1u) %02u:%02u", currentLocation / Board::N_SEATS, currentLocation % Board::N_SEATS + 1,
r, s, player_ms / 60, player_ms % 60); r, s, player_ms / 60, player_ms % 60);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
@ -483,23 +483,23 @@ out:
return true; return true;
} }
bool Game::_place(int r, int s, int time_p) bool Game::_place(int r, int s)
{ {
// 转换为 location // 转换为 location
int location = Board::polarToLocation(r, s); int location = Board::polarToLocation(r, s);
return place(location, time_p, true); return place(location, true);
} }
bool Game::_capture(int r, int s, int time_p) bool Game::_capture(int r, int s)
{ {
// 转换为 location // 转换为 location
int location = Board::polarToLocation(r, s); int location = Board::polarToLocation(r, s);
return capture(location, time_p, 1); return capture(location, 1);
} }
bool Game::capture(int location, int time_p, int8_t updateCmdlist) bool Game::capture(int location, int8_t updateCmdlist)
{ {
// 如果局面为"未开局"或“结局”返回false // 如果局面为"未开局"或“结局”返回false
if (position.phase == PHASE_NOTSTARTED || position.phase == PHASE_GAMEOVER) if (position.phase == PHASE_NOTSTARTED || position.phase == PHASE_GAMEOVER)
@ -549,7 +549,7 @@ bool Game::capture(int location, int time_p, int8_t updateCmdlist)
move_ = static_cast<move_t>(-location); move_ = static_cast<move_t>(-location);
if (updateCmdlist) { if (updateCmdlist) {
player_ms = update(time_p); player_ms = update();
sprintf(cmdline, "-(%1u,%1u) %02u:%02u", r, s, player_ms / 60, player_ms % 60); sprintf(cmdline, "-(%1u,%1u) %02u:%02u", r, s, player_ms / 60, player_ms % 60);
cmdlist.emplace_back(string(cmdline)); cmdlist.emplace_back(string(cmdline));
currentStep++; currentStep++;
@ -704,7 +704,6 @@ bool Game::command(const char *cmd)
int r1, s1, r2, s2; int r1, s1, r2, s2;
int args = 0; int args = 0;
int mm = 0, ss = 0; int mm = 0, ss = 0;
int tm = -1;
// 设置规则 // 设置规则
if (sscanf(cmd, "r%1u s%3hd t%2u", &r, &s, &t) == 3) { if (sscanf(cmd, "r%1u s%3hd t%2u", &r, &s, &t) == 3) {
@ -725,7 +724,7 @@ bool Game::command(const char *cmd)
} }
if (choose(r1, s1)) { if (choose(r1, s1)) {
return _place(r2, s2, tm); return _place(r2, s2);
} }
return false; return false;
@ -738,7 +737,7 @@ bool Game::command(const char *cmd)
if (mm >= 0 && ss >= 0) if (mm >= 0 && ss >= 0)
tm = mm * 60 + ss; tm = mm * 60 + ss;
} }
return _capture(r1, s1, tm); return _capture(r1, s1);
} }
// 落子 // 落子
@ -748,7 +747,7 @@ bool Game::command(const char *cmd)
if (mm >= 0 && ss >= 0) if (mm >= 0 && ss >= 0)
tm = mm * 60 + ss; tm = mm * 60 + ss;
} }
return _place(r1, s1, tm); return _place(r1, s1);
} }
// 认输 // 认输
@ -794,9 +793,10 @@ bool Game::command(int move)
return false; return false;
} }
inline int Game::update(int time_p /*= -1*/) inline int Game::update()
{ {
int ret = -1; int ret = -1;
int time_p = -1;
time_t *player_ms = &elapsedSeconds[position.sideId]; time_t *player_ms = &elapsedSeconds[position.sideId];
time_t playerNext_ms = elapsedSeconds[position.opponentId]; time_t playerNext_ms = elapsedSeconds[position.opponentId];

View File

@ -84,6 +84,8 @@ public:
int score[3]; int score[3];
int score_draw {}; int score_draw {};
int tm {-1};
private: private:
// 创建哈希值 // 创建哈希值
@ -217,10 +219,10 @@ public:
bool choose(int r, int s); bool choose(int r, int s);
// 落子在第r圈第s个位置为迎合日常r和s下标都从1开始 // 落子在第r圈第s个位置为迎合日常r和s下标都从1开始
bool _place(int r, int s, int time_p = -1); bool _place(int r, int s);
// 去子在第r圈第s个位置为迎合日常r和s下标都从1开始 // 去子在第r圈第s个位置为迎合日常r和s下标都从1开始
bool _capture(int r, int s, int time_p = -1); bool _capture(int r, int s);
// 认输 // 认输
bool giveup(player_t loser); bool giveup(player_t loser);
@ -229,7 +231,7 @@ public:
bool command(const char *cmd); bool command(const char *cmd);
// 更新时间和状态,用内联函数以提高效率 // 更新时间和状态,用内联函数以提高效率
inline int update(int time_p = -1); inline int update();
// 是否分出胜负 // 是否分出胜负
bool win(); bool win();
@ -250,8 +252,8 @@ public:
// 下面几个函数没有算法无关判断和无关操作,节约算法时间 // 下面几个函数没有算法无关判断和无关操作,节约算法时间
bool command(int move); bool command(int move);
bool choose(int location); bool choose(int location);
bool place(int location, int time_p = -1, int8_t cp = 0); bool place(int location, int8_t cp = 0);
bool capture(int location, int time_p = -1, int8_t cp = 0); bool capture(int location, int8_t cp = 0);
// hash 相关 // hash 相关
hash_t getHash(); hash_t getHash();