
Introduction:
Board games offer a fascinating amount of raw data that is very useful to someone interested in Programming. With 6 rows and 7 columns, Connect 4 has a significant amount of different game scenarios and decision making packed into just one match. This got me interested in designing a strategic algorithm for this classic board game.
How it works:
In the past, I have used a neural network to do all of the learning (like in my article, The Disneyland Algorithm). The nature of this board game makes a neural network a harder choice, just due to the vast amounts of different scenarios it would need to learn from. That is why I moved away from neural networks, and more towards a scoring-based system.
The user makes the first move, and then the program begins scoring all of the next 7 moves it could make to find the best one.


Each of these next 7 moves are scored based on 3 criteria:
- Offensive Benefit: How does this move benefit the computer in a horizontal, vertical, and diagonal direction? If that move will make three in a row for the computer, that move gets 3 points for that direction.
- Defensive Benefit: The same as offensive, but weighted more so the computer takes a more conservative approach to the game.
- Critical Moves: Moves that block the user from getting 4 in a row, moves that win the computer the game, avoiding moves that set up the user to win. All of these are taken into account in the critical moves section.
Below is some of the code for calculating the offensive benefit in the vertical direction for all 7 moves:
scores = zeros(1,7); % array for all 7 offensive scores
opp_scores = zeros(1,7); % array for all 7 defensive scores
for j = 1:7 % repeats for all 7spots
hori = 0; % sets all scoring to 0
vert = 0;
diag1 = 0;
diag2 = 0;
if row_choices(j) ~= 0 % if the row is available for play (not filled up)
if row_choices(j) ~= 6 % if the choice is not in the bottom row
for i = row_choices(i) + 1:6 % looks at all the spaces below potential move
if layout(i,j) == 2 % if computer already has moves below
vert = vert + 1; % calculates how many in a row this move would achieve
else
break
end
end
vert = vert + 1; % adds 1 vertical point regardless (one move is still a tower of 1 in a row)
else
vert = vert + 1; % adds 1 vertical point regardless (one move is still a tower of 1 in a row)
end
The defensive benefit accounts for 70% of the score, while offensive benefit takes up the remaining 30%. Critical move points are added on based on special case situations after the previous scores are totaled. The program returns the scores for all 7 possible moves, and then picks the highest scoring one.
For example, shown below is an output after 1 round of moves from both the user and the program. The first line is the scores for each of the 7 moves, and a "2" is placed at the highest scoring spot (or one of the highest tied spots). The third column from the left was tied at 1.90 points, so it was the chosen move. A "1" is the user’s move, and a "2" is the program’s move.
0.9000 1.2000 1.9000 1.9000 1.9000 1.2000 0.9000
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 2 1 0 0 0
This sequence continues until there is a finished game. A game is over based on the results from the code shown below, which checks every possible location where a straight 4 could exist, and breaks the game loop when that condition is met.
iar = 0; % variable for same colors in a row
done = 0; % break for "for" loop
all_done = 0; % break for larger "for" loop
for i = 1:6 % for all rows
for j = 1:7 % for all columns
if layout(i,j) == 2 % if there is a "2" in that spot
iar = iar + 1; % one more in a row
if iar == 4 % if 4 in a row
all_done = 1; % ends larger "for" loop
done = 1; % ends smaller "for" loop
break % ends the search for horizontal 4 in a row
end
else
iar = 0; % if not 4 in a row, sets back to 0
end
end
iar = 0; % sets back to 0
if done == 1 % breaks a "for" loop
break
end
end
if all_done == 1 % ends game
disp("Loss") % displays that you lost
comp_win = 1; % indicates the computer won
break % ends game loop
end
A finished game is presented below as a win (four 1’s going diagonally), or as a loss (four 2’s going diagonally).
Win
0 0 0 0 2 2 2
0 0 0 1 1 1 2
1 0 2 2 1 2 1
2 0 1 2 2 1 2
1 2 2 1 2 1 1
1 1 2 1 1 1 2
Loss
0 0 0 0 0 0 0
0 0 0 0 2 1 1
0 0 2 0 2 1 2
0 0 1 2 1 2 1
0 0 1 2 2 2 1
0 0 2 1 1 2 1
Conclusion:
I tested out this Connect 4 algorithm against an online Connect 4 computer to see how effective it is. After 10 games, my Connect 4 program had accumulated 3 wins, 3 ties, and 4 losses. While it is not able to win 100% of the games against other computers, it provides the average Connect 4 player with a worthy opponent. It proved to be a decent decision maker when it came to setting itself up for future success, and was a good challenge in developing a board game algorithm.