|
|
| Computer chess |
|
Evert
Member #794
November 2000
|
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"} 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 (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
|
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. __________________________________ |
|
Evert
Member #794
November 2000
|
Ben Delacob said: 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. |
|
anonymous
Member #8025
November 2006
|
f2-f3 is probably the worst first move one can make... It should probably just choose the first move at random (but not f2-f3, except in handicapped mode). |
|
Evert
Member #794
November 2000
|
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! No, seriously, it's pretty atrocious, for pretty much the reasons you mentioned. 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
|
|
Alianix
Member #10,518
December 2008
|
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
|
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
|
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
|
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. 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): 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
|
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
|
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
|
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. EDIT: 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. Jonatan Hedborg said: 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
|
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
|
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. I'll focus on making the code ready for a release this weekend before trying to make it stronger though. |
|
ReyBrujo
Moderator
January 2001
|
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. -- |
|
Evert
Member #794
November 2000
|
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
|
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. |
|
Evert
Member #794
November 2000
|
Dustin Dettmer said: Cool! What license are you releasing the code under?
Good question. I hadn't really thought about that yet. 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! EDIT: {"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"} |
|
GullRaDriel
Member #3,861
September 2003
|
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" |
|
ImLeftFooted
Member #3,935
October 2003
|
Evert said: my program finding Bxf7+ in less than one second
|
|
Evert
Member #794
November 2000
|
Dustin Dettmer said: I don't get it, how is that a good move?
See the main variation printed at the bottom of the "Iterations" window. |
|
Neil Walker
Member #210
April 2000
|
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. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
|
Evert
Member #794
November 2000
|
Neil Walker said: 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
|
I'm going to try it when I get home tonight. |
|
Evert
Member #794
November 2000
|
Amazing! 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 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. |
|
|
|