Andy's LEGO® Mindstorms® Page
About this site
  
What's New?
  
Standard Disclaimer
Quite C
  
Francesco Ferrara
  
Getting Started
  
Installing Quite C
  
Compiling a project
  
Sample Code
Chess
  
The Robot Chess Project
  
Programming Chess
  
First Attempt
  
Version 0
  
Version 1
  
The Program
  
The State of Play
  
The User Interface
  
The Move Generator
  
The Move Applier
  
The Rating Algorithm
  
The Search Algorithm
  
Putting it all together
  
The Robot
  
The Head
  
Moving the head
  
Movies
  
Downloads: Source Executables, MLCad Models
4-in-a-Robot
  
4-in-a-Robot - the Robot
  
The 4-in-a-Robot Base
  
The 4-in-a-Robot Delivery Mechanism
  
The 4-in-a-Robot Controller
  
4-in-a-Robot - the Code
  
The 4-in-a-Robot Algorithm
  
The 4-in-a-Robot Control Programming
  
The 4-in-a-Robot Robot-less version
  
Installing 4-in-a-robot
  
Playing the robotless 4-in-a-robot
  
View the source code
Speech
  
The problems with speech
  
Sounds familiar
  
H8 Timers Background
  
Trial and Error
  
Volume Zapper
  
Actually Speaking
  
Speak.c - the code
  
The VB code generator
Some ideas for the future
  
Room positioning robot
  
Neural Net Bot
  
Pianola
  
Text to speech
LEGO® Mindstorms® Links

[268703]

The 4-in-a-Robot Algorithm

 

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.


Show Topic: 4-in-a-Robot - the Code

Next Page: The 4-in-a-Robot Control Programming