HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Checking for a win in Connect Four

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
winconnectcheckingfourfor

Problem

I'm making a simple connect four game that I will eventually extend by creating an AI that the player can play against. Given that AI algorithms are reasonably complex, I find myself wanting to reduce the algorithmic complexity of my win-checker in order to maximize performance once the entire thing is built.

Currently, my win-checker checks all spaces on the board for wins. My win-checking is separated into 3 different functions - one for checking horizontally, another vertically, yet another diagonally. Each algorithm is essentially the same; I've copied the WinChecker class below for your understanding. As you can see, the win checking methods are all very similar with only 2 or 3 lines of code have been changed.

The issue is that as it is designed currently, each time a player places a piece, my program checks roughly 200 possible combinations on the board for a win. If I am to use this same algorithm to help the AI deduce what move to make, I should find myself with a slow AI. However, even if I had plenty of processing power to resolve these problems quickly, I'd like to learn a bit about algorithms and make my program as efficient as possible.

How can I improve my win-checking algorithm to achieve better or maximum computational efficiency?

Any other comments about my code are also welcome.

The whole project is available on Github, if seeing more of the program will help you analyze the code.

```
class WinChecker:

def __init__(self, game, board):
self.game = game
self.board = board

def check_for_win(self):
results = []
results.append(self.check_win_horizontally())
results.append(self.check_win_vertically())
results.append(self.check_win_diagonally())
try:
win = results.index(True)
# if the above statement does not generate a ValueError, then a win exists
return True
except ValueError:
return False

def check_win_horizontally

Solution

Instead of checking the entire board each time, write a function which checks if placing a piece on a given field will result in a win. This can then be used both for checking whether the last actual piece played was a winning piece, or when implementing the AI whether this is a good place to place it.

A simplistic approach to an AI version, would be to let this win function return how many pieces of the specific kind is placed in this row, and whether there is room for the remaining pieces to be placed. That is the function could return max of how many pieces of your kind is in the row (and if its 4 you win), and how many available spaces are there within a reach of 4 pieces.

Finally when contemplating which move to make next, you don't need to check all places, as the pieces drop to the bottom, so you only need to check one for each column.

Context

StackExchange Code Review Q#112948, answer score: 7

Revisions (0)

No revisions yet.