Connect 4 robot

So there was I, halfway through writing another blog post, when this landed on Twitter. Feast your eyes. It’s a Raspberry Pi robot that plays a pretty mean game of Connect 4.

4 Bot – Raspberry Pi Connect 4 playing robot

Written in Python, uses the minimax AI algorithm to calculate moves. Plays a fairly mean game! Thanks to Mark @astro-designs for the PiXi board controller which runs servos and stepper motor.

David Pride, the maker, had to do a considerable amount of building work here: the grippers are the pincers from a MeArm, cunningly mounted on a spare rail for a 3d printer.

IMG_1483-300x225

Connect 4 is not, when you think about it, that straightforward a problem. Happily for us all, David had the great foresight to make a writeup available. He says:

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.

Of course, the robot needs to be able to do some other things besides understanding how to play Connect 4. For starters, it has to be able to differentiate between red and yellow counters, and see where they are in the grid. Earlier this year, David showed off a Lego sorter he’d made with a visual sorting algorithm: here he is with our very own Philip Colligan (who is the fella saying “yep” a lot), demonstrating the visual sorting algorithm in action.

Lego sorting robot

BETT Show 2016 – Raspberry Jam Lego sorting MeArm robot by David Pride

You can read David’s writeup of the Connect 4 build at his website – David, let us know if you put the code on GitHub!