Part 1 – Introduction

This tutorial explains, step-by-step, how to build the Artificial Intelligence behind this Connect Four perfect solver

Connect Four game and rules

Connect Four (or Four-in-a-line) is a two-player strategy game played on a 7-column by 6-row board. Each player has a color and drops succesively a disc of his color in one column, the disc falls down to the lowest empty cell of the column. The first player to make an alignment of four discs of his color wins, if the board is filled without alignment it’s a draw game. More details on the game here.

Connect Four example. Author: Silver Spoon. License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)

Solving Connect Four, an history.

Connect Four is a strongly solved perfect information strategy game: first player has a winning strategy whatever his opponent plays. Initially, the game was first solved by James D. Allen (October 1, 1988), and independently by Victor Allis two weeks later (October 16, 1988). At this time, it was not yet feasible to brute force completely the game. Both solutions are based on rule based approaches in combination with knowledge database. James D. Allen’s strategy1 was later published in a more complete book2, while Victor Allis’ solution was published in his thesis3.

Later, with more computational power, the game was strongly solved using brute force resolution. John Tromp extensively solved the game and published in 1995 an opening database providing the outcome (win, loss, draw) of any 8-ply position. John Tromp’s solver4 recently solved the 8x8 board in 2015.

The final step in solving Connect Four is to compute the best number of plies before the end of the game in addition to outcome (win, loss, draw). GameCrafters from Berkely university provided a first online solver5 computing the number of remaining moves to perform the perfect strategy. As well as Christian Kollmann’s solver build as student project in Graz University of Technology6. Mine7, is the acheivement of a nostagic project: my first big computer program was a Connect Four (non perfect) AI, coded long time ago when I was 16 years old.

Purpose of this tutorial

This tutorial is itended to be a pedagogic step-by-step guide explaining the differents algorithms, tricks and optimization requiered to build a very fast Connect Four solver able to solve any valid position in a few milliseconds. We start with a very basic and inefficient solver that will be improved little by little.

I hope this tutorial will be a comprhensive and useful resource for intermediate or advanced algorithm and computer science trainings. C++ source code is provided under the GNU affero GLP licence.

Tutorial plan

References

  1. James D. Allen, Expert Play in Connect-Four 

  2. James D. Allen, The Complete Book of Connect 4: History, Strategy, Puzzles. Sterling Publishing Company (2010). ISBN 1402756216. 

  3. Victor Allis, A Knowledge-based Approach of Connect-Four, Vrije Universiteit, October 1988 

  4. John Tromp, John’s Connect Four Playground 

  5. (defunct) GameCrafters, Berkeley University, Connect Four solver 

  6. Christian Kollmann, Graz University of Technology, Connect Four solver 

  7. Pascal Pons, gamesolver.org, 2015, Connect Four solver 

Updated: