Answers.Body: <p>Your approach is correct. Stockfish does it as well.</p>
  
  <blockquote>
    <p><a href="https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp" rel="nofollow noreferrer">https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp</a></p>
  </blockquote>
  
  <pre><code>if (rootMoves.empty())
  {
    rootMoves.emplace_back(MOVE_NONE);
    sync_cout &lt;&lt; "info depth 0 score "
              &lt;&lt; UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW)
              &lt;&lt; sync_endl;
   }
  </code></pre>
  
  <p>There shouldn't be much penalty in performance, because:</p>
  
  <ol>
  <li>You will always need to generate candidate moves anyway</li>
  <li>Your code should maintain a bitboard (or other data structure) for the pieces checking the king. You will always need this information in your evaluation.</li>
  </ol>
  
  <p>Consequently, what you're doing is just checking information that you already have.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-16T10:54:20.237Z
Answers.Text: Thank you. I thought it would be a better way. My engine can currently only reach a depth of 3 (1.5 ply?) which is not very good. Not using bitboards at the moment, the board is a class (in Python) with all move information and stuff in it.
Answers.UserDisplayName: eligolf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-17T13:44:26.177Z
Answers.Text: @eligolf Would that be possible to accept my answer?
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-30T14:59:44.123Z
Answers.Text: @eligolf Dear Eligolf, could you kindly have a look at this question: https://stackoverflow.com/questions/78404555/chess-endgame-engine-is-not-working-completely-correctly ?
Answers.UserDisplayName: user2925716
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-16T10:30:42.703Z
Answers.LastActivityDate: 2020-01-16T10:30:42.703Z
Answers.UserDisplayName: SmallChess
Body: <p>in my minimax function I have a first few lines looking something like this:</p>
  <pre><code>children = board.get_all_possible_moves(board)
  
  if depth = 0 or board.is_check_mate or board.is_draw:
      return None, evaluation(board)
  </code></pre>
  <p>The problem is that it takes time to calculate the children one &quot;extra time&quot; if the depth is 0 and the position is not draw or check mate. However, I want to know if the board state at depth 0 is check mate or draw.</p>
  <p>As of now I detect check mate and draw by the following logic:</p>
  <pre><code>if not possible_moves:
      if board.is_check:
          board.is_check_mate = True
      else:
          board.is_draw = True
  </code></pre>
  <p>Is there a better way of finding whether a posisiton is check mate or not? Without having to calculate the opponents possible moves first, to save time in the minimax recursive function.</p>
  
Tags: <engines><check><minimax>
Title: Minimax check(mate) detection
ThreadText: # Minimax check(mate) detection
  
  (Asked by: username_0 on 2020-01-16)
  
  in my minimax function I have a first few lines looking something like this:
  
  ```
  children = board.get_all_possible_moves(board)
  
  if depth = 0 or board.is_check_mate or board.is_draw:
      return None, evaluation(board)
  
  ```
  
  The problem is that it takes time to calculate the children one "extra time" if the depth is 0 and the position is not draw or check mate. However, I want to know if the board state at depth 0 is check mate or draw.
  
  As of now I detect check mate and draw by the following logic:
  
  ```
  if not possible_moves:
      if board.is_check:
          board.is_check_mate = True
      else:
          board.is_draw = True
  
  ```
  
  Is there a better way of finding whether a posisiton is check mate or not? Without having to calculate the opponents possible moves first, to save time in the minimax recursive function.
  
  **Tags:** engines, check, minimax
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-01-16. Score: 3 | Accepted answer
  
  Your approach is correct. Stockfish does it as well.
  
  > <https://github.com/official-stockfish/Stockfish/blob/master/src/search.cpp>
  
  ```
  if (rootMoves.empty())
  {
    rootMoves.emplace_back(MOVE_NONE);
    sync_cout << "info depth 0 score "
              << UCI::value(rootPos.checkers() ? -VALUE_MATE : VALUE_DRAW)
              << sync_endl;
   }
  
  ```
  
  There shouldn't be much penalty in performance, because:
  
  1. You will always need to generate candidate moves anyway
  2. Your code should maintain a bitboard (or other data structure) for the pieces checking the king. You will always need this information in your evaluation.
  
  Consequently, what you're doing is just checking information that you already have.
  
  > **Comments:**
  > **username_0**: Thank you. I thought it would be a better way. My engine can currently only reach a depth of 3 (1.5 ply?) which is not very good. Not using bitboards at the moment, the board is a class (in Python) with all move information and stuff in it.  
  > **username_1**: @username_0 Would that be possible to accept my answer?  
  > **username_2**: @username_0 Dear username_0, could you kindly have a look at this question: https://stackoverflow.com/questions/78404555/chess-endgame-engine-is-not-working-completely-correctly ?  
  
  ---
  
  