You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Halite II Python机器人无法运行:停靠最近行星逻辑报错

Fixing the 'int' object has no attribute 'is_owned' Error in Your Halite II Bot

Alright, let's break down what's going wrong with your bot and how to fix it. The AttributeError you're hitting is a classic mix-up of data types, and it's easy to resolve once we trace the issue.

The Root Cause

Your arr_distances function returns an OrderedDict where keys are planet IDs (integers) and values are the calculated distances. When you loop with for planet in distances:, you're actually iterating over the dictionary's keys—so planet here is just an integer ID, not a full Planet object. That's why calling planet.is_owned() throws an error: integers don't have that method.

Fix Options

You have two straightforward ways to fix this. Let's walk through both:

Option 1: Return Sorted Planet Objects Directly (Cleanest Approach)

Instead of returning a dictionary of IDs and distances, modify arr_distances to return a list of Planet objects sorted by distance from the ship. This way, your loop will work with actual Planet instances right away.

Update your arr_distances function:

def arr_distances(ship, game_map):
    # Create a list of (distance, planet) tuples
    planet_distance_pairs = []
    for planet in game_map.all_planets():
        # Use Halite's built-in distance method instead of manual calculation
        distance = game_map.distance_between(ship, planet)
        planet_distance_pairs.append( (distance, planet) )
    
    # Sort the list by distance (ascending order)
    planet_distance_pairs.sort(key=lambda x: x[0])
    
    # Extract just the planets from the sorted tuples
    return [planet for (dist, planet) in planet_distance_pairs]

Then keep your main loop structure—now distances is a list of sorted Planet objects, so your existing logic works as intended:

for planet in distances:
    if planet.is_owned():
        continue
    # Rest of your docking/movement logic stays the same

Option 2: Look Up Planets by ID (If You Want to Keep the Dictionary)

If you prefer to retain the dictionary of IDs and distances, just fetch the actual Planet object from the game map using the ID before calling any methods on it.

Modify your main loop's planet iteration:

for planet_id in distances:
    # Get the Planet object using its ID
    planet = game_map.get_planet(planet_id)
    if planet.is_owned():
        continue
    # Rest of your docking/movement logic stays the same

Bonus: Simplify Distance Calculation

Notice in Option 1, I used game_map.distance_between(ship, planet) instead of your manual square root calculation. Halite's game map has optimized built-in methods for this—always prefer these over custom calculations to avoid errors and improve performance.

Testing the Fix

After making either change, rerun your bot. The AttributeError should disappear, and your ships will now correctly target the closest unowned planets instead of the map center.


内容的提问来源于stack exchange,提问作者Abdou Gadou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:36:04