Halite Home

Confused about the Production Bot from erdman's alt-python3 halite starter package


#1

target, direction = max(((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
if neighbor.owner != myID),
default = (None, None),
key = lambda t: t[0].production)
if target is not None and target.strength < square.strength:
return Move(square, direction)
What are the use of these statements? What do they have to do with production? Please explain these line of codes.


#2

Source of the code: https://github.com/erdman/alt-python3-halite-starter/blob/master/ProductionBot.py#L25

I've included a little more context below and added comments. You can look at the tutorial that the ProductionBot comes from to learn more.

def get_move(square):
    # select a target and the direction towards that target
    # we want the target to be the neighbor with the most production
    # that we don't already own
    target, direction = max(((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
                                # filter the list to exclude my own cells
                                if neighbor.owner != myID),
                                # if the list is empty, use (None, None)
                                default = (None, None),
                                # lambda expression to compare cells based on the production 
                                # of the target - the input is (target, direction)
                                key = lambda t: t[0].production)

    # if the list of neighbors wasn't empty (target would be None) and
    # our square is stronger than the target (so we can capture it)
    # then move there!
    if target is not None and target.strength < square.strength:
        return Move(square, direction)
    # otherwise wait until our strength is greater than production * 5
    elif square.strength < square.production * 5:
        return Move(square, STILL)

    # elided remainder of function...

#3

Thanks for the very quick response with the expected answer from the No.1 in the rankings.