42

Beating myself at chess

For people interested in making their own, I highly recommend reading through https://github.com/thomasahle/sunfish/blob/master/sunfish.py , a surprisingly readable and surprisingly strong chess engine in 111 lines of Python

https://www.chessprogramming.org/ is also really interesting, see e.g. https://www.chessprogramming.org/Sunfish and https://www.chessprogramming.org/Quiescence_Search

3 days agoRecursing

> surprisingly readable and surprisingly strong chess engine in 111 lines of Python

Link I get shows 500 lines and it starts with 50 lines of piece-square tables. Maybe it's obvious when you are into the domain but otherwise... that's pretty much of opposite of what I would call "readable".

3 days agoutopiah

Yes it's "surprisingly readable" only for being so strong and so concise, definitely not production code.

The file is 500 lines because of the piece square tables (as you mentioned), comments, and the CLI interface logic

Previous HN thread here: https://news.ycombinator.com/item?id=20068651

3 days agoRecursing

The ai source code doesn't seem to have alpha-beta pruning yet. If you add that plus some basic move-ordering (killer move heuristic is easy, powerful, and has a cool name) that will let it search a lot deeper. One nice thing about such improvements is they converge to the same result, just faster, so unlike tweaking the evaluation function you can be pretty sure that you're improving it.

3 days agorecursivecaveat

Bug report: I got to a late-middlegame position and then the bot crashed because it tried to castle illegally (after its queenside rook had already moved):

``` Uncaught Error: Invalid move: {"from":"e8","to":"c8"} at Chess.move (chess.js:2530:23) at cm-chessboard/:75:23 ```

There didn't seem to be a way to recover besides refreshing.

3 days agoapetresc

I'm just here to comment on the CGA color palette on this website, lol.

I always hated that video mode, but it looks good here when the magenta and cyan are limited just to links and headers!

3 days agofogleman

Would maybe benefit from a "Checkmate!" or other finish screen, but cool project! Though I agree that it beating you may be more based on your skill level than its, given how it keeps pushing pawns.

3 days agodado3212

It is very humbling to write your own chess engine.

3 days agopetermcneeley

Yes. I did the exact same thing a few weeks ago (in Java, but the idea is to port it to my own programming language). I wrote a terminal UI. Lots of small challenges. Many things to learn, like minimax, bitboards, immutable vs mutable.

3 days agothomasmg
[deleted]
3 days ago

I was surprised how quickly I created a chess engine with Claude code a few months ago. It worked quite well. It was bad at recognizing certain chess patterns so I created a lot of tests for the chess engine.

3 days agomettamage

When I created my chess variant and had to create an engine, I used ChatGPT and was surprised that it worked, too. But it was fun learning its flaws.

3 days agoelicash

Note most modern Chess engines use UCI instead of Winboard protocol. But either is fine really.

3 days agoyyyk

It is terrible in the opening and a little better in the middle game (if you didn't win already, I mean if you played 4-5 random moves).

I think you can improve it a lot by taking into account its own position. I mean, moving forward isn't very important want you are under the menace of a checkmate somewhere else.

3 days ago_ache_

I tried playing against it, I didn't have many expectations, but even though I blundered a bishop on move 3 due to a mouse-slip, I could still checkmate it in 6 moves. To me it seemed like it makes random moves.

3 days agoXCSme

The algorithm behind it is very basic.

Chose the most aggressive move (in term of pieces value and check-mate), if none is aggressive, it takes on of the move equally non aggressive.

Didn't remember the depth of the algorithm but it was very simple C code, could check quickly. It should be able to find a mate in 2 or 3 if it was in position of having one.

I didn't check the correctness of the algorithm, just the intention.

3 days ago_ache_

I thought it played worse than random moves and couldn't understand how it could beat anyone (no offence to OP).

But if you intentionally hang your pieces, it tends to take them. And it will try to promote pawns in the endgame. So it is possible for it to stumble upon a checkmate, though in my effort where I gave away all my pieces, it instead found the only move to stalemate once it had K+Q+R vs K.

3 days agodmurray

It tries to promote pawns in the opening and mid-game too.

3 days agodullcrisp
[deleted]
3 days ago

It’s terrible everywhere tbh, it has zero awareness of how dangerous the f7 square is in the opening, you can pretty much just mate it by landing the queen on f7 with a piece covering it every time.

I applaud the effort but not sure I see the point, I had a more capable chess playing program than this on a zx speccy in the 80’s.

3 days agonoir_lord

It's a fun little AI, but I have another bug report. This time my move is the one that made it crash!

    Uncaught Failed to apply player move! cm-chessboard:69:55
    promotion result 
    Object { type: "pieceSelected", square: "c8", piece: "wq" }
    cm-chessboard:111:37
    Uncaught Error: Invalid move: {"from":"d7","to":"c8","promotion":"q"}
I had forked a rook and king with a pawn. Pawn takes rook, and was going to be offered a promotion, but taking and promoting at once seems to make it crash.