4-Bot – A Raspberry Pi Connect 4 Robot!

Playing games with the Raspberry Pi

IMG_1675

Been a while since I last posted anything – this one has been a work in progress for some time now. My wife bought me a MeArm kit at PiWars at the end of last year which is a brilliant little kit for the price. I wanted to use the arm for some thing really cool, some of you may have seen the Lego sorter that I built for the Raspberry Pi stand at the BETT show in January. If you’ve not seen it, Igor Pavol shot a really good video that you can see at the end of this post.

Game on.

I have always been fascinated by games and logic and the idea for this came after building the Lego sorter and wondered what else the vision / colour capture code could be used for.

I started first off before physically building anything to see if I could first accurately capture the game board, and then get the Pi to play Connect 4 with any reasonable speed and decent AI. I found many versions in Python (almost all of which did not work or would not run on the Pi!) so ended up with a mixture of pre-built Python libraries and my own code.

IMG_1398

Early capture tests – note the experimental lighting rig!

4-Bot uses the same Python Imaging library as the Lego sorter to process the image of the game board. The image is down-sampled to 16 colours to enhance readability and then divided into a grid. Each of the 42 spaces on the board is identified as red, yellow or empty by reading the RGB value of each space in the grid and this data is then saved as an array which is stored as the board state and then passed to the AI. I discovered that there is a well known algorithm called ‘minimax’ which is applicable to games of this nature – and there is a Python library for it 😀 This algorithm uses tree searching methods to look n steps ahead for the next best move. Even on a small 6 x 7 Connect 4 board there are 4,531,985,219,092 possible game positions – yes, that’s TRILLIONS! So getting the Pi to play the game effectively could be quite a challenge. It becomes a trade off between absolute perfect play, and reasonable time for each move. I eventually struck a balance between them where it now plays pretty intelligently but still completes each move in roughly 25 seconds which is acceptable for a flowing game.

Once the vision & AI was sorted, it was time to build the delivery mechanism. Sadly, no matter how I positioned it, the MeArm wasn’t big enough to reach over the entire board as I had hoped, so some serious thinking was required. I am extremely lucky to be a member of Cheltenham Hackspace and I was very fortunate to find some rather useful bits in the pile of donations we have been given!

Building a frame.

IMG_1472

Print some new mounting plates….

IMG_1480

…for the recently amputated MeArm claw (Sorry Ben!)

IMG_1483

Which was then attached to a superb find from Cheltenham Hackspace –  the top rail from a disassembled 3D printer (!)

IMG_1485

I then sprayed the frame and mounted the claw / rail attachment. Some *really* careful measurement was required here to get the token to be lined up exactly with the top of the board to ensure it drops in smoothly every time.

IMG_1522

Wiring it up.

IMG_1550

The servos for the claw, the stepper motor that drives the rail attachment and the LCD message screen are all operated using the PiXi controller board from Astro Designs. It’s a mighty monster of an add on board with FPGA built in so here I am barely scratching the surface of what it can do!

I also needed a neat way of dispensing the bot’s tokens so the claw could grab them each time. A simple vertical holder made from corex-board with an angled lip to stop them all dropping out everywhere works very effectively…

IMG_1675

I finally added a button and LCD message screen so 4-Bot can function as a stand alone robot that runs in a nice continuous game loop.

The finished game.


Overall this is by far the most complex and challenging project I have built with the Pi, and definitely one of the most satisfying. The posts above probably don’t convey the many late nights of head scratching, finger slicing and hair pulling that went into this but hey,  I think it was worth it.

I will tidy up and publish the code shortly. I will also do a ‘software’ only version that anyone can recreate with just the Pi, camera and a Connect 4 board. The Pi will still work out the moves, you’ll just have to make them manually. 😉

Also I should give MASSIVE thanks to Mark from Astro Designs for the PiXi controller board – and for the hours of late night support sessions! For more PiXi info and projects, follow Mark on Twitter @AstroDesignsLtd

This is the Lego sorter from the BETT show that I built using the MeArm

Until next time….