1

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

2
  • 2
    Have you considered just doing a query of the endgame tablebases? I suspect that would be quicker.
    – Herb
    Commented Sep 6, 2017 at 21:22
  • @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. :) Commented Sep 6, 2017 at 21:56

1 Answer 1

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 for probing.
1
  • @Quuxplusone Ok. I edited.
    – SmallChess
    Commented Sep 12, 2017 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.