Answers.Body: <p>You can do that easily in Python.</p>
  
  <blockquote>
    <p><a href="https://pypi.python.org/pypi/python-chess" rel="nofollow noreferrer">https://pypi.python.org/pypi/python-chess</a></p>
  </blockquote>
  
  <ol>
  <li>Create a FEN position based on your loops</li>
  <li>Make it white to move in the FEN</li>
  <li>Use the FEN position to create <code>Board</code> (<a href="http://python-chess.readthedocs.io/en/latest/core.html#board" rel="nofollow noreferrer">http://python-chess.readthedocs.io/en/latest/core.html#board</a>)</li>
  <li>Use the <code>legal_moves</code> function to generate all possible moves</li>
  <li>Use the <code>push</code> function to make the move</li>
  <li>Use the <code>is_game_over</code> function to check for checkmate</li>
  <li>Use the <code>pop</code> function to undo the move</li>
  <li>Go back to <code>push</code> until you have tried all legal moves</li>
  <li>Now make it black to move in the FEN and repeat step three</li>
  </ol>
  
  <p><strong>PS:</strong> If you want to add tablebase support. There're two ways:</p>
  
  <ol>
  <li>Use API provided from <a href="https://syzygy-tables.info/apidoc?fen=4k3/8/8/8/8/8/8/4K3%20w%20-%20-%200%201" rel="nofollow noreferrer">https://syzygy-tables.info/apidoc?fen=4k3/8/8/8/8/8/8/4K3%20w%20-%20-%200%201</a>.</li>
  <li>If you prefer to do it locally, download Syzygy files from <a href="http://kirill-kryukov.com/chess/tablebases-online" rel="nofollow noreferrer">http://kirill-kryukov.com/chess/tablebases-online</a>. Use <a href="http://python-chess.readthedocs.io/en/latest/syzygy.html" rel="nofollow noreferrer">chess.syzygy</a> for probing.</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-12T08:49:53.773Z
Answers.Text: @Quuxplusone Ok. I edited.
Answers.UserDisplayName: SmallChess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-07T01:40:23.350Z
Answers.LastActivityDate: 2017-09-12T08:49:23.753Z
Answers.LastEditDate: 2017-09-12T08:49:23.753Z
Answers.UserDisplayName: SmallChess
Body: <p>I have quite a bit of programming background, and even game programming background, but have never looked at a real chess engine before.</p>
  
  <p>I recently stumbled on <a href="https://chess.stackexchange.com/questions/1232/how-many-such-positions-are-there-chess-math-checkmate-puzzle">this puzzle-question</a>, which asks for the number of four-piece positions where the next move (whosever it is) will be checkmate. The number has to be somewhere between 400 (the count found by the OP) and 381,225,600 (the total count of four-piece positions), so it ought to be easy to enumerate them all by computer.</p>
  
  <p>I'd like to make a quick program like this:</p>
  
  <pre><code>for white_king_pos in [0, 64):
    for black_king_pos in [0, 64):
      for white_piece_pos in [0, 64):
        for black_piece_pos in [0, 64):
          for white_piece_kind in {Q,B,N,R,P}:
            for black_piece_kind in {Q,B,N,R,P}:
              if next_move_wins_for(black) &amp;&amp; next_move_wins_for(white):
                count = count + 1
  </code></pre>
  
  <p>What library should I use in order to make writing the <code>next_move_wins_for</code> function as painless as possible?</p>
  
  <p>(My program is so simple that I don't care about the library's choice of programming language. But FWIW, my fluent languages are C++, Python, and C.)</p>
  
  <p>Edited to add: Same question for other ways of accomplishing the goal, e.g. tablebases. :)</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-06T21:22:16.333Z
Comments.Text: Have you considered just doing a query of the endgame tablebases? I suspect that would be quicker.
Comments.UserDisplayName: Herb
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-09-06T21:56:27.057Z
Comments.Text: @HerbWolfe: I had not, but if that's a better approach, then I have the same question about it. :)  You say "THE endgame tablebases" as if I would know what tablebases are, where to find them, how to query them, etc. I'm completely out of my depth here. :)
Comments.UserDisplayName: Quuxplusone
Tags: <engines><software><programming>
Title: Getting started in position-counting by computer
ThreadText: # Getting started in position-counting by computer
  
  (Asked by: username_0 on 2017-09-06)
  
  I have quite a bit of programming background, and even game programming background, but have never looked at a real chess engine before.
  
  I recently stumbled on [this puzzle-question](https://chess.stackexchange.com/questions/1232/how-many-such-positions-are-there-chess-math-checkmate-puzzle), which asks for the number of four-piece positions where the next move (whosever it is) will be checkmate. The number has to be somewhere between 400 (the count found by the OP) and 381,225,600 (the total count of four-piece positions), so it ought to be easy to enumerate them all by computer.
  
  I'd like to make a quick program like this:
  
  ```
  for white_king_pos in [0, 64):
    for black_king_pos in [0, 64):
      for white_piece_pos in [0, 64):
        for black_piece_pos in [0, 64):
          for white_piece_kind in {Q,B,N,R,P}:
            for black_piece_kind in {Q,B,N,R,P}:
              if next_move_wins_for(black) && next_move_wins_for(white):
                count = count + 1
  
  ```
  
  What library should I use in order to make writing the `next_move_wins_for` function as painless as possible?
  
  (My program is so simple that I don't care about the library's choice of programming language. But FWIW, my fluent languages are C++, Python, and C.)
  
  Edited to add: Same question for other ways of accomplishing the goal, e.g. tablebases. :)
  
  **Tags:** engines, software, programming
  
  > **Comments:**
  > **username_1**: Have you considered just doing a query of the endgame tablebases? I suspect that would be quicker.  
  > **username_0**: @HerbWolfe: I had not, but if that's a better approach, then I have the same question about it. :)  You say "THE endgame tablebases" as if I would know what tablebases are, where to find them, how to query them, etc. I'm completely out of my depth here. :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2017-09-07. Score: 3
  
  You can do that easily in Python.
  
  > <https://pypi.python.org/pypi/python-chess>
  
  1. Create a FEN position based on your loops
  2. Make it white to move in the FEN
  3. Use the FEN position to create `Board` (<http://python-chess.readthedocs.io/en/latest/core.html#board>)
  4. Use the `legal_moves` function to generate all possible moves
  5. Use the `push` function to make the move
  6. Use the `is_game_over` function to check for checkmate
  7. Use the `pop` function to undo the move
  8. Go back to `push` until you have tried all legal moves
  9. Now make it black to move in the FEN and repeat step three
  
  **PS:** If you want to add tablebase support. There're two ways:
  
  1. Use API provided from <https://syzygy-tables.info/apidoc?fen=4k3/8/8/8/8/8/8/4K3%20w%20-%20-%200%201>.
  2. If you prefer to do it locally, download Syzygy files from <http://kirill-kryukov.com/chess/tablebases-online>. Use [chess.syzygy](http://python-chess.readthedocs.io/en/latest/syzygy.html) for probing.
  
  > **Comments:**
  > **username_2**: @username_0 Ok. I edited.  
  
  ---
  
  