A robot that plays Connect 4 against a human opponent thanks to a bit of maths and code

You may have seen computers that can defeat grandmasters at chess, win at trivia game shows or, more recently, beat a human at Go, but have you ever played Connect 4 against a robot? Humour us by answering no (statistically speaking, you probably haven’t) and then prepare to be amazed at the 4bot created by David Pride.

The full article can be found in The MagPi 46

The 4bot has humble beginnings, as David explains its origins to us:

“My wife bought me the brilliant MeArm kit and I used it to build a Lego block sorter, as you do. This used the Pi Camera Module and a colour recognition script I wrote in Python to identify the different coloured blocks, and then used the arm to drop them in the correct ‘buckets’.”

“Based on this, I was then looking for other uses for the colour-capture code,” David continues. “Connect 4 seemed like a really good choice. Research soon led me to find that the game, and the logic behind it, is far from simple. There is good information online; however, whilst I found many versions of Connect 4 for Python, few of them ran successfully on the Pi.”

The robot works by taking a picture of the game board, processing the colours, and then giving the game program the state of the board to calculate the next move.

“In terms of how well it plays, Connect 4 is a ‘perfect’ game in mathematical terms,” David reveals. “There are a huge but finite number of solutions, and they can all be calculated with enough processing power. The trade-off is in the depth of search and therefore the time taken to calculate each move… If you increase the search depth, this massively increases the calculation time. So I selected a middle ground where the bot plays a pretty meangame, but the total time per move is still acceptable. With capturing and processing the image, calculating the next move, and delivering thecounter, the total time per turn is around 25 seconds.”

 Eben has a bit of fun at the big birthday bash

Recognising the colours currently in play is not easy, though, and took a bit of trial and error to get right.

“The trickiest part was undoubtedly capturing the game board accurately every time,” David tells us. “It is extremely lightdependent, as the Python module works by capturing the RGB value of the 42 spaces on the board. These values, however, do change dramatically depending on the lighting. I wrote a ‘testcard’ script that can be run with counters in known locations. This script then reports back what it thinks it sees, and the tolerances for the RGB components can then be adjusted until the result matches what is actually there on the real game board. This made the game more portable, as it can be adjusted to its surroundings each time.”

Current upgrades for the robot involve a stronger frame, as it came in for heavy use at the fourth Pi birthday party. Maybe it’s time to bring robot Connect 4 battles to the next Pi Wars?…

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.