Answers.Body: <p>Partly building on ferdy's answer (and the comments to it). In Python this can be done as follows:</p>
  <ol>
  <li><p>Using the <code>python-chess</code> module, iterate through the games in your database. Docs:
  <a href="https://python-chess.readthedocs.io/en/latest/pgn.html" rel="nofollow noreferrer">https://python-chess.readthedocs.io/en/latest/pgn.html</a></p>
  <p><a href="https://python-chess.readthedocs.io/en/latest/core.html#board" rel="nofollow noreferrer">https://python-chess.readthedocs.io/en/latest/core.html#board</a></p>
  </li>
  <li><p>Now you could use tablebases, but another option is to use Stockfish. Download the Stockfish engine, and then use the <code>stockfish</code> python module:
  <a href="https://pypi.org/project/stockfish/" rel="nofollow noreferrer">https://pypi.org/project/stockfish/</a></p>
  <p>For each position in a game, get its FEN (using <code>python-chess</code>), and then set the <code>stockfish</code> module object's position to it (using the <code>set_fen_position</code> method). Finally, call <code>get_top_moves(2)</code> with the <code>stockfish</code> object.</p>
  </li>
  </ol>
  <p>Then, a position can be deemed critical if:</p>
  <p>a) The top move is winning (e.g., &gt;= +4 or &lt;= -4, for White's and Black's perspectives).</p>
  <p>b) The second top move is under an evaluation that's winning (e.g., &lt;= +0.5 (White isn't winning) or &gt;= -0.5 (Black isn't winning)).</p>
  <p>OR</p>
  <p>a) The top move is at least around equal (e.g., &gt;= -0.5 (White is at least close to equal) or &lt;= +0.5 (Black is at least close to equal)).</p>
  <p>b) The second top move is losing (e.g., &lt;= -4 (White is losing) or &gt;= +4 (Black is losing)).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T07:52:54.637Z
Answers.LastActivityDate: 2022-08-31T11:46:33.627Z
Answers.LastEditDate: 2022-08-31T11:46:33.627Z
Answers.UserDisplayName: Inertial Ignorance
Answers.Body: <p>Here is a typical procedure to save critical positions.</p>
  <ul>
  <li>For the given material configuration like kbpkb ... generate positions and save to FEN. To generate positions you can use python-chess library for example.</li>
  <li>Analyze the FEN with ending tablebase and save it if critical.</li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-03T18:05:19.047Z
