Minimax algorithm for Connect Four

After the player places a tile in a column, the function runs and returns zeroes (the AI’s identifier) for all rows except the first one and where the player has put a tile. I’ve also observed that if printing the depth, row and column, the depth alternates once but then doesn’t for a long while. Example: 2, 3, 5 then 1, 6, 7 then a long line of just depth 2.

The minmax algorithm:

def minimax(depth, board, maximizing_player):
    # Terminal states. #
    # Low score for player win. #
    if depth == 0 or is_terminal(board):
        if win_check(board, player_id):
            return -9999
        # High score for AI win. #
        elif win_check(board, enemy_id):
            return +9999
        # There is a draw, which equals 0. #
        elif draw_check(board, enemy_id) or draw_check(board, player_id):
            return 0
        else:
            return board_check(board, enemy_id)

    # Gets valid locations. #
    valid_locations = get_valid_locations(board)

    # Will switch back and forth between maximizing and minimizing players. #
    # Maximising Function #
    if maximizing_player:
        best_value = -99999999

        # Checks all the valid locations and returns the highest evaluated one. #
        # Error. #
        for row, column in valid_locations:
            print(depth, row, column)
            # Creates a copy of the board, places random piece in and evaluates. #
            board_copy = board.copy()
            board_copy[row][column] = enemy_id
            new_value = minimax(depth - 1, board_copy, False)
            if new_value > best_value:
                best_value = new_value
                current_column = column

        return best_value

    # Minimising Function #
    else:
        best_value = +99999999

        # Checks all valid positions and finds the lowest rated one. #
        for row, column in valid_locations:
            # Creates a copy of the board, places random piece in and evaluates. #
            board_copy = board.copy()
            print(depth, row, column)
            board_copy[row][column] = player_id
            new_value = minimax(depth - 1, board_copy, True)
            if new_value < best_value:
                best_value = new_value
                current_column = column

        return best_value

My valid locations function creates a tuple of the row and column.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.