At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Gaming » Projects

Connect4 Game Project

Connect4 is a simple game that requires little more than iterations to make the computer a very difficult opponent. Actually, it is probably possible to create an opponent that will always beat the player (assuming it starts first).

Outline

The rules of Connect4 are really simple. The game is a turned-based two player game, the winner is the first person to connect4 stones together. The version that I've played requires that the pieces "build" on top of each other (see diagram). The game can be played on a arbitarily sized board, but the smaller the harder - normally a 6x6 board is used.

The diagram shows three moves from the fifth move of the game (blue started). Firstly blue wants to move the third column so the piece is placed in (2,3). Red then moves into the 2nd column, thus the piece is placed at (1,2).

Guidelines

If you have not read the Simple Board Game AI essay, do so. Connect4 AI will merely consist of iterations, and a priority board. Since it is only really numbers (how many in a row) that you need to care about, the heuristics are simple:
  • How many pieces in a row will I get if I move here?
  • Do I block a good path for the enemy if I move here?
This should create quite a reasonable opponent. If you want to improve the AI further you could add heuristics like figuring out when moving to a position would create a favourable space for your opponent. For example, given this board set up:

Notice that if red moves to position 1, he sets up blue to win at position 2. The "do I block a good path for the enemy" heuristic wouldn't pick such a problem up. Therefore, add an additional heuristic:
  • Does the space above give the opponent an advantage?


Solutions

c4_p3.zip (7.54Kb) Solution submitted by Thomas Wright. Interestingly, this solution is for the Psion 3a, 3c and 3mx.
VBConnect4.zip (18Kb) Solution submitted by Stefan Koschik. A VB solution that allows you to vary the aggressiveness of the AI.

Last Updated: 27/10/2002

Article content copyright © James Matthews, 2002.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -