import numpy, random

"""
Board represents a four in a row game board.

Author: Isaac Arvestad
"""
class Board:
    """
    Initializes the game with a certain number of rows
    and columns.
    """
    def __init__(self, rows, columns):
        self.rows = rows
        self.columns = columns
        self.matrix = numpy.zeros((rows, columns))

    """
    Attempts to add a piece to a certain column. If the column is 
    full the move is illegal and false is returned, otherwise true 
    is returned.
    """
    def add_piece(self, column, value):
        "Check if column is full."
        if self.matrix.item(0,column) != 0:
            return False

        "Place piece."
        for y in range(self.rows):
            if y == self.rows - 1: # Reached bottom
                self.matrix.itemset((y, column), value)
                break
            elif self.matrix.item(y + 1, column) == 0: # Next row is also empty
                continue
            else: # Next row is not empty
                self.matrix.itemset((y, column), value)
                break
        return True

    """
    Returns the column which the ai want to place their piece in.
    Column is 0 indexed. Returns -1 if no moves are available.
    """
    def get_ai_move(self):
        available_moves = []
        for i in range(self.columns):
            if self.matrix.item(0,i) == 0:
                available_moves.append(i)
        if len(available_moves) == 0:
            return -1

        index = random.randint(0, len(available_moves)-1)
        return available_moves[index]
        

@@ -1568,17 +1568,1528 @@
 %5Bindex%5D%0A
-        
+%0A    %22%22%22%0A    Evaluates the board position and checks if a player has won, a draw%0A    has occured or if the game if unfinished. Does not check if both %0A    players have four in a row as it is assumed that the board is %0A    evaluated every move.%0A%0A    Returns an interger where:%0A    0 = unfinished%0A    1 = player 1 won%0A    2 = player 2 won%0A    3 = draw%0A    %22%22%22%0A    def evaluate_board(self):%0A        # Check vertial wins%0A        for x in range(self.columns):%0A           for y in range(self.rows-3):%0A               initial_piece = self.matrix.item((y,x))%0A               if initial_piece == 0:%0A                   continue%0A               for dy in range(0,4):%0A                   next_piece = self.matrix.item((y+dy,x))%0A                   if next_piece != initial_piece:%0A                       break%0A                   if dy == 3:%0A                       return initial_piece%0A        # Check horizontal wins%0A        for y in range(self.rows):%0A            for x in range(self.columns - 3):%0A                initial_piece = self.matrix.item((y,x))%0A                if initial_piece == 0:%0A                    continue%0A                for dx in range(0,4):%0A                    next_piece = self.matrix.item((y,x+dx))%0A                    if next_piece != initial_piece:%0A                        break%0A                    if dx == 3:%0A                        return initial_piece%0A        %0A        # Check for draw%0A        for x in range(self.columns):%0A            if self.matrix.item((0,x)) == 0:%0A                return 0%0A        return 3
 %0A

Add board evaluation. Does not evaluate diagonals.