diff --git a/src/position.cpp b/src/position.cpp index d879b253..58650913 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -169,8 +169,7 @@ Position::Position() { construct_key(); - // TODO: Do not need to set again - set_position(DEFAULT_RULE_NUMBER); + reset(); score[BLACK] = score[WHITE] = score_draw = nPlayed = 0; @@ -708,43 +707,6 @@ int Position::pieces_in_hand_count() return pieceCountInHand[BLACK] + pieceCountInHand[WHITE]; } -int Position::set_position(int ruleNo) -{ - memset(&rule, 0, sizeof(Rule)); - memcpy(&rule, &RULES[ruleNo], sizeof(Rule)); - - gamePly = 0; - st.rule50 = 0; - - phase = PHASE_READY; - set_side_to_move(BLACK); - action = ACTION_PLACE; - - memset(board, 0, sizeof(board)); - st.key = 0; - memset(byTypeBB, 0, sizeof(byTypeBB)); - - if (pieces_on_board_count() == -1) { - return -1; - } - - pieces_in_hand_count(); - pieceCountNeedRemove = 0; - millListSize = 0; - winner = NOBODY; - MoveList::create(); - create_mill_table(); - currentSquare = SQ_0; - - int r; - for (r = 0; r < N_RULES; r++) { - if (strcmp(rule.name, RULES[r].name) == 0) - return r; - } - - return -1; -} - #ifdef THREEFOLD_REPETITION extern int nRepetition; #endif // THREEFOLD_REPETITION @@ -773,6 +735,9 @@ bool Position::reset() pieceCountInHand[BLACK] = pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide; pieceCountNeedRemove = 0; millListSize = 0; + + MoveList::create(); + create_mill_table(); currentSquare = SQ_0; #ifdef ENDGAME_LEARNING @@ -781,15 +746,14 @@ bool Position::reset() } #endif /* ENDGAME_LEARNING */ - int i; - - for (i = 0; i < N_RULES; i++) { - if (strcmp(rule.name, RULES[i].name) == 0) + int r; + for (r = 0; r < N_RULES; r++) { + if (strcmp(rule.name, RULES[r].name) == 0) break; } if (sprintf(cmdline, "r%1u s%03u t%02u", - i + 1, rule.maxStepsLedToDraw, 0) > 0) { + r + 1, rule.maxStepsLedToDraw, 0) > 0) { return true; } @@ -1073,11 +1037,11 @@ bool Position::command(const char *cmd) int args = 0; if (sscanf(cmd, "r%1u s%3d t%2u", &ruleNo, &step, &t) == 3) { - if (ruleNo <= 0 || ruleNo > N_RULES) { + if (set_rule(ruleNo - 1) == false) { return false; } - return set_position(ruleNo - 1) >= 0 ? true : false; + return reset(); } args = sscanf(cmd, "(%1u,%1u)->(%1u,%1u)", (unsigned*)&file1, (unsigned*)&rank1, (unsigned*)&file2, (unsigned*)&rank2); diff --git a/src/position.h b/src/position.h index be469468..da77a850 100644 --- a/src/position.h +++ b/src/position.h @@ -104,8 +104,6 @@ public: /// Mill Game - int set_position(int ruleNo); - Piece *get_board(); Square current_square() const; enum Phase get_phase() const; diff --git a/src/rule.cpp b/src/rule.cpp index f696ab69..61bd2d2e 100644 --- a/src/rule.cpp +++ b/src/rule.cpp @@ -125,3 +125,15 @@ const struct Rule RULES[N_RULES] = { 50 // 50步 } }; + +bool set_rule(int ruleIdx) +{ + if (ruleIdx <= 0 || ruleIdx > N_RULES) { + return false; + } + + memset(&rule, 0, sizeof(Rule)); + memcpy(&rule, &RULES[ruleIdx], sizeof(Rule)); + + return true; +} diff --git a/src/rule.h b/src/rule.h index d5f03431..c36e292b 100644 --- a/src/rule.h +++ b/src/rule.h @@ -42,5 +42,6 @@ struct Rule #define N_RULES 4 extern const struct Rule RULES[N_RULES]; extern struct Rule rule; +extern bool set_rule(int ruleIdx); #endif /* RULE_H */ diff --git a/src/ui/qt/game.cpp b/src/ui/qt/game.cpp index 66c6bd9a..31701fa3 100644 --- a/src/ui/qt/game.cpp +++ b/src/ui/qt/game.cpp @@ -353,7 +353,11 @@ void Game::setRule(int ruleNo, int stepLimited /*= -1*/, int timeLimited /*= -1* } // 设置模型规则,重置游戏 - int r = position.set_position(ruleNo); + if (set_rule(ruleNo) == false) { + return; + } + + int r = ruleNo; elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0; char cmdline[64] = { 0 };