How to make a chess engine

Sort:
Snowcat14

Hi.  I have recently taken an intrest in chess engines and would like to make one.  The problem is, I don't know how.  is there anyone that can help me?  Thanks.

Lou-for-you

Houdini is a UCI chess engine developed by Belgian programmer Robert Houdart. It is influenced by open source engines IPPOLIT/RobboLito, Stockfish, and Crafty. Earlier versions are free for non-commercial use (up to version 1.5a), but later versions (2.0 and onwards) are commercial. So you build on the open source ones to start..

Lou-for-you

Houdart has houdini playing matches all day long against other engines and on the rare occasion of a loss he analyses the game as to find the cause and improve the engine..

Snowcat14

I want to make a chess engine to play against for fun.  It doesn't have to be good.

Lou-for-you

Wow, all romanians can do that? Fascinating...

waffllemaster

Well first you have to learn a programming language and probably have at least a few years experience in programming.  Then you'd be good enough to make some crude ones by yourself.

EscherehcsE

One of these days I might gather up enough courage to try creating an engine myself. I've been told it will end up taking far more of your time than you could ever imagine.

The way I understand it, you'll need to decide on the programming language to use. (I'll probably try regular C.)

Start reading forums where the chess programmers hang out, like Talkchess:  http://talkchess.com/forum/index.php

You'll want to start out simple. Use a simple program to study to learn the basic concepts. (Forget even looking at something as complicated as Houdini.) For the first attempt, you'll probably be doing good to just create a working engine, never mind if it's even any good.

A couple of links to start:

http://chessprogramming.wikispaces.com/

http://adamsccpages.blogspot.com/p/chess-programming-resources.html

Snowcat14

Thank you all for your helpful comments.

DiogenesDue
jadarite wrote:

So, another thing you might consider is teaming up with other computer programmers and work as a team. One could program in as white, the other black.  Then switch. 

This is a completely nonsensical statement...

Bigthirtynanster

shut up diogenesdue

basketstorm

That's an interesting topic.

For learning purposes I recommend python and a library called "chess", docs are here: https://python-chess.readthedocs.io/en/latest/

Install python, then in command-line type: pip install chess

This library has notion of a Game, Board, pieces. It can return a list of legal moves on each stage of the game, it can tell whether it is a checkmate or stalemate or when draw can be claimed. All you have to do is to implement logic of choosing a move out of a list of legal moves while having information about the board/game state.

Here is the very simple engine with text input-output program. It just throws random (but legal) moves at you:

https://pastebin.com/raw/vj1hdkfw

Replace get_random_move implementation with actual evaluation of board state after each move and make that evaluation recursive. Basic evaluation just sums piece values (Q=9, R=5 etc or Q=-9,R=-5 etc for black pieces), example - https://pastebin.com/raw/NY2SsVze). You do board.push(move) to apply move temporarily, then evaluate and then board.pop() to undo the move. If engine plays black smallest evaluation will indicate a best move. Do not forget to shuffle the move list before scoring, otherwise engine will be picking the first one (typically Rb7-Rb8 back and forth or other stupid neutral move). Advanced evaluation considers location of each piece, stage of the game and other factors. Then add the opening book. Use dictionary, concatenate move list with a dash and check the dictionary for that key. Example:

...

# Spanish
'e4-e5-Nf3': [
'Nc6', # 2... Nc6
'd6', # 2... d6 (Philidor Defense)
'Nf6', # 2... Nf6 (Petrov Defense)
],
'e4-e5-Nf3-Nc6': [
'Bb5', # 3. Bb5 (Ruy Lopez)
'Bc4', # 3. Bc4 (Italian Game)
'd4', # 3. d4 (Scotch Game)
],
'e4-e5-Nf3-Nc6-Bb5': [
'a6', # 3... a6 (Morphy Defense)
'Nf6', # 3... Nf6 (Berlin Defense)
'd6', # 3... d6 (Steinitz Defense)
],

...

You can also add some better visualisation. This chess library can generate SVG files with board and pieces (chess.svg.board(board) -- and save as svg file). Reference that svg in a local html file and make the page refresh regularly using javascript. Input will still be in console but you will see graphics updating in a browser window automatically, this is how this python library draws the chessboard:

Alternatively, implement UCI interface. With this chess library adding UCI support is simple and no need to do it fully, just "uci", "ucinewgame", "isready", "position startpos/fen/moves" and "go" commands will be enough, add exe wrapper and use your engine with any chess UI.

DiogenesDue
Bigthirtynanster wrote:

shut up diogenesdue

The thread was was not posted on for 11 years before you bumped it. Are you normally upset by things that happened when you were (just guessing here based on you avatar and post...) 3?

G3N0SS3
Snowcat14 wrote:

I want to make a chess engine to play against for fun. It doesn't have to be good.

Just do A* dont make the ply too deep. 
Or try minmax
Very old and basic "Chess Engines"

They will not be very good though you would need to optimize and adapt