Live data from Hacker News

A step-by-step guide to building a simple chess AI

medium.com

41–44 of 44 posts

Re: A step-by-step guide to building a simple chess AI

#41
post #32

Earlier quoted context omitted.

Good parent question and interesting links, thanks, will take a look. To follow on though, shoukdn't it technically be the case that piece values should in theory fluctuate based on current situation. For instance, my castle is worth zero to me if I'm throwing it so I can get you in check. Will I know modern chess engines take this stuff into account. Mostly just a random thought.

> To follow on though, shoukdn't it technically be the case that piece values should in theory fluctuate based on current situation. Yes. One common example is knights and bishops. Both are worth about 3 pawns, but knights are generally more useful in closed positions due to their ability to jump over pieces, while bishops are better in open positions since they can more attack and move long distances.

Fascinating.

So in some sense, and I'm just brain-farting here, the value of a piece is a weighted sum of all the possible squares it could occupy during move generation. Something like a rudimentary path trace.

Now trying my best not to get distracted building chess machines. :/

Re: A step-by-step guide to building a simple chess AI

#42
post #37

Earlier quoted context omitted.

I'm not quite that bad at chess, but bad enough that I wrote a chess game that plays random moves so I could always beat it: http://www.notesfromandy.com/2016/09/27/chessfidget/ That got boring, so I added very weak AI by connecting to the chess engine that comes preinstalled on macOS (inside Chess.app).

> connecting to the chess engine that comes preinstalled on macOS Can you elaborate on this please? The presentation at your link doesn't say.

One of the preinstalled apps on macOS is /Applications/Chess.app. Turns out it uses an open-source chess engine called sjeng, the binary for which is inside the app bundle. Chess.app is itself open source, so I tweaked Apple's code for my own purposes. The code launches an sjeng process, opens a pipe to it, and sends commands over the pipe.

There's a couple of sentences about this in the _README file here (as well as my tweaked source files):

https://github.com/aglee/ChessFidget/tree/master/ChessFidget...

Fun fact: you can play chess in a terminal window by running /Applications/Chess.app/Contents/Resources/sjeng.ChessEngine. You can enter the `help` command for instructions. Note this will create four files with .lrn extensions in your home directory. I forget how to control this behavior -- you can Google "sjeng" for details.

Re: A step-by-step guide to building a simple chess AI

#43
post #2

Interesting that it's deterministic. I can get it to play the same game again and again. Why is this not the case with more advanced chess engines? Edit: Apparently single-threaded stockfish is deterministic. Maybe my experience with non-deterministic chess engines just has to do with the handicapping related to providing easier levels of play.

Never checked but somehow got me surprised: aren't all chess engines deterministic? The same program, given the same input, should give the same output unless it is specifically programed to try to avoid it (and then we have PRNGs).

Many chess engines are (also) made for humans to play against.

Humans wouldn't want their opponent to play the exact same way every time. For a simple example, playing white, one wouldn't want the computer to always open 1. h4.

So, either the computer has to pick an opening, or the human opponent has to pick one.

Of course, that extends past the first move, so, assuming the computer to play deterministically, one would end up with the human opponent having to specify "let's play opening X, variant Y, as typically played by Z up to black's 15th move". It probably is easier to use a "set up a custom board" feature than to specify that.

And it wouldn't even stop there. The human specifying that particular opening likely wants to study that opening, not just the single continuation that the computer thinks to be the best one.

Also, as another poster said, a chess engine may use Monte Carlo simulation to pick a move, and that means using a random number generator.

Re: A step-by-step guide to building a simple chess AI

#44
post #37

Earlier quoted context omitted.

> connecting to the chess engine that comes preinstalled on macOS Can you elaborate on this please? The presentation at your link doesn't say.

One of the preinstalled apps on macOS is /Applications/Chess.app. Turns out it uses an open-source chess engine called sjeng, the binary for which is inside the app bundle. Chess.app is itself open source, so I tweaked Apple's code for my own purposes. The code launches an sjeng process, opens a pipe to it, and sends commands over the pipe. There's a couple of sentences about this in the _README file here (as well as…

Thanks for the explanation!
Post reply on HN