
Connect 4 is a game in which the 2 players take turns dropping coloured pieces into a grid consisting of 7 columns and 6 rows. The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one’s own pieces.
I have recently added a page to my site where the user can play the Connect 4 game, you can find it here.
Coding the user-interface and the logic to allow 2 human players to play against each other was quite simple. The fun started when I tried to implement some logic to allow for a computer opponent.
My initial approach was a very simple algorithm:
- If the computer had a winning move, make that move
- If the human player could win if it were their move, block their move
- If making a move would enable the human to play a winning move above that move, erase that move from the list of candidate moves
- From the remaining list of candidate moves, select a move at random, with a bias towards moves nearer the centre column (there are more winning chances near the centre)
On trying this, it immediately became apparent that step 4 was not a very good approach. My next attempt was to create simple scoring function for step 4 and make the move which scored highest.
There are 69 possible winning lines in the game:
- 24 horizontal, 4 for each row (starting at columns 1, 2, 3 and 4)
- 12 diagonally down to the left, 4 for each row 1, 2 and 3 (starting at columns 4, 5, 6 and 7)
- 12 diagonally down to the right, 4 for each row 1, 2 and 3 (starting at columns 1, 2, 3 and 4)
- 21 vertical, 7 for each row 1, 2 and 3
The scoring function simply counted the number of rows that the computer had with a high score for rows of 3, a medium score for a row of 2 and a minimum score for a row of 1. It calculated this score for each possible move, selecting the move with the highest score.
This approach was a definite improvement, but it does not take into account what the human player is aiming toward. Implementing a look-ahead was the next step, but this is costly in terms of processing time; there are a maximum of 7 possible moves at any stage and we would need to process 7 * 7 * 7 * 7 positions (ie 2401 positions) if we looked ahead 4 plies. This is called the MiniMax algorithm because one player is trying to maximise their score from the evaluation function while the other player is trying to minimise it.
I then implemented the alpha-beta pruning algorithm, which reduces the number of positions which need to be analysed. This is analogous to the chess player rejecting a possible sequence of moves because they would lose material. This allowed me to increase the look ahead to 7 plies, while maintaining tolerable processing times.
Best results are achieved with a combination of the look ahead and the scoring function. Play improves with the length of the look-ahead, and it also improves with the accuracy of the scoring function.
The computer’s success rate with my algorithm above is still quite low; I am continuing to experiment with the scoring function and hope to improve the computer performance.
The AI plays a tolerable game, although still beatable.
Are you aware that Connect 4 has been solved completely? With best play it should result in a win for the first player.
I was not aware that the game had been solved when I wrote the original code.
Since then, as I have been trying to improve the AI, I have become aware of the solution. However, I do not want to use that, as it would make the game boring to play.
I will continue to look for a better algorithm, albeit it will still be imperfect.