Answers.Text: There are many endgame positions for a given material configuration. I am interested in a systematic and efficient way to find all of them.
Answers.UserDisplayName: Wilfred Montoya
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-04T06:16:07.073Z
Answers.Text: Python-chess can be systematic and efficient.
Answers.UserDisplayName: ferdy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T01:34:17.057Z
Answers.Text: Late to the party but https://python-chess.readthedocs.io/en/latest/pgn.html is the documentation for parsing pgns with python-chess.
Answers.UserDisplayName: Inertial Ignorance
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T02:56:17.430Z
Answers.Text: That is more than that, for example if you check the [board module](https://python-chess.readthedocs.io/en/latest/core.html#board) it can do many things.
Answers.UserDisplayName: ferdy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T07:40:05.550Z
Answers.Text: Yup, a board can be created for the starting positoin in a given game: `first_game = chess.pgn.read_game(pgn)` `board = first_game.board()`. And then the board can be updated by iterating through `first_game`'s .mainline_moves(), and sending each move as an argument in calls to the board's `push` member function. Docs go over this more clearly.
Answers.UserDisplayName: Inertial Ignorance
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T08:24:51.283Z
Answers.Text: Not necessarily from start position. We can construct an ending and/or piece configuration we like immediately with the use of `set_piece_at()`
Answers.UserDisplayName: ferdy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-02T05:06:33.527Z
Answers.LastActivityDate: 2022-04-02T05:06:33.527Z
Answers.UserDisplayName: ferdy
Answers.Body: <p>As posed, I think this is more of a “data structures and algorithms” question. I don’t think the chess content can be leveraged, except there are existing tables which can be mined.</p>
  <p>In compositions, it’s not just a single critical position which is interesting - rather it’s a chain of critical positions where with “best play by Black”, there is at least one chain of mostly unique White moves that lead to a “clearly won/drawn” position.</p>
  <p>If this line of thinking interests you, then there is definitely chess content, and there is also a clear retroanalytic approach to efficiently determine a sequence of critical positions.</p>
  <p>In this case it may well be worth constructing your own tablebase to allow you to explore the possibilities, particularly the notions of “clearly won” and “best defensive play”.</p>
  <p>Hope this helps: good luck!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-31T02:59:30.360Z
Answers.LastActivityDate: 2022-08-31T02:59:30.360Z
Answers.UserDisplayName: Laska
Body: <p>A winning position is called critical, if all moves - except one - draw or lose (assuming the opponent plays perfectly). Similarly, a drawn chess position is called critical, if all moves - except one - lose.</p>
  <p>Consider this critical endgame position as an example.</p>
  <pre><code>[FEN &quot;8/8/1B6/6P1/1b6/8/2K4k/8 w - - 0 1&quot;]
  </code></pre>
  <p>The only winning move is 1. Ba5!! while every other move draws.</p>
  <p>Using endgame tables it is possible to determine if a given position is critical. However, I am interested in a database with all critical endgames. Does such database exist? If not, how could I create one myself?</p>
  
Tags: <endgame><tablebases>
Title: Finding all critical endgames
ThreadText: # Finding all critical endgames
  
  (Asked by: username_0 on 2022-02-02)
  
  A winning position is called critical, if all moves - except one - draw or lose (assuming the opponent plays perfectly). Similarly, a drawn chess position is called critical, if all moves - except one - lose.
  
  Consider this critical endgame position as an example.
  
  ```
  [FEN "8/8/1B6/6P1/1b6/8/2K4k/8 w - - 0 1"]
  
  ```
  
  The only winning move is 1. Ba5!! while every other move draws.
  
  Using endgame tables it is possible to determine if a given position is critical. However, I am interested in a database with all critical endgames. Does such database exist? If not, how could I create one myself?
  
  **Tags:** endgame, tablebases
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2022-08-31. Score: 2 | Accepted answer
  
  Partly building on username_2's answer (and the comments to it). In Python this can be done as follows:
  
  1. Using the `python-chess` module, iterate through the games in your database. Docs:
     <https://python-chess.readthedocs.io/en/latest/pgn.html>
  
     <https://python-chess.readthedocs.io/en/latest/core.html#board>
  2. Now you could use tablebases, but another option is to use Stockfish. Download the Stockfish engine, and then use the `stockfish` python module:
     <https://pypi.org/project/stockfish/>
  
     For each position in a game, get its FEN (using `python-chess`), and then set the `stockfish` module object's position to it (using the `set_fen_position` method). Finally, call `get_top_moves(2)` with the `stockfish` object.
  
  Then, a position can be deemed critical if:
  
  a) The top move is winning (e.g., >= +4 or <= -4, for White's and Black's perspectives).
  
  b) The second top move is under an evaluation that's winning (e.g., <= +0.5 (White isn't winning) or >= -0.5 (Black isn't winning)).
  
  OR
  
  a) The top move is at least around equal (e.g., >= -0.5 (White is at least close to equal) or <= +0.5 (Black is at least close to equal)).
  
  b) The second top move is losing (e.g., <= -4 (White is losing) or >= +4 (Black is losing)).
  
  
  ---
  
  ### Answer by username_2 on 2022-04-02. Score: 3
  
  Here is a typical procedure to save critical positions.
  
  * For the given material configuration like kbpkb ... generate positions and save to FEN. To generate positions you can use python-chess library for example.
  * Analyze the FEN with ending tablebase and save it if critical.
  
  > **Comments:**
  > **username_0**: There are many endgame positions for a given material configuration. I am interested in a systematic and efficient way to find all of them.  
  > **username_2**: Python-chess can be systematic and efficient.  
  > **username_1**: Late to the party but https://python-chess.readthedocs.io/en/latest/pgn.html is the documentation for parsing pgns with python-chess.  
  > **username_2**: That is more than that, for example if you check the [board module](https://python-chess.readthedocs.io/en/latest/core.html#board) it can do many things.  
  > **username_1**: Yup, a board can be created for the starting positoin in a given game: `first_game = chess.pgn.read_game(pgn)` `board = first_game.board()`. And then the board can be updated by iterating through `first_game`'s .mainline_moves(), and sending each move as an argument in calls to the board's `push` member function. Docs go over this more clearly.  
  > **username_2**: Not necessarily from start position. We can construct an ending and/or piece configuration we like immediately with the use of `set_piece_at()`  
  
  ---
  
  ### Answer by username_3 on 2022-08-31. Score: 2
  
  As posed, I think this is more of a “data structures and algorithms” question. I don’t think the chess content can be leveraged, except there are existing tables which can be mined.
  
  In compositions, it’s not just a single critical position which is interesting - rather it’s a chain of critical positions where with “best play by Black”, there is at least one chain of mostly unique White moves that lead to a “clearly won/drawn” position.
  
  If this line of thinking interests you, then there is definitely chess content, and there is also a clear retroanalytic approach to efficiently determine a sequence of critical positions.
  
  In this case it may well be worth constructing your own tablebase to allow you to explore the possibilities, particularly the notions of “clearly won” and “best defensive play”.
  
  Hope this helps: good luck!
  
  
  ---
  
  