Data shape and type. Python


#1

I’m using Python kit and I need to understand which are the shapes and data types of direction, position which are given in several functions. Is there somewhere where I can get that info? I’m reading all the .py in the hlt module but I cannot get, for example, which is the data type and structure of destination on naive_navigate(self, ship, destination) or direction in move(self, direction) and how can be extract the x and y position from a ship.

Thanks in advance for anyone who help even if it’s a bit :slight_smile:


#2

The quick way:

If you look through the code, you can see destination is used in a call to get_unsafe_moves and in there you can see destination is sent to normalise, and in the description of normalise you can see it say “A position object” so that’s the type for Destination.


#3

That’s how I’m doing but I keep in a loop in which I don’t know if Destination is a custom data type of a string set as commands: “n”, “w” . Because I try to take the shipyard position but I cannot make it to be used into move()… So except that quick way I’m stack :stuck_out_tongue: Anyway, thank you for you time. I’ll keep trying.


#4

Direction is a tuple saying which way of x and y to move (1, 0) for example. That’s what move() is expecting. There are helpers (Direction.East for example) that translates these.


#5

Maybe if you use the “position.directional_offset(direction)” function? Given a command it will return a coordinates tuple.


#6

I used a convert func:
from copy import deepcopy
def convert(caller, orig_obj, cls=MapCell):
obj = deepcopy(orig_obj)
game_map_ref = None
if hasattr(caller, ‘game’):
game_map_ref = caller.game.game_map
elif hasattr(caller, ‘game_map’):
game_map_ref = caller.game_map
if isinstance(obj, cls):
return obj
elif game_map_ref:
if cls == MapCell:
# wants MapCell
if isinstance(obj, Position):
# from Position
return game_map_ref[obj]
elif isinstance(obj, tuple):
# from tuple
return game_map_ref[Position(*obj)]
elif isinstance(obj, Ship) or isinstance(obj, Entity):
# from ship
return game_map_ref[obj.position]
elif cls == Position:
# wants Position
if isinstance(obj, MapCell) or isinstance(obj, Ship) or isinstance(obj, Entity):
# from MapCell or Ship
return obj.position
elif isinstance(obj, tuple):
# from tuple
return Position(*obj)
elif cls == tuple:
# wants tuple
if isinstance(obj, MapCell) or isinstance(obj, Ship) or isinstance(obj, Entity):
# from MapCell or Ship
return (obj.position.y, obj.position.x)
elif isinstance(obj, Position):
# from Position
return (obj.y, obj.x)

Which you can use like convert(game, object, Position)
Or convert(game, object, MapCell)
Or convert(game, object, tuple)