Ó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's even written a 170 page book to serve as a reference to the 1326-byte "Nanochess" program, his strongest small chess engine.<p><a href="http://www.nanochess.org/chess.html" rel="nofollow">http://www.nanochess.org/chess.html</a>
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'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://github.com/vishvananda/ivory</a>
[2] <a href="https://chessprogramming.wikispaces.com/Bitboards" rel="nofollow">https://chessprogramming.wikispaces.com/Bitboards</a>
"class Position(namedtuple('Position', 'board score wc bc ep kp')):"<p>Whoa. I've literally never seen this idiom before.<p>But I immediately like it.
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://chessprogramming.wikispaces.com/</a>
Correct me if I'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. :)
I haven'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!
Unfortunately this program doesn't play legal chess. When I played the program it did not move its king out of attack when checked. And yes, I've played enough chess to know an illegal move when I see one.
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.
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 "taught" the rules of a board game --- a problem that falls squarely in the domain of supervised learning. In the studies I'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.
Here'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://www0.us.ioccc.org/2001/dgbeards.c</a>
I don'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't exist.
Tried it. Typed e4. Got this:<p><pre><code> Traceback (most recent call last):
File "sunfish.py", line 388, in <module>
main()
File "sunfish.py", line 365, in main
move = parse(crdn[0:2]), parse(crdn[2:4])
File "sunfish.py", line 345, in parse
fil, rank = ord(c[0]) - ord('a'), int(c[1]) - 1
IndexError: string index out of range</code></pre>
As a fellow chess coder I find this very cool, but we should be careful with the use of the word "strong." What ELO (for the non-chess people, rating) does this play at? I very much doubt it's truly what most would consider "strong" nowadays. In fact I doubt it plays above even 1500 - that's not strong. Great example of a simple chess engine in Python though? Surely!
I'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/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/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.