Chess Engine – How a Chess Engine Decides Its Moves

chesscommon-lispdesign-patternslisp

I'm writing a simple chess engine in LISP. I actually know how the engine decide the move, it evaluates and reads some opening books. But that's not what i mean. This is my design.

57 58 59 60 61 62 63 64
49 50 51 52 53 54 55 56
41 42 43 44 45 46 47 48
33 34 35 36 37 38 39 40
25 26 27 28 29 30 31 32
17 18 19 20 21 22 23 24
09 10 11 12 13 14 15 16
01 02 03 04 05 06 07 08

I looked at more complicated solutions but i came out with what i believe is the simplest one. Say the bishop is on the square 23, it could move 4 possible moves, (to 16 or 14 or 32 or 30), so it moves -7 or +7 or +9 or -9.

I create an array

(make-array '(8 8) :initial-contents
'((R B N Q K B N R)
 (P P P P P P P P)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
(NIL NIL NIL NIL NIL NIL NIL NIL)
          (P P P P P P P P))
          (R B N Q K B N R)))

And move the pieces from index to index. My question is, i cannot simply tell the bishop to move +7 or whatever, if it's an open diagonal. I need to tell it to move +9 * 1 or +9 * 2 etc. and you bishop have to decide which one to go to. I cannot write a condition and a loop for every possible square

Best Answer

Imagine you've got a function that evaluates the game board and returns a score, where large positive scores mean you're doing well and large negative scores means you're doing badly.

Now imagine that for each possible position that a piece can move to, for each piece on the board, you calculate what the score would become and choose the move that results in the best score. This isn't too hard to do and results in an AI opponent that's roughly equivalent to a novice human chess player.

Now; instead of finding the best score after you've made one move, you could find the score after you've made a move and the opponent has also made a move.

More importantly you can do this to "n levels" - e.g. find the score after you've made n moves and the opponent has made n moves. In this case "n = 3" is about equivalent to a decent human player, and extremely good human players may be equivalent to about "n = 7".

Computers are fast enough to do up to "n = 7" relatively quickly; so that's all relatively easy to implement.

If you want to completely and utterly destroy the game (by making it impossible for a human player to win or enjoy playing); you can go further than this. In this case you have to worry about the "pruning" stuff other people have mentioned to reduce processing time and memory consumption. My advice here is don't bother - there's already been far too many smart/stupid people ruining "player vs. machine" chess.

Related Topic