user14163: 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, 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. :)

user825: You can do that easily in Python.

https://pypi.python.org/pypi/python-chess


Create a FEN position based on your loops
Make it white to move in the FEN
Use the FEN position to create Board (http://python-chess.readthedocs.io/en/latest/core.html#board)
Use the legal_moves function to generate all possible moves
Use the push function to make the move
Use the is_game_over function to check for checkmate
Use the pop function to undo the move
Go back to push until you have tried all legal moves
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:

Use API provided from https://syzygy-tables.info/apidoc?fen=4k3/8/8/8/8/8/8/4K3%20w%20-%20-%200%201.
If you prefer to do it locally, download Syzygy files from http://kirill-kryukov.com/chess/tablebases-online. Use chess.syzygy for probing.


14163: <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>
 825: <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>
