From a3854b94fbd70a6860bb7f4bceb3f2d37a9ec8f8 Mon Sep 17 00:00:00 2001 From: Calcitem Date: Sun, 20 Sep 2020 22:54:46 +0800 Subject: [PATCH] =?UTF-8?q?position:=20=E6=96=B0=E5=A2=9E=20set=5Fgameover?= =?UTF-8?q?()=20=E5=87=BD=E6=95=B0=E5=B9=B6=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/position.cpp | 29 +++++++++++++---------------- src/position.h | 1 + 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 3ea7ee22..2970fbd2 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -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; } } diff --git a/src/position.h b/src/position.h index ab083679..1eb76c4c 100644 --- a/src/position.h +++ b/src/position.h @@ -143,6 +143,7 @@ public: void change_side_to_move(); Color get_winner() const; + void set_gameover(Color w, GameOverReason reason); void mirror(vector &cmdlist, bool cmdChange = true); void turn(vector &cmdlist, bool cmdChange = true);