13,045,790 members (46,342 online)
alternative version

#### Stats

77K views
22 bookmarked
Posted 15 May 2004

# Connect4 using MIN-MAX algorithm

, 15 May 2004
 Rate this:
MiniMax algorithm can be used in game AI.

## Introduction

This is the traditional Connect4 game that we all played, trying to build a row consisting of 4 pieces of the same color (horizontal, vertical or diagonal).

It will be very helpful for the programmers that need to learn general game AI to put in any turn based strategy game like Tic Tac Toe or even Chess.

I made this game on C++ two years ago. I converted all the code to C# to see how easy it is to convert C++ code to C#.

## Implementation

The project contains the Form1 class which contains simple user interface and drawing functions. Function Form1.Draw takes the Graphics object of the form to draw the board based on the saved values for the pieces.

The Connect4Board class is the one that was converted from C++ and it contains all the AI code and the game rules and functions.

## The Game AI

I am very sorry that the code of the game AI may seem unreadable because it was done under C++ first. So I'll try to explain the AI Algorithm breifly so you don't need to read the code.

The AI is based on the MiniMax algorithm. It works by a recursion function that checks the 7 possible plays that the computer can make. Inside each one of them it checks the 7 plays that the player may play after. Then inside each one of the player, it checks again for the next 7 plays for the computer and go on until a certain recursion level like (3 or 5) levels.

Then it position the board state and give it a score based on the opportunities for the computer and for the player. The score is returned per level to the upper level until reach the first level where there is a score for every computer possible play. If we are on a computer recursion level, the best score is returned to the previous level. If we are in a player level then the worse score is returned because the player would make the better move for him against the computer.

For those who got confused I'll try to say it with another words.

First we need a function that calculates a score for the board position based on opportunities for both the computer and the player. This function is called 'int position()' in the code.

An opportunity is an empty place (a hole) that has around it, a row of 3 pieces of the same color (horizontal, vertical or diagonal). If around it 2 pieces, then it takes less score. The board position score is the sum of all the scores of the computer - the scores of the player.

Then we need to figure out when to check for the position of the board. A possible way is to check after every computer move and that will be 7 possible moves for the computer in Connect4. So we add a move for the board. Calculates the score of the board. Then remove this piece. Then we add another move until all the moves has been given a score. So the computer will decide to play the best move based on the maximum score.

This approach will work fine if the function that calculates the score, is very complex code that give a score for every next turn. But we will not be able to make this function. So the better approach is to calculate the score after the player put his coin also. That would need to check for all the possiblilties for the computer and player will play. That means for every one of the 7 plays for the computer we can have 7 plays for the player. That would be 49 position to check for score. Then we return the worst position on every 7 plays of the player. Then we take the best position on every 7 plays of the computer.

The more you give the algorithm more recursion levels, the more it can predict the best play in the future. So I made the easy level has 3 levels of recursion (Checks for the computer turn, the player next and the computer next) and the hard level has 5 levels of recursion to make better estimates while having longer processing time.

This algorithm can be used for any turn based strategy game like Chess or something. But the more complex the game is. You will need to make complex score function. And have more recursion levels. And the most important is to remove some of the bad plays on every level to reduce the processing cost. Because you don't have to see all the future for a turn that will make you lose a lot of score.

I hope you enjoy the game and the algorithm.

## Share

IF YOU WANT TO IMPROVE YOUR SOFTWARE, DON'T TEST MORE; DEVELOP BETTER.

http://bishoylabib.blogspot.com

## You may also be interested in...

 Pro

 First Prev Next
 Is c++ available? Member 129399348-Jan-17 5:59 Member 12939934 8-Jan-17 5:59
 It works fine under vs2010 jmtree25-Sep-12 11:16 jmtree 25-Sep-12 11:16
 Hi Alisalahi28-Jun-12 2:15 Alisalahi 28-Jun-12 2:15
 My vote of 1 MR.Haghighat10-Aug-09 7:31 MR.Haghighat 10-Aug-09 7:31
 New code for connect4 Bishoy Labib11-Apr-09 12:31 Bishoy Labib 11-Apr-09 12:31
 MiniMax for Tic Tac Toe Mario Campo18-Sep-08 23:07 Mario Campo 18-Sep-08 23:07
 [Message Deleted] Danny Rodriguez27-Jan-08 9:16 Danny Rodriguez 27-Jan-08 9:16
 code comments KaptainKruton15-Sep-05 22:11 KaptainKruton 15-Sep-05 22:11
 C++ code KristjanK5-Sep-04 22:27 KristjanK 5-Sep-04 22:27
 Last Visit: 31-Dec-99 18:00     Last Update: 22-Jul-17 23:30 Refresh 1