Developing a Chess Engine

Sort:
Coder_On_Ster01ds

DISCLAIMER (to mods): // First of all sorry for posting here. I couldn't find anywhere suitable. You seriously need a "Computer Chess" section. If you create such a forum, please do move this over there. //

I have been a chess player for years (over 17 years of experience). All those years ago, I was playing against a computer opponent, on MS/DOS. I think it might have been called Battle Chess or something, a "visual" game with topnotch graphics at the time.

I was like 8 or 9 years old, but I was fascinated both by computers and chess. More than 15 years later, I got enrolled in the university as a Software Engineering undergrad. Now I am constantly writing codes on DOS-like environments (UNIX they call it) and still playing chess.

On this site I am not playing often, and I just let like 12-15 games time out. As such my rating is mostly incorrect. I am certainly not a patzer!

Recently, I have been interested in modern chess engines which can defeat even the strongest human players. Certainly awe-inspiring! However, after playing some games against them, I see that they only play boring lines, probably tested before, or just play defensive and wait for the opponent to err out. Yes, I cannot beat them at all, but that is not the point. It is not fun when I make a tactical mistake, then the engine looks 3453754943 moves ahead and punishes me with a lost pawn (best case) or sometimes with a whole knight or something! It feels like these engines are all too cruel. Nothing creative, but perfect games!

Seeing this, I decided I want a put an end to it for myself, and put my programming and general computers knowledge (shameless self-promotion: I am usually the top of the class with 1/20th as much time studied as the others) to use and create a fun to play engine!

This engine will play more bravely, sometimes even making (imperfect) strategical sacrifices, always on the attacking side, combining two of my favorite players Capablanca (for the natural ability) and Tal (attacking, positional chess).

Actually we had started a similar idea with a friend (he is in Computer Science, and a smart guy) but from the scratch. As in, we tried to write an engine that would learn, but used a built-in window to play with humans. This was not portable and not very feasible, and as such, with other matters bothering us, we had to stop it.

Now I have a better idea: Use the UCI protocol to write the engine in, and use the ideas of the oldtimers. This way, I will progress more in a more limited time. And people will be able to use the engine anywhere should they want to use my engine at all.

I have checked some pages, and found some information. I am planning to read most of them. But I think there MUST be other players here who tried to write an engine, and I wanted to ask them what they can share with me. A PDF, an image, a website, some sourcecode, ideas... Anything. Just post here and I certainly won't judge you!

Here are some things I already have in mind:

- Should be fun. That's the whole point!

- I want to write the code in Ruby. If that doesn't work, then Python. No Javascript or something. Also, no C++.

- The code will be usable as a UCI engine.

- Should not take a 20-men team to code. I am alone!

- I will mostly be coding on Linux, but will try the code on Windows and several GUI's. But mostly, please suggest multi-platform ideas or tools.

- Should not fry the user's CPU! Will need smart algorithms for that.

- I am planning to leave it open-source.

That's all I can think of for now. Sorry for the long post! Please leave your ideas here. Thank you!

MrEdCollins

If you're serious about writing your own engine, you're at the wrong website.  You will find ten times as much information, or more, over at Talk Chess (http://talkchess.com/forum/index.php) specifically in their Programming and Technical Discussions forum.

Coder_On_Ster01ds

Yeah, I had noticed them in my searches. I know this is a chess "playing" forum, not chess "programming" or anything, but still... I felt someone might want to let me know. Actually I am thinking of posting over at TalkChess too.

attwo
Coder_On_Ster01ds wrote:

Now I am constantly writing codes on DOS-like environments (UNIX they call it).

wtf

Also, coding a chess engine in Ruby or Python is a bad idea. Stick to C or C++. May the plague get you if you ever think about Java for such a thing.

My suggestion would be to check out the general theory behind programs that play games, with particular emphasis on minimax and alpha-beta pruning.

Check out Crafty and Stockfish, they are both FLOSS.

Nimzo-Sicilian

As a developer, C and C++ are going to be the best languages I would pick to write the code.

"Javascript"? I think you ment Java. They are obviously two different languages and Java would be the one to use for computer-based applications.

Coder_On_Ster01ds
attwo wrote:
Coder_On_Ster01ds wrote:

Now I am constantly writing codes on DOS-like environments (UNIX they call it).

wtf

Also, coding a chess engine in Ruby or Python is a bad idea. Stick to C or C++. May the plague get you if you ever think about Java for such a thing.

My suggestion would be to check out the general theory behind programs that play games, with particular emphasis on minimax and alpha-beta pruning.

Check out Crafty and Stockfish, they are both FLOSS.

I didn't want to go too much into detail when I said that "UNIX is a DOS-like environment". Maybe I should elaborate. UNIX was one of the first operating systems (that's the base software, you can think of Windows) developed in early '70s. Then Microsoft tried enhancing it, mixing ideas from Apple, IBM and UNIX and developed Microsoft DOS. It later evolved into Windows, but if you are young and have never seen a DOS system, you can try to run "cmd" in your Windows. On modern UNIX-like systems, you can still use this in an application called "terminal". It is very helpful to the IT guys and is still widely used. Thanks for listening :)

 

About the language of choice... I believe developing in C would be the ideal choice if one wanted the speed. But I am not too much worried about speed, I am not trying to develop a speed monster system with 40-moves ply. It should still see far enough, but should not bother "perfecting" the lines. Ruby is one of the most fun languages to code in, so I am looking if I can use it.

 

I will also look into opensource engines, mainly Stockfish. But the final product will have no resemblance to the way Stockfish plays (it is THE engine and is far too strong to realize by a one-man team) but I will learn the ideas only.

