|
|
The Algorithm The heart of the algorithm is a 'scoring' routine, similar to the one used in the last version. Thankfully, now we're using QC, we can use long integers, which means we don't have to try and cram the score into a sixteen bit variable. To score a possible move, it checks each possible row which could intersect the playing position in the column.
Each row is scored as follows: current player 3: 100000 opponent 3: 10000 current player 2: 1000 opponent 2: 100 current player 1: 10 opponent 1: 2 empty: 1
This is the simplest type of scoring, but in order to play a four-in-a-row well, we need to do something a little more complex. There is another routine which will work out the best move for a player, and return the column and score of that move (using a typedef struct). Now this routine doesn't just call the scoring for each column, instead it gets the scoring, and calls itself again (recusively) to calculate the scoring for the opponent. It subtracts 1/5th of this score from the main score. By passing in a lookahead depth, we can limit how far ahead we want to look. This is along the lines of the classic min-max algorith, used in nearly all strategy game programs.
Because the RCX is not hugely fast, a lookahead of 4 moves is probably the most we want, as it takes about a minute to work out the best move at this level, but then it is scoring 16807 different possible moves, each with up to 16 possible four-in-a-rows. That's a lot of number crunching, especially for the RCX!
After all the coding for the chess robot - I realise that I could vastly improve this code using alpha-beta pruning and a nega-scout. In fact I could probably get it to look five or six moves ahead in about a minute per move, and be pretty much invincibe. Maybe I'll revisit it sometime later.
|
|