position: 解决 QtUI 切规则后对弈异常问题

原因是之前提交引入的:
// TODO: Do not need to set again
    set_position(DEFAULT_RULE_NUMBER);

解决方案:
set_position() 部分分离到新函数 set_rule() 其余则补充到 reset()。
This commit is contained in:
Calcitem 2020-12-12 23:05:54 +08:00
parent 0ac61a94d0
commit df68dafa8e
5 changed files with 28 additions and 49 deletions

View File

@ -169,8 +169,7 @@ Position::Position()
{ {
construct_key(); construct_key();
// TODO: Do not need to set again reset();
set_position(DEFAULT_RULE_NUMBER);
score[BLACK] = score[WHITE] = score_draw = nPlayed = 0; score[BLACK] = score[WHITE] = score_draw = nPlayed = 0;
@ -708,43 +707,6 @@ int Position::pieces_in_hand_count()
return pieceCountInHand[BLACK] + pieceCountInHand[WHITE]; 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<LEGAL>::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 #ifdef THREEFOLD_REPETITION
extern int nRepetition; extern int nRepetition;
#endif // THREEFOLD_REPETITION #endif // THREEFOLD_REPETITION
@ -773,6 +735,9 @@ bool Position::reset()
pieceCountInHand[BLACK] = pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide; pieceCountInHand[BLACK] = pieceCountInHand[WHITE] = rule.nTotalPiecesEachSide;
pieceCountNeedRemove = 0; pieceCountNeedRemove = 0;
millListSize = 0; millListSize = 0;
MoveList<LEGAL>::create();
create_mill_table();
currentSquare = SQ_0; currentSquare = SQ_0;
#ifdef ENDGAME_LEARNING #ifdef ENDGAME_LEARNING
@ -781,15 +746,14 @@ bool Position::reset()
} }
#endif /* ENDGAME_LEARNING */ #endif /* ENDGAME_LEARNING */
int i; int r;
for (r = 0; r < N_RULES; r++) {
for (i = 0; i < N_RULES; i++) { if (strcmp(rule.name, RULES[r].name) == 0)
if (strcmp(rule.name, RULES[i].name) == 0)
break; break;
} }
if (sprintf(cmdline, "r%1u s%03u t%02u", if (sprintf(cmdline, "r%1u s%03u t%02u",
i + 1, rule.maxStepsLedToDraw, 0) > 0) { r + 1, rule.maxStepsLedToDraw, 0) > 0) {
return true; return true;
} }
@ -1073,11 +1037,11 @@ bool Position::command(const char *cmd)
int args = 0; int args = 0;
if (sscanf(cmd, "r%1u s%3d t%2u", &ruleNo, &step, &t) == 3) { 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 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); args = sscanf(cmd, "(%1u,%1u)->(%1u,%1u)", (unsigned*)&file1, (unsigned*)&rank1, (unsigned*)&file2, (unsigned*)&rank2);

View File

@ -104,8 +104,6 @@ public:
/// Mill Game /// Mill Game
int set_position(int ruleNo);
Piece *get_board(); Piece *get_board();
Square current_square() const; Square current_square() const;
enum Phase get_phase() const; enum Phase get_phase() const;

View File

@ -125,3 +125,15 @@ const struct Rule RULES[N_RULES] = {
50 // 50步 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;
}

View File

@ -42,5 +42,6 @@ struct Rule
#define N_RULES 4 #define N_RULES 4
extern const struct Rule RULES[N_RULES]; extern const struct Rule RULES[N_RULES];
extern struct Rule rule; extern struct Rule rule;
extern bool set_rule(int ruleIdx);
#endif /* RULE_H */ #endif /* RULE_H */

View File

@ -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; elapsedSeconds[BLACK] = elapsedSeconds[WHITE] = 0;
char cmdline[64] = { 0 }; char cmdline[64] = { 0 };