Coder_On_Ster01ds
DoubtingDave wrote:

As a developer, C and C++ are going to be the best languages I would pick to write the code.

"Javascript"? I think you ment Java. They are obviously two different languages and Java would be the one to use for computer-based applications.

I have already replied about C. Yes, it will be faster. But I want to code in Ruby just to show that speed is not everything.

 

And no, I MEAN Javascript. Because I saw someone on the web who said he was a chess expert and wrote a Javascript engine in a few days. I don't want to do that. I believe Javascript is far from a language that should be used outside client-side web programming. Some may think otherwise, and there are programs that such people wrote in Javascript. Pain to run and use. Best to avoid.

Wilbert_78

Ruby, Python, C# will all perform good. C and C++ is nice, but for a chess engine my personal favour would be python, so I can concentrate on the engine and not the memoryleaks.

I have been thinking about this idea myself. In another fashion though. I am creating a z80 based computer and want to make a digital chessboard with it eventually. In my case, the z80 won't be running the engine, but will use the dgt, uci protocols to communicate with Fritz. My chess-engine of choice. However I did gave it quite some thought. 

I would suggest start with the basics. Chessboard, rules of pieces to move, en passant, castle, etc. When you got that going, take a look at mirco max. It's not the smallest program I have ever seen, but with 133 lines of code it's pretty darn close. And those 133 lines of code have 34 webpages devoted to them to explain them! The source is in C though.

http://chessprogramming.wikispaces.com/Open+Source+Engines
http://home.hccnet.nl/h.g.muller/max-src2.html

And don't let the small source confuse you... with elo 2000 this engine will beat the majority of human players.

Coder_On_Ster01ds

Wow Micro Max looks really interesting. Only 133 lines? Brilliant. I will bookmark it :)

rtr1129
Wilbert_78 wrote:

Ruby, Python, C# will all perform good. C and C++ is nice, but for a chess engine my personal favour would be python, so I can concentrate on the engine and not the memoryleaks.

I love Python, but it's a terrible language for chess engine programming. It's just too slow. I have written engines in C, C++, and Python. I really wanted Python to work out, but it was just too slow no matter what I did. I ended up having to write Python code that looked like C code, which is not the point, so I stuck with C++.

I think Go or D are interesting choices if you want to avoid C/C++.

Wilio9
DoubtingDave wrote:

As a developer, C and C++ are going to be the best languages I would pick to write the code.

"Javascript"? I think you ment Java. They are obviously two different languages and Java would be the one to use for computer-based applications.

Honestly, it's possible to do it in javascript with the help of asm.js.

Wilio9

There's no reason why it wouldn't work in Ruby since the language is Turing complete. Also PyChess is a chess client (with its own engine) that was coded on entirely in Python. And of course, it is licensed under the Gnu public license, so you can have access to the code.

ex0du5

I think it should be pointed out that the premises of this thread are false:

  • Many engines written to support UCI support the options "Aggressiveness" and "Contempt Factor", among others.
  • Engines don't choose lines by simply "playing defensively and waiting for errors".  They score every position based on material, positional patterns, and other heuristic scoring.
  • The score for a move is found by walking the tree of all positions accessible by that move to some depth and calculating a score by evaluating all paths, accumulating the scores through some algorithm.  This accumulation algorithm may use the UCI options to find paths that may not score best with best play (in their scoring scheme) but offer tactical opportunites or other score clusterings.
  • Alternatively (and maybe more common), the options alter the scoring so that tactical heuristics may contribute to overall positional score.

My point is, if you find you do not like the play of the engines you have, you may be able to tweak them with simple things like:

setoption name Contempt Factor value 40

setoption name Aggressiveness value 160

And if you still want to write your own, maybe start by downloading Stockfish code and open up evaluate.cpp.  Start by messing around with the evaluate_pieces function.  That can let you test out various heuristics you are thinking about.

gerberito

Sunfish: a Python Chess Engine in 111 lines of code

https://github.com/thomasahle/sunfish

Raja_Kentut

Back when I was in college, I tried to write a chess engine. It was decent, but I abandoned the project as the workload in college increased. What I know may be obsolete, but it may help towards the direction you need to go.

Chess engines are divided into two types. One is knowledge-based, and the other is brute-forced type. Botvinnik tried to build knowledge-based chess engine - an engine that thinks and evaluates a position similar to the way humans do. However, as computation power increases, as of this moment, strongest chess engines are of brute force type - the one that relies on heavy computing power.

Assuming that you want to go for a conventional route (which is building the brute-force type chess engine), you need to give the following points some thoughts:

  1. Interface. The common ones are UCI and Winboard.
  2. Position / board representation. Back then people used bitboard. The advantage of bitboard is that it is small in size and it allows bit-wise operaton to be performed so as to make computation more efficient. However, with today's computing power, I have seen chess engines that do not use bitboard and yet they are still fast and powerful. Bear in mind that if you use a high-level programming language, you may not be able to use bit-wise manipulation.
  3. Search algorithm. You to do some homework for this. Minimax, etc. This is the key to your engine's computational strength.
  4. Position evaluation criteria. Not only you need to determine the search algorithm, you also need a way to tell the computer whether a position is good or bad, and interpret this in terms of number. Back then, I had to take a look at a number of open source chess engines. Crafty's was the neatest and most readable in my opinion. Stockfish's was not bad. Look around and see how different engines converts knowledge into numbers.
  5. Technology. If you want to build a strong brute-force chess engine, stay away from interpreted language. You need to squeeze as much computing power as you can. I went for C, but I have seen Java-based chess engines performing descent.

Best of luck in writing your chess engine. Have fun!

PlayerOfDoom

Bro u'll never be able to do it in python believe me...