From 188f6f8d3a2983187f0ffb116d64573d499acadd Mon Sep 17 00:00:00 2001 From: Sebastian Lague Date: Mon, 24 Jul 2023 22:51:54 +0200 Subject: [PATCH] Expose insufficient material and repeated position functions --- Chess-Challenge/src/API/Board.cs | 35 ++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Chess-Challenge/src/API/Board.cs b/Chess-Challenge/src/API/Board.cs index 3b692ff..d617a2a 100644 --- a/Chess-Challenge/src/API/Board.cs +++ b/Chess-Challenge/src/API/Board.cs @@ -167,25 +167,38 @@ namespace ChessChallenge.API /// public bool IsInCheckmate() => IsInCheck() && GetLegalMoves().Length == 0; - /// - /// Test if the current position is a draw due stalemate, - /// 3-fold repetition, insufficient material, or 50-move rule. - /// - public bool IsDraw() + /// + /// Test if the current position is a draw due stalemate, repetition, insufficient material, or 50-move rule. + /// Note: this function will return true if the same position has occurred twice on the board (rather than 3 times, + /// which is when the game is actually drawn). This quirk is to help bots avoid repeating positions unnecessarily. + /// + public bool IsDraw() { - return IsFiftyMoveDraw() || Arbiter.InsufficentMaterial(board) || IsInStalemate() || IsRepetition(); + return IsFiftyMoveDraw() || IsInsufficientMaterial() || IsInStalemate() || IsRepeatedPosition(); bool IsInStalemate() => !IsInCheck() && GetLegalMoves().Length == 0; bool IsFiftyMoveDraw() => board.currentGameState.fiftyMoveCounter >= 100; - bool IsRepetition() => repetitionHistory.Contains(board.ZobristKey); } /// - /// Does the given player still have the right to castle kingside? - /// Note that having the right to castle doesn't necessarily mean castling is legal right now - /// (for example, a piece might be in the way, or player might be in check, etc). + /// Test if the current position has occurred at least once before on the board. + /// This includes both positions in the actual game, and positions reached by + /// making moves while the bot is thinking. /// - public bool HasKingsideCastleRight(bool white) => board.currentGameState.HasKingsideCastleRight(white); + public bool IsRepeatedPosition() => repetitionHistory.Contains(board.ZobristKey); + + /// + /// Test if there are sufficient pieces remaining on the board to potentially deliver checkmate. + /// If not, the game is automatically a draw. + /// + public bool IsInsufficientMaterial() => Arbiter.InsufficentMaterial(board); + + /// + /// Does the given player still have the right to castle kingside? + /// Note that having the right to castle doesn't necessarily mean castling is legal right now + /// (for example, a piece might be in the way, or player might be in check, etc). + /// + public bool HasKingsideCastleRight(bool white) => board.currentGameState.HasKingsideCastleRight(white); /// /// Does the given player still have the right to castle queenside?