Sunfish: A simple but strong chess engine written in Python

7 years ago (github.com)

It would be fun to have 'demoscene' for chess engines and AI in general where computers to play chess with very restricted resources.

What is the smallest computer with superhuman cababilities?

  • It definitely isn't superhuman by any means, but I was deeply impressed when I learned about microchess, an early computer chess program that managed to run in 1K of ram in the KIM-1 microcomputer. It's a pretty easy program to beat if you play chess regularly. But if you are the kind of person that only plays chess sporadically and non-seriously, it can give a human a run for their money.

  • With advancements in saved/pretrained models I think answer is or will be a raspberry pi. I think a more interesting question is “what is the smallest computer that can learn chess”

    • When I was a kid I had a TI-83+ with a chess engine that could whoop my ass. Zilog z80 at 6MHz, a few KB of ram. Of course, I sucked at chess...

  • Also: What is the fewest required bits. With modern data based techniques, it's tempting to add a large data file that must be loaded with sunfish. However that kinda feels like cheating.

What's the deal with naming chess engines after fish? I tried to look up someplace to buy stockfish the other day and instead finding information about dried fish I found a bunch of information about the chess engine.

  • Stockfish was named that way because the two main authors are from Norway and north-eastern Italy. Oddly enough, in both places stockfish is a typical dish.

  • You might have had more luck searching in an incognito tab. If google thinks you are a programmer it may be more likely to display results related to the chess engine rather than the actual food. (Assuming you are using google and have done many programming related queries)

The screen shot could use a little work. Looks like the dots and letters don't line up with the pieces. Very cool though.

  • It's surprisingly hard to get browsers to use monospace with unicode. If anyone knows a trick, I'd love to learn about it!

This engine appears to have neither an interesting evaluation of positions, nor any of the performance tweaks that make traditional engines powerful. You're likely to learn more in an afternoon on https://www.chessprogramming.org, but all chess engines are beautiful in their own way, and I have certainly created nothing better.

  • It actually has quite a few performance tweaks that make traditional engines powerful.

    For starters:

    - A 12x10 Mailbox board representation.

    - Killer move heuristic.

    - QSearch.

    - Null move pruning.

    - Move sorting based on captures and positioning.

    - MTD-bi search.

    - Mate testing based on king capture.

    - Transposition tables.

    The Chess Programming Wiki is a great place to learn more about all of these though.

  • It's not meant to be a competitive engine, after all, it's written in 111 lines of python.

    I think it could be very useful for didactic purposes, or for getting started. Not having all the extra complexities needed for efficient board representation, position evaluation, move generation etc... makes the big picture much clearer imvho

    • Yeah, I totally agree that eval + search is the first step to learn, in terms of overall structure. And this includes lots of important concepts like quiescence search etc. I just think it's worth being honest: you either need really good eval or really fast search just to give _yourself_ a good game.

      2 replies →

I’m curious how the code is sped up with pypy and why that helps?

What does pypy bring to this situation?

  • PyPy is a JIT-based interpreter for Python - code that loops a lot over similar data structures is a natural candidate for being faster if jit-ed.

This is very cool. How does the chess engine work without some sort of AI?

  • It's debatable if this is AI but basically it looks at all the moves you can make then all the responses an opponent can make and then what you can do back etc. and attaches scores based on pieces taken and the positions. And then goes with the best score.

    Conventional chess programs use a similar algorithm and ones like AlphaZero are similar except they use machine learning and neural nets to judge how good positions are rather than a simple point score system.

    I remember trying to write a similar one after seeing the algorithm explained on the TV show Tomorrow's World, around 1980. (Here they are explaining the cutting edge of mobile phones in the day https://www.youtube.com/watch?v=vix6TMnj9vY&feature=youtu.be...)

    • Notably, Stanislaw Lem mentioned eye-tracking studies by Tikhomirov which suggest that human players also do some sort of tree-walking (or whatever the proper term is), but are unable to describe it consciously.

    • Just to add to your reply, alpha0 and Leela actually learn to play by playing with itself, which is much more than just having a score for a given position. Also, in the chess community, Stockfish is not seen as AI, but Leela and A0 are.

      7 replies →

  • Strictly speaking, this is an AI. AI does not imply AGI.

    Typically, chess engines are built on minimax, with optimizations like alpha beta pruning.

    • AI definitely doesn't imply Artificial General Intelligence, but I'm pretty sure no-one's saying that. The common confusion is between AI and sexy Machine Learning.