Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Computer chess

This thread is locked; no one can reply to it. rss feed Print
 1   2   3   4 
Computer chess
Evert
Member #794
November 2000
avatar

Motivated by a recentish chess-related question (http://www.allegro.cc/forums/thread/600698), I returned to chess programming. I found that it was probably better to rewrite almost the entire thing from scratch (move generator, search, evaluation) and after more than a month of tinkering with it, things seem to be coming along fairly well.

{"name":"599212","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8506c1a6d60f6dd39a8145e69853386.png","w":893,"h":665,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8506c1a6d60f6dd39a8145e69853386"}599212

All rules are implemented (obviously) although I disabled repetition checks and left out the 50-move rule (the first are actually important if for no other reason than to kick the program out of searching cycles) for the time being.
The program reaches a somewhat decent 7-8 ply search depth fairly easily, but the evaluation function mostly only looks at material, which is not really good enough. The program will also mess up its opening quite badly, but I haven't really invested any time into that department yet.
That said, if I give it a decent starting position I find that it can already give me a hard time, so I fully expect the program to be able to beat me consistently if I put some more effort into it.

The (Allegro 5 based) graphical interface is very elementary at the moment: you can make a move (not underpromotions though), tell the computer to play a move or load a position from disk. No time control, difficulty settings or other options. I'll be adding those eventually, and I'll make a text-based interface and a UCI/xboard interface (standard protocols for chess engines, so they can be used with other interfaces) too. Eventually.

If the Allegro interface is a bit more polished and the program doesn't make a complete mess of the opening anymore, I'll put it on the depot (along with some information on how the program works and what decisions I made in its design and why).

Ben Delacob
Member #6,141
August 2005
avatar

Sounds neat. I'd find it much more interesting if you don't use an opening move database but if your goal is making a strong competitor, that's sadly how it's usually done.

__________________________________
Allegro html mockup code thread -website-
"two to the fighting eighth power"

Evert
Member #794
November 2000
avatar

I'd find it much more interesting if you don't use an opening move database but if your goal is making a strong competitor, that's sadly how it's usually done.

Oh, I do plan to give it enough rudimentary opening knowledge that it will at least be able to place its pieces half-way decently on its own before adding an opening book.
Right now (at 5s per move), it calculates the first position to 8 ply and plays 1. f2-f3... (however, previous preferred 1. e2-e4 or 1. d2-d4, so it should be able to do better with a little tweaking).

anonymous
Member #8025
November 2006

f2-f3 is probably the worst first move one can make... :) Couldn't the position evaluation also take into account how many moves are available / how many squares are under fire (f2-f3 not only doesn't open up any important diagonals and weakens the king's position, it also blocks a useful move for your own knight).

It should probably just choose the first move at random (but not f2-f3, except in handicapped mode).

Evert
Member #794
November 2000
avatar

anonymous said:

f2-f3 is probably the worst first move one can make... :)

Hey, it has some merit: it attacks e4 and it moves your weakest pawn[1] to a saver square! :P

No, seriously, it's pretty atrocious, for pretty much the reasons you mentioned.
It's said that letting a computer play an opening on its own (no opening book) is the ultimate proof that computers can't "really" play chess. They're unbeatable in tactical positions though.

Quote:

It should probably just choose the first move at random (but not f2-f3, except in handicapped mode).

It should either have a decent-ish evaluation for opening positions (centre control, develop pieces, castle king are all good), or it needs to draw moves from an opening book. Otherwise you end up with silly things like 1. Nh3 or 1. h4. Many moves are bad, although e4 d4 Nf3 Nc3 c4 f5 g3 h3 b3 b4 a3 are all playable, which is slightly more than half (a3 obviously isn't that great, but it's surprisingly enough not completely horrible either because a6 is a semi-useful move in many black defences, which means you get to play a black defence as white, so you have a tempo more).

I'll work on that in due course; I'm trying to get it to perform decently in test positions first. It needs a better mate determination; right now it will sometimes find a deep mate and discard it until it reaches a deeper overall search depth, at which point it sometimes no longer sees the mate because the line that lead to it has been overwritten in the transposition table, so it can't look as deep. I'll fix that, then improve the evaluation function so it is more aware of piece placement, mobility and (very important) king safety (that will also help detect mate threats). I'll probably have a look at the openings after that.

