Answers.Body: <p>I don't understand why this is an issue. What you're asking is just chess engine search. Nothing more.</p>
  
  <ol>
  <li>Generate all possible moves to a list</li>
  <li>Make a move to the position</li>
  <li>Search the new position with Stockfish (the <code>search&lt;&gt;</code> function)</li>
  <li>Check the returned negamax value if the move leads to forced mate</li>
  <li>Undo the move</li>
  <li>Get the next move from the list, go back to (2) and repeat </li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-20T11:27:20.403Z
Answers.LastActivityDate: 2017-07-20T11:27:20.403Z
Answers.UserDisplayName: SmallChess
Body: <p>I'm working on a function that can forecast whether, for a given position, I can be checkmated by the opponent. I have this function, already in Stockfish:</p>
  
  <pre><code>bool Position::is_mate() const {
  
    MoveStack moves[MAX_MOVES];
    return in_check() &amp;&amp; generate&lt;MV_LEGAL&gt;(*this, moves) == moves;
  }
  </code></pre>
  
  <p>This tells me that I am already being checkmated. Ideally, I would like to write a function:</p>
  
  <pre><code>bool Position::is_trap() const {
    for all of the opponent's legal moves
      if move i results in is_mate()
        return true
  
    return false
  }
  </code></pre>
  
  <p>Both of these functions will be in position.cpp.</p>
  
  <p>Subsequently, I want to generate a list of all legal moves that will result in is_trap() being true (all legal moves that will result in me being checkmated in the next turn). In movegen.cpp I have:</p>
  
  <pre><code>template&lt;&gt;
  MoveStack* generate&lt;TRAP&gt;(const Position&amp; pos, MoveStack* mlist) {
  
    assert(pos.is_ok());
    MoveStack *last, *cur = mlist;
    // Generate all legal moves
    last = generate&lt;MV_LEGAL&gt;(pos, mlist);
  
    // If a legal move isn't a trap, remove it
    while (cur != last)
      for (i = 0; i = MAX_MOVES; i++)
        pos-&gt;do_move(mlist[i].move);
        if (!pos.is_trap())
            cur-&gt;move = (--last)-&gt;move;
        else
            cur++;
        pos-&gt;undo_move(mlist[i].move);
  
    return last;
  }
  </code></pre>
  
  <p>Is there a more efficient way of doing this? Thank you for your help!</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-09T08:36:55.437Z
Comments.Text: Just get stockfish to search to depth 2 or 3 and you'll get pretty much all of the possible mates.
Comments.UserDisplayName: konsolas
Tags: <stockfish>
Title: Stockfish - Function that predicts checkmate
ThreadText: # Stockfish - Function that predicts checkmate
  
  (Asked by: username_0 on 2017-07-20)
  
  I'm working on a function that can forecast whether, for a given position, I can be checkmated by the opponent. I have this function, already in Stockfish:
  
  ```
  bool Position::is_mate() const {
  
    MoveStack moves[MAX_MOVES];
    return in_check() && generate<MV_LEGAL>(*this, moves) == moves;
  }
  
  ```
  
  This tells me that I am already being checkmated. Ideally, I would like to write a function:
  
  ```
  bool Position::is_trap() const {
    for all of the opponent's legal moves
      if move i results in is_mate()
        return true
  
    return false
  }
  
  ```
  
  Both of these functions will be in position.cpp.
  
  Subsequently, I want to generate a list of all legal moves that will result in is\_trap() being true (all legal moves that will result in me being checkmated in the next turn). In movegen.cpp I have:
  
  ```
  template<>
  MoveStack* generate<TRAP>(const Position& pos, MoveStack* mlist) {
  
    assert(pos.is_ok());
    MoveStack *last, *cur = mlist;
    // Generate all legal moves
    last = generate<MV_LEGAL>(pos, mlist);
  
    // If a legal move isn't a trap, remove it
    while (cur != last)
      for (i = 0; i = MAX_MOVES; i++)
        pos->do_move(mlist[i].move);
        if (!pos.is_trap())
            cur->move = (--last)->move;
        else
            cur++;
        pos->undo_move(mlist[i].move);
  
    return last;
  }
  
  ```
  
  Is there a more efficient way of doing this? Thank you for your help!
  
  **Tags:** stockfish
  
  > **Comments:**
  > **username_1**: Just get stockfish to search to depth 2 or 3 and you'll get pretty much all of the possible mates.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-07-20. Score: 1
  
  I don't understand why this is an issue. What you're asking is just chess engine search. Nothing more.
  
  1. Generate all possible moves to a list
  2. Make a move to the position
  3. Search the new position with Stockfish (the `search<>` function)
  4. Check the returned negamax value if the move leads to forced mate
  5. Undo the move
  6. Get the next move from the list, go back to (2) and repeat
  
  
  ---
  
  