Answers.Body: <p>What you are looking for is a quiescence search. Chess programming wiki has a helpful guide to it here: <a href="https://www.chessprogramming.org/Quiescence_Search" rel="nofollow noreferrer">https://www.chessprogramming.org/Quiescence_Search</a>. A quiescence search is a search that searches only captures. When depth reaches 0 in your search, instead of returning the static evaluation of a position, return the output from a quiescence search. Here is the pseudocode (assuming a minimax-esque search function)</p>
  <pre><code>func quiescence(alpha, beta) // no depth!
      eval = static_eval()
      if eval &gt;= beta : return beta
      else if eval &lt; alpha : alpha = eval
      capture_moves = generate_capture_moves()
      for move in capture_moves:
          push(move)
          eval = -quiescence(-beta, -alpha)
          unpush(move)
          if eval &gt;= beta : return beta
          else if eval &lt; alpha : alpha = eval
      return alpha
  </code></pre>
  <p>If you search until a position with no captures, this problem does not arise.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-07-22T03:59:30.883Z
Answers.LastActivityDate: 2023-07-22T03:59:30.883Z
Answers.UserDisplayName: BanjoMan
Body: <p>So I’m making a chess engine and have an evaluation function that has material as (a large) part of the eval weighting. But the eval function (as if depth is 1) will think that one side is up a piece if a defended piece is taken and hasn’t been taken back yet.</p>
  <p>You might think that’s not a problem as this is why we have search depth, but I have the issue that if the search ends with ‘half’ a trade then the program thinks this is a bad move when maybe it isn’t.</p>
  <p>How do I implement something in the eval function to let it know the position is still equal.</p>
  
Tags: <engines><programming>
Title: How to prevent evaluation function thinking better position after defended piece is taken?
ThreadText: # How to prevent evaluation function thinking better position after defended piece is taken?
  
  (Asked by: username_0 on 2023-07-22)
  
  So I’m making a chess engine and have an evaluation function that has material as (a large) part of the eval weighting. But the eval function (as if depth is 1) will think that one side is up a piece if a defended piece is taken and hasn’t been taken back yet.
  
  You might think that’s not a problem as this is why we have search depth, but I have the issue that if the search ends with ‘half’ a trade then the program thinks this is a bad move when maybe it isn’t.
  
  How do I implement something in the eval function to let it know the position is still equal.
  
  **Tags:** engines, programming
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-07-22. Score: 2 | Accepted answer
  
  What you are looking for is a quiescence search. Chess programming wiki has a helpful guide to it here: <https://www.chessprogramming.org/Quiescence_Search>. A quiescence search is a search that searches only captures. When depth reaches 0 in your search, instead of returning the static evaluation of a position, return the output from a quiescence search. Here is the pseudocode (assuming a minimax-esque search function)
  
  ```
  func quiescence(alpha, beta) // no depth!
      eval = static_eval()
      if eval >= beta : return beta
      else if eval < alpha : alpha = eval
      capture_moves = generate_capture_moves()
      for move in capture_moves:
          push(move)
          eval = -quiescence(-beta, -alpha)
          unpush(move)
          if eval >= beta : return beta
          else if eval < alpha : alpha = eval
      return alpha
  
  ```
  
  If you search until a position with no captures, this problem does not arise.
  
  
  ---
  
  