How can I get the coordinates of a point using relative location?


#1

This is my first forum post, so I apologize if it’s too basic or has already been posted a million times.

To prevent collisions, I want to be able to check the points that are adjacent to my ships or my shipyard and see if they are occupied. This seems like a pretty simple idea, but I haven’t figured out how to do it. Is there only one ‘position’ value for each ship, or do they have x and y values as well? If the latter is true, how would I be able to retrieve (as an example) the space to the right of a ship?

Thank you!


#2

If you are writing the bot using the Halite starter APIs, you can try calling the Position methods: position.directional_offset(d) or position.get_surrounding_cardinals().

For example,

  • position.directional_offset(Direction.EAST) would give you the position East the position you call the method on

  • get_surrounding_cardinals would give you a list that you can iterate over
    for neighbor in position.get_surrounding_cardinals:

If you want to do it by hand you could create a list of offsets like:

offsets = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for offset in offsets:
    dx = position.x + offset.x
    dy = position.y + offset.y
    neighbor = Position(dx, dy)

#3

This helped! Thank you!


#4

Back with a follow up question! I tried

game_map[me.shipyard].directional_offset(Direction.EAST).is_occupied

but it keeps saying that a MapCell object doesn’t have the directional_offset attribute.

When I remove the ‘game_map’[] part from ‘game_map[me.shipyard]’, it said that shipyard has no directional_offset attribute. I know that a ‘position’ has this attribute, but I don’t know how to get just the position of the shipyard, rather than the MapCell.


#5

Because it doesn’t. MapCell != Position. You need game_map[…].position.directional_offset… Or if you need a shipyard postion you can get it directly: me.shipyard.position.


#6

Okay, thank you! I didn’t realize you had to include the .position part. That makes much more sense.