Answers.Body: <blockquote>
  <p>Are there any methods to encourage the engine to go for mate rather
  than a massive material imbalance?</p>
  </blockquote>
  <p>Any reasonably sophisticated position evaluation function is going to give extra points for certain positional elements (passed pawns, advanced pawns, bishop pair, etc.) and deduct points for other negative positional elements (doubled pawns, isolated pawns, bad bishop, unsafe king, etc)</p>
  <p>Obviously any halfway decent evaluation function is also going to handle positional elements like checkmate (add MAXINT), stalemate (assign 0.0), 3-fold repetition (0.0), 50 move rule (0.0), insufficient material (0.0), etc.</p>
  <p>If all your evaluation function does is use piece values and nothing else then you still have a lot of work to do to have an engine that is any good.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-17T00:04:20.627Z
Answers.Text: Yep- to solve this precise issue, what you'd want to do is have any position with mate have an evaluation of  the maximum value your language allows (generally there'll be some environment constant like MAXINT or math.MAXINT or whatever)
Answers.UserDisplayName: pulsar512b
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-17T11:56:35.117Z
Answers.Text: Thank you for the MAXINT tip it appears to work (makes engine go for mate). However, is there a method to efficiently determine if a position is mate. I implemented the obvious way, finding zero legal moves, but this if of course extremely slow.
Answers.UserDisplayName: operator
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-16T13:20:09.863Z
Answers.LastActivityDate: 2020-12-16T13:20:09.863Z
Answers.UserDisplayName: Brian Towers
Body: <p>I have made a chess engine that can play a decent game of chess. Like a lot of engines it uses a minimax search algorithm with alpha-beta pruning. It uses this <a href="https://www.chessprogramming.org/Simplified_Evaluation_Function" rel="nofollow noreferrer">simple evaluation function</a> to assign a score. The problem I have is that the engine does not attempt to go for checkmate. Against Stockfish, it will attempt to capture all the enemy pieces and then get a queen via pawn promotion, presumably to maximize its score. It sometimes stumbles upon a mate position, but it is more common to result in stalemate even with multiple pieces (queen, rooks, bishops) vs a lone king. I think this is because my algorithm currently only encourages maximizing the difference in score (white vs black) and has no concept of mate. One idea I had is increasing the piece value of the king to encourage mate. Are there any methods to encourage the engine to go for mate rather than a massive material imbalance?</p>
  
Tags: <engines><checkmate><computer-chess>
Title: Chess engine does not go for checkmate
ThreadText: # Chess engine does not go for checkmate
  
  (Asked by: username_0 on 2020-12-16)
  
  I have made a chess engine that can play a decent game of chess. Like a lot of engines it uses a minimax search algorithm with alpha-beta pruning. It uses this [simple evaluation function](https://www.chessprogramming.org/Simplified_Evaluation_Function) to assign a score. The problem I have is that the engine does not attempt to go for checkmate. Against Stockfish, it will attempt to capture all the enemy pieces and then get a queen via pawn promotion, presumably to maximize its score. It sometimes stumbles upon a mate position, but it is more common to result in stalemate even with multiple pieces (queen, rooks, bishops) vs a lone king. I think this is because my algorithm currently only encourages maximizing the difference in score (white vs black) and has no concept of mate. One idea I had is increasing the piece value of the king to encourage mate. Are there any methods to encourage the engine to go for mate rather than a massive material imbalance?
  
  **Tags:** engines, checkmate, computer-chess
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-12-16. Score: 4
  
  > Are there any methods to encourage the engine to go for mate rather
  > than a massive material imbalance?
  
  Any reasonably sophisticated position evaluation function is going to give extra points for certain positional elements (passed pawns, advanced pawns, bishop pair, etc.) and deduct points for other negative positional elements (doubled pawns, isolated pawns, bad bishop, unsafe king, etc)
  
  Obviously any halfway decent evaluation function is also going to handle positional elements like checkmate (add MAXINT), stalemate (assign 0.0), 3-fold repetition (0.0), 50 move rule (0.0), insufficient material (0.0), etc.
  
  If all your evaluation function does is use piece values and nothing else then you still have a lot of work to do to have an engine that is any good.
  
  > **Comments:**
  > **username_2**: Yep- to solve this precise issue, what you'd want to do is have any position with mate have an evaluation of  the maximum value your language allows (generally there'll be some environment constant like MAXINT or math.MAXINT or whatever)  
  > **username_0**: Thank you for the MAXINT tip it appears to work (makes engine go for mate). However, is there a method to efficiently determine if a position is mate. I implemented the obvious way, finding zero legal moves, but this if of course extremely slow.  
  
  ---
  
  