position: 新增 set_gameover() 函数并使用

This commit is contained in:
Calcitem 2020-09-20 22:54:46 +08:00
parent df58e02132
commit a3854b94fb
2 changed files with 14 additions and 16 deletions

View File

@ -780,9 +780,7 @@ bool Position::remove_piece(Square s, bool updateCmdlist)
pieceCountOnBoard[them]--;
if (pieceCountOnBoard[them] + pieceCountInHand[them] < rule.nPiecesAtLeast) {
winner = sideToMove;
phase = PHASE_GAMEOVER;
gameoverReason = LOSE_REASON_LESS_THAN_THREE;
set_gameover(sideToMove, LOSE_REASON_LESS_THAN_THREE);
return true;
}
@ -846,10 +844,8 @@ bool Position::resign(Color loser)
return false;
}
phase = PHASE_GAMEOVER;
set_gameover(~loser, LOSE_REASON_RESIGN);
winner = ~loser;
gameoverReason = LOSE_REASON_RESIGN;
//sprintf(cmdline, "Player%d give up!", loser);
update_score();
@ -918,6 +914,13 @@ Color Position::get_winner() const
return winner;
}
inline void Position::set_gameover(Color w, GameOverReason reason)
{
phase = PHASE_GAMEOVER;
gameoverReason = reason;
winner = w;
}
int Position::update()
{
int ret = -1;
@ -989,14 +992,10 @@ bool Position::check_gameover_condition()
#endif
if (pieceCountOnBoard[BLACK] + pieceCountOnBoard[WHITE] >= RANK_NB * FILE_NB) {
phase = PHASE_GAMEOVER;
if (rule.isBlackLosebutNotDrawWhenBoardFull) {
winner = WHITE;
gameoverReason = LOSE_REASON_BOARD_IS_FULL;
set_gameover(WHITE, LOSE_REASON_BOARD_IS_FULL);
} else {
winner = DRAW;
gameoverReason = DRAW_REASON_BOARD_IS_FULL;
set_gameover(DRAW, DRAW_REASON_BOARD_IS_FULL);
}
return true;
@ -1004,12 +1003,10 @@ bool Position::check_gameover_condition()
if (phase == PHASE_MOVING && action == ACTION_SELECT && is_all_surrounded()) {
if (rule.isLoseButNotChangeSideWhenNoWay) {
phase = PHASE_GAMEOVER;
gameoverReason = LOSE_REASON_NO_WAY;
winner = ~sideToMove;
set_gameover(~sideToMove, LOSE_REASON_NO_WAY);
return true;
} else {
change_side_to_move();
change_side_to_move(); // TODO: Need?
return false;
}
}

View File

@ -143,6 +143,7 @@ public:
void change_side_to_move();
Color get_winner() const;
void set_gameover(Color w, GameOverReason reason);
void mirror(vector <string> &cmdlist, bool cmdChange = true);
void turn(vector <string> &cmdlist, bool cmdChange = true);