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

Connect 4 game for AI agents

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

Problem

This Connect 4 game will be used for implementing game-playing AI. Sample players are supplied. One takes user input, and the other plays randomly. Right now it's set for a human player to play against a randomly playing agent, but in the future I plan to try out algorithms such as Minimax and have AI agents play against themselves.

Some more features (such as notifying the players when the game is over, instead of just printing to stdout) need to be added for later work, but I'm showing this simply as a working base. I'm not so much looking for feature suggestions, since those will come later. I will be adding docstrings and comments, but for now it should be straight forward.

I'm looking for tips on: program correctness, code structure, and best practices (whether specific to Python or not).

(Python 3.2.3)

```
#!/usr/bin/env python

import numpy as np
from copy import copy
from abc import ABCMeta, abstractmethod

class Connect4Game():
def __init__(self, player1, player2):
self.player1 = player1
self.player2 = player2
self.player_names = {player1 : "Player1", player2 : "Player2"}
self.board = np.zeros((6, 7), dtype=np.int8)
self.turn = player1
self.last_move = ()
self.winner = None

def gameover(self):
if self.last_move == ():
return False

rind, cind = self.last_move

row = self.board[rind, :]
col = self.board[:, cind]
diag1 = np.diagonal(self.board, cind - rind)
diag2 = np.diagonal(np.fliplr(self.board), -(cind + rind - 6))

for line in [row, col, diag1, diag2]:
if line.shape[0] < 4:
continue

for four in [line[i:i+4] for i in range(len(line)-3)]:
if sum(four == 1) == 4:
self.winner = self.player1
return True
elif sum(four == 2) == 4:
self.winner = self.player2
return True

Solution

I would advise using slightly longer names (rind should be row_ind, etc.), adding comments (while you are writing the code, not after) and avoiding counterintuitive variable names such as four. It is quite confusing when you are writing statements such as four == 2.

In addition, I would replace

self.last_move == ()


with

not self.last_move


to future-proof your code in case you switch to using lists or something else entirely.

Also, to simplify your structure, I would suggest splitting up your longer functions into other smaller functions, to make your code more digestible and reusable.

Also, instead of initializing players with tuples of seemingly random numbers, add some constants in your code for further increased readability.

Code Snippets

self.last_move == ()
not self.last_move

Context

StackExchange Code Review Q#56061, answer score: 5

Revisions (0)

No revisions yet.