TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Sunfish: A simple but strong chess engine in 111 lines of Python

291 pointsby ff_almost 11 years ago

18 comments

chengsunalmost 11 years ago
Óscar Toledo G. has notably written various tiny, strong and highly obfuscated chess engines in C (winning the IOCCC twice) and JavaScript (2nd place in the first JS1k). He&#x27;s even written a 170 page book to serve as a reference to the 1326-byte &quot;Nanochess&quot; program, his strongest small chess engine.<p><a href="http://www.nanochess.org/chess.html" rel="nofollow">http:&#x2F;&#x2F;www.nanochess.org&#x2F;chess.html</a>
评论 #8133917 未加载
评论 #8134312 未加载
vishvanandaalmost 11 years ago
Pretty neat stuff. Looks like the move generator is missing a few features like underpromotion but it is very concise. I wrote a rudimentary engine + move generator[1] using bitboards[2] a couple of years ago. Unfortunately python poorly suited for bitmath optimizations because it doesn&#x27;t support fixed width integers. Once I saw how slow my movegen went compared to a c version, I gave up on finishing up the minimax search to complete the engine. It does run quite a bit faster in pypy but still no easy way to force 64 bit integers.<p>[1] <a href="https://github.com/vishvananda/ivory" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vishvananda&#x2F;ivory</a> [2] <a href="https://chessprogramming.wikispaces.com/Bitboards" rel="nofollow">https:&#x2F;&#x2F;chessprogramming.wikispaces.com&#x2F;Bitboards</a>
评论 #8137780 未加载
评论 #8133762 未加载
edavisalmost 11 years ago
&quot;class Position(namedtuple(&#x27;Position&#x27;, &#x27;board score wc bc ep kp&#x27;)):&quot;<p>Whoa. I&#x27;ve literally never seen this idiom before.<p>But I immediately like it.
评论 #8133422 未加载
评论 #8133709 未加载
评论 #8133903 未加载
WizzleKakealmost 11 years ago
If you are interested in this sort of thing, the chess programming wiki is an excellent resource:<p><a href="http://chessprogramming.wikispaces.com/" rel="nofollow">http:&#x2F;&#x2F;chessprogramming.wikispaces.com&#x2F;</a>
Jemaclusalmost 11 years ago
Correct me if I&#x27;m wrong, but that looks like 388 lines of Python?<p>Still, very cool. I spent awhile trying to write my own and failed miserably. :)
评论 #8133437 未加载
评论 #8133438 未加载
评论 #8133416 未加载
评论 #8134203 未加载
S4Malmost 11 years ago
I haven&#x27;t played it yet, but I am curious, the variable pst seems to give the score of a piece given the square it is, <i>independently</i> of the position? How it that possible, am I missing something? (You only use pst to calculate the score of a move, if there is no check, checkmates or capture).<p>Having said that, the code is very neat and readable. Thank you for showing it here!
评论 #8134505 未加载
评论 #8148933 未加载
评论 #8135280 未加载
slm_HNalmost 11 years ago
Unfortunately this program doesn&#x27;t play legal chess. When I played the program it did not move its king out of attack when checked. And yes, I&#x27;ve played enough chess to know an illegal move when I see one.
评论 #8133592 未加载
评论 #8133688 未加载
评论 #8136938 未加载
评论 #8148750 未加载
评论 #8135416 未加载
Isofarroalmost 11 years ago
Yuck, just caught it out with:<p><pre><code> e2e4 g8f6 e4e5 f6d5 d2d4 b8c6 f2f4 e7e6 c2c4 d8h4? g2g3 f8b4 c1d2 -- 2 pieces en-prise at this point. h4h6 c4d5 e6d5 d2b4 c6b4 a2a3 b4c6 b1c3 c6e7 </code></pre> Just a piece down with very little compensation.
评论 #8148944 未加载
jostmeyalmost 11 years ago
Pretty cool. I once wrote a C++ program to play me in chess. It was ~5000 lines of code! I can appreciation this script.<p>I think a more interesting problem now is to create computer algorithms that can be &quot;taught&quot; the rules of a board game --- a problem that falls squarely in the domain of supervised learning. In the studies I&#x27;ve found, the algorithms were provided with a lot of prior knowledge about the specific board game, so there may be a lot of room for progress.
评论 #8133899 未加载
评论 #8134249 未加载
Bootvisalmost 11 years ago
My ego is saved by its weak endgame but it plays a pretty decent opening all on its own.
评论 #8133935 未加载
评论 #8148718 未加载
mightybytealmost 11 years ago
Here&#x27;s a simple but strong suicide chess engine in literally 162 lines of C.<p><a href="http://www0.us.ioccc.org/2001/dgbeards.c" rel="nofollow">http:&#x2F;&#x2F;www0.us.ioccc.org&#x2F;2001&#x2F;dgbeards.c</a>
erikbalmost 11 years ago
I don&#x27;t care that much for chess engines, but the code looks really pythonic. I learned a lot from reading it! I already started to believe that real pythonic projects don&#x27;t exist.
评论 #8134687 未加载
grondilualmost 11 years ago
Tried it. Typed e4. Got this:<p><pre><code> Traceback (most recent call last): File &quot;sunfish.py&quot;, line 388, in &lt;module&gt; main() File &quot;sunfish.py&quot;, line 365, in main move = parse(crdn[0:2]), parse(crdn[2:4]) File &quot;sunfish.py&quot;, line 345, in parse fil, rank = ord(c[0]) - ord(&#x27;a&#x27;), int(c[1]) - 1 IndexError: string index out of range</code></pre>
评论 #8133314 未加载
评论 #8133387 未加载
评论 #8133321 未加载
WoodenChairalmost 11 years ago
As a fellow chess coder I find this very cool, but we should be careful with the use of the word &quot;strong.&quot; What ELO (for the non-chess people, rating) does this play at? I very much doubt it&#x27;s truly what most would consider &quot;strong&quot; nowadays. In fact I doubt it plays above even 1500 - that&#x27;s not strong. Great example of a simple chess engine in Python though? Surely!
评论 #8148976 未加载
chatmanalmost 11 years ago
Mentioning &quot;strong&quot; without benchmarks seems subjective.
pbreitalmost 11 years ago
Python is the nicest language to read, right?
gaiusalmost 11 years ago
I&#x27;ve a VIC-20 here running chess. 3.5k RAM. And people even got it to run on the ZX-81, with 1k. Now <i>that&#x27;s</i> impressive.
评论 #8133753 未加载
评论 #8133929 未加载
Alupisalmost 11 years ago
I&#x27;ve noticed the odd trend of people bragging about how few lines it took to do X. Seems like people are trying to equate less loc == better code, which is not usually the case (better&#x2F;more robust error handing and correcting, more complicated AI logic, etc).<p>It just means you are using a more abstracted version of whatever.<p>In reality, a 111 lines of Python program is likely thousands of lines if you were to count all of the standard Python libraries and&#x2F;or any 3rd party libraries used.<p>What if I took this program, wrapped it in say, 5 lines of Python, then said I implemented chess in 5 lines of Python? Did I really? Of course not... but it makes for a good attention grabber.
评论 #8134345 未加载
评论 #8135601 未加载
评论 #8149041 未加载
评论 #8134793 未加载
评论 #8135930 未加载
评论 #8134359 未加载