That said, I just fixed a stupid mistake in my search implementation (it was trying to give accurate scores to all moves, which is stupid: you only need an accurate score for the best move, for the others you only need to know that they're worse) and now the search is about three times faster. Yay!

References

  1. i.e., the only one only protected by the king
Alianix
Member #10,518
December 2008
avatar

I would not mind to try your chess program, I used to play a lot of chess. At one point planned on writing a chess engine for practice programming. ( never materialized ...) Did you include promotion and en passant in the rules ?

Evert
Member #794
November 2000
avatar

Alianix said:

I would not mind to try your chess program, I used to play a lot of chess.

Ok. That reminds me: at the moment it probably only compiles on OS X... although that shouldn't be hard to fix.

Quote:

At one point planned on writing a chess engine for practice programming. ( never materialized ...)

It's a fun project to undertake, but it's not an easy task. Bugs can be very subtle and hard to find, and it's not so easy to make the program fast (read: strong).

Quote:

Did you include promotion and en passant in the rules ?

Of course. Though as I said, you cannot specify an under promotion at the moment (the program will play them, but if you play a promotion the move will be matched to a queen promotion). This is more of an interface limitation though, the engine will play them if applicable. I should really fix repetition detection though; the 50-move rule is less important.

Alianix
Member #10,518
December 2008
avatar

It would be really nice to be able to try it under Linux, but Windows will do too...

Yes chess programming is pretty complex when it gets to making it the engine strong enough to play masters. I wonder if computers in my lifetime will be ever so fast as to actually brute force mate from an early position...

Evert
Member #794
November 2000
avatar

Alianix said:

It would be really nice to be able to try it under Linux, but Windows will do too...

Linux is probably easier for me to get to work than Windows. :P

Quote:

Yes chess programming is pretty complex when it gets to making it the engine strong enough to play masters. I wonder if computers in my lifetime will be ever so fast as to actually brute force mate from an early position...

I doubt it, to be honest... the size of the search tree is just too prohibitive.

Anyway, things are progressing nicely. I took a break from running it against test positions and played an actual game yesterday. I'm not entirely unhappy with how it played the game in the end, but there are quite a few oddities that need to be worked out (I was white):

#SelectExpand
11. e4 e6 22. d4 d5 33. Nd2 f5 44. e5 c5 55. c3 cxd4 66. cxd4 Nc6 77. Nf3 Bd7 88. Nb3 Rc8 99. Bd2 a5 1010. a4 Nb4 1111. Bxb4 Bxb4 1212. Nfd2 Kf8? 1313. Bd3 Qb6 1414. Bc2 Rc4 1515. O-O Rxd4 1616. Nxd4 Qxd4 1717. Nb3 Qxb2 1818. f4 g5 1919. g3 Qc3 2020. Rc1 gxf4 2121. Bd3 Qxe5 2222. gxf4 Qg7 2323. Kh1 Bxa4 2424. Rc8 Be8 2525. Nd4 Qg6 2626. Bb5 Nf6 2727. Qe2 Ba3? 2828. Nxe6 Kf7 2929. Rc7 Bd7 3030. Bxd7 Be7 3131. Ng5 Kg8 3232. Rc8 Kg7 3333. Qxe7 Kh6 3434. Nf7 Kh5 3535. Rxh8 d4 3636. Rg1 Qxg1 3737. Kxg1 b6 3838. Qxf6 39 401 - 0

This is still without an opening book of any kind, so it's quite interesting that the first 10 or so moves are quite decent (3. ... f5 is a bit odd though). 12. ... Kf8 is of course a horrible move, but it should be very obvious from how the game progressed that the program has no notion of "king safety" (and that it desperately needs such a concept). Personally (without analysing the game in detail) I thought black was slightly better until around move 20 or so, at which point the scales tipped. By the time I got to play 24. Rc8+ I felt fairly certain I would be able to win.

EDIT: fixed a few typos I made in transcribing the game above, attached PGN file of the game for easy viewing.

Alianix
Member #10,518
December 2008
avatar

It does not play correctly here for me, it plays some of the white's moves on the black side, anyhow it looks like black gets to make two moves after Bb4...Kf8 ?

Using glchess.

Jonatan Hedborg
Member #4,886
July 2004
avatar

I wonder if you could program a pixel/vertex program to work as a move generator? Anyway, I really enjoy the subject of <game>-playing computer programs (I don't enjoy chess much though). I would love to try it out at some point (and I have a mac).

Evert
Member #794
November 2000
avatar

I found another typo: white's 7th move should be 7.Ngf3. I used the PGN viewer from http://members.shaw.ca/lapides/pgnedit/pgnedit.html to test it (the software I have installed on my machine plays the moves very slowly for some reason; it also didn't catch the error in the recording of white's 7th move).

I should really add exporting games to PGN before I do anything else... been on the list for a while and it will make these things much easier. :P

EDIT:
Double checked, with the corrected 7th move for white, the game plays correctly for me in the online viewer linked to above.

Alianix said:

it looks like black gets to make two moves after Bb4...Kf8 ?

I'm not sure what's causing that... but try after correcting white's 7th move.

I wonder if you could program a pixel/vertex program to work as a move generator?

Consensus seems to be that you can't use a graphics card to speed up chess programs very much, the reason being that graphics card tend to be good at parallel floating point operations and chess engines mostly needing bitwise integer manipulations. Even if you could, move generation is fairly quick and easy to do; it shouldn't be the thing that determines the speed of the chess engine.

I'll try to get the source in a shape where it will compile for other people on the weekend. It'll need a recent version of Allegro 4.9 (but not SVN at the moment, though I may change that before then).

EDIT2: Uploaded a slightly changed version of the previous game: I replaced the horrible move Kf8 by a more reasonable development move and let the program play the game normally again from there onwards. The final position has black up by two pawns but in an ending of opposite bishops that I hope is somewhat drawn. Certainly my program doesn't know how to play the ending if it isn't though.

Alianix
Member #10,518
December 2008
avatar

Ok, i looked at the game and the moves seem pretty decent, does your engine focus on mate and / or material at this point ?

Evert
Member #794
November 2000
avatar

Alianix said:

Ok, i looked at the game and the moves seem pretty decent, does your engine focus on mate and / or material at this point ?

It'll only evaluate material (well, not entirely - it has some other terms in there, but it's all very crude and rudimentary at the moment), but it will of course flag a checkmate as a win and it has some code to help find mates more quickly.
The occasional strategic blunder aside, it plays very decently. Hopefully I'll be able to turn this into a reasonably strong program eventually. :)
Right now, I'm testing it against a set of tactical test positions. It scored 247/300 last night, it's well on its way to score 251+/300 at the moment.

I'll focus on making the code ready for a release this weekend before trying to make it stronger though. :)

ReyBrujo
Moderator
January 2001
avatar

Are you hardcoding decisions or you got a rules database? While you should not focus on the opening, maybe you should start after the opening movements (setup the initial board with a finished opening) and see how it goes.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Evert
Member #794
November 2000
avatar

ReyBrujo said:

Are you hardcoding decisions or you got a rules database? While you should not focus on the opening, maybe you should start after the opening movements (setup the initial board with a finished opening) and see how it goes.

The program uses a set of heuristics to score a position: doubled pawns are bad, passed pawns are good, rooks are stronger on open files than on closed files, a knight is usually better placed near the centre than near the edge of the board, etc. These get translated into a number and summed (so the evaluation function is a linear function of the game features it knows about) along with the material balance. Picking the relative weights is what determines how good an evaluation function is; however, the evaluation function alone isn't enough: you need to add a search function as well to find the line of play that leads to the best position.

The sad thing about the game I posted above is actually that the program played the opening better than I did... :(

ImLeftFooted
Member #3,935
October 2003
avatar

Cool! What license are you releasing the code under?

I'm making a mechanical chess board that allows you to play people online. I'm looking into making chess-bots so that the online community can be started.

http://www.chessghost.com/sand/

Evert
Member #794
November 2000
avatar

Cool! What license are you releasing the code under?

Good question. I hadn't really thought about that yet.
For now, it's under something I just cooked up. When it gets to the stage where I want to release it "properly" I'll probably get a proper licence for it.

Quote:

I'm making a mechanical chess board that allows you to play people online.

Cool! When I was younger I always wanted a chess board that I could connect to my computer. I still don't like staring at a diagram on the computer screen; however, the expected lifetime of a chess set is much larger than that of a computer, so I now feel that a chess board that I can hook up to a computer is likely to be outdated quickly...

Quote:

I'm looking into making chess-bots so that the online community can be started.

Feel free to have a look at my source code, although there are probably other resources available to look at that are easier to get into. Two design elements of my code factor into this: I make heavy use of static inline functions in header files in an attempt to speed up the code (probably not as important as I thought when I first wrote it because the search algorithm is much more important; still my program was clearly faster than my previous attempt had been when I just got it working, it's a lot faster now) and I use "bitboards", 64 bit integers that represent one piece of information about the board (location of kings, white pieces, black pieces, etc.), which are very powerful but not that easy to wrap your head around if you're not used to them. A plain char array is better for that, and there are many open source programs that use those.

Anyway, all of that said: source attached for anyone who is interested!
No binary I'm afraid - I tried making an application bundle for OS X at least, but I couldn't figure out how to put Allegro's dylib's inside the bundle (and I couldn't static link for boring reasons having to do with being unable to compile all of Allegro in 64 bits and therefore being unable to install the static version of Allegro). I didn't have time to try the code at all in Linux, but it should probably work. I have no idea about Windows. MinGW will probably work after modifying the Makefile, MSVC will probably not work.

EDIT:
Added a new screenshot, highlighting something I'm quite happy with: my program finding Bxf7+ in less than one second (it used to take ~15 seconds not too long ago).

{"name":"599241","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/a\/1a2d4f3cff771ed39355692ae0399594.png","w":532,"h":514,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/a\/1a2d4f3cff771ed39355692ae0399594"}599241

GullRaDriel
Member #3,861
September 2003
avatar

Chess programming is really an interesting thing. I'll give it a go one day.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

ImLeftFooted
Member #3,935
October 2003
avatar

Evert said:

my program finding Bxf7+ in less than one second

??? I don't get it, how is that a good move?

Evert
Member #794
November 2000
avatar

I don't get it, how is that a good move?

See the main variation printed at the bottom of the "Iterations" window. ;)
After 1. ... Kxf7 2. Ne6 Black's queen has nowhere to go. The only alternative is 2. ... Kxe6, which leads to a forced mate after 3. Qd5+ Kf5 4. g4+ (which is why the program opts for 2. ... dxe6 3. Qxd8 instead).
Neither of the alternatives 1. ... Rxf7 and 1. ... Kh8 do anything to prevent 2. Ne6 either, of course.

Neil Walker
Member #210
April 2000
avatar

It's probably just me and my lack of memory, but I like chess boards to have the letters/numbers next to them so I don't have to work out where pieces are moving in relation to the move history :)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Evert
Member #794
November 2000
avatar

It's probably just me and my lack of memory, but I like chess boards to have the letters/numbers next to them so I don't have to work out where pieces are moving in relation to the move history :)

Hmm... good point, I didn't realise I hadn't put those in! Easy to add though.

ImLeftFooted
Member #3,935
October 2003
avatar

I'm going to try it when I get home tonight.

Evert
Member #794
November 2000
avatar

Amazing!
I was able to speed the program up by some 20-30% by just refactoring code in various parts of the move generator and board update code. Remember how barnches are expensive and chars are slower than ints? Well, they're faster than unconditionally calculating things you don't have to while relying on the compiler to pair instructions to eliminate any overhead (and we're not talking expensive calculations here, we're talking bitshifts and logical ors):

#SelectExpand
1static inline uint8_t get_piece(const board_t *board, const int where) 2{ 3 assert(where<=H8); 4 assert(board); 5 bitboard_t bb_where = ((bitboard_t)1)<<where; 6 int piece = ((board->bbc[1]>>where)&1)<<7; 7 if (board->bbp[0] & bb_where) 8 return piece | PAWN; 9 if (board->bbp[1] & bb_where) 10 return piece | KNIGHT; 11 if (board->bbp[2] & bb_where) 12 return piece | BISHOP; 13 if (board->bbp[3] & bb_where) 14 return piece | ROOK; 15 if (board->bbp[4] & bb_where) 16 return piece | QUEEN; 17 if (board->bbp[5] & bb_where) 18 return piece | KING; 19 20 return piece; 21}

is a whopping 20% faster than

static inline uint8_t get_piece(const board_t *board, const int where)
{
   assert(where<=H8);
   assert(board);
   int n;
   int piece = 0;
   for (n=0; n<6; n++)
      piece |= ((board->bbp[n]>>where)&1)<<n;
   piece |= ((board->bbc[1]>>where)&1)<<7;

   return piece;
}

Maybe I shouldn't be surprised, but 20% is a lot more than I was expecting to get (yes, that piece of code gets run all the time).

Anyway, I'm happy. Test suite score has increased to 275/300. :D

 1   2   3   4 


Go to: