Use more methods

This commit is contained in:
Gabriel Augendre 2017-08-28 15:30:18 +02:00
parent 8605fbd547
commit d7fb6f278a
No known key found for this signature in database
GPG key ID: F360212F958357D4

View file

@ -47,11 +47,43 @@ class GameMap:
self.my_ships = my_ships self.my_ships = my_ships
self.barrels = barrels self.barrels = barrels
self.map = {}
def reset(self): def reset(self):
self.opponent_ships = [] self.opponent_ships = []
self.cannonballs = [] self.cannonballs = []
self.mines = [] self.mines = []
self.barrels = [] self.barrels = []
self.map = {}
def add_opponent_ship(self, opponent: 'Ship'):
self.opponent_ships.append(opponent)
self._add_to_map(opponent)
def add_my_ship(self, ship: 'Ship'):
my_ship = self.my_ships.get(ship.id, None)
if my_ship:
my_ship.orientation = ship.orientation
my_ship.speed = ship.speed
my_ship.stock = ship.stock
else:
self.my_ships[ship.id] = ship
self._add_to_map(ship)
def add_mine(self, mine: 'Mine'):
self.mines.append(mine)
self._add_to_map(mine)
def add_barrel(self, barrel: 'Barrel'):
self.barrels.append(barrel)
self._add_to_map(barrel)
def add_cannonball(self, cannonball: 'Cannonball'):
self.cannonballs.append(cannonball)
self._add_to_map(cannonball)
def _add_to_map(self, entity: 'Entity'):
self.map[entity.coord] = entity
class Tile: class Tile:
@ -59,6 +91,13 @@ class Tile:
self.y = y self.y = y
self.x = x self.x = x
@property
def coord(self):
return '{};{}'.format(self.x, self.y)
def __repr__(self):
return 'Tile(x={}, y={})'.format(self.x, self.y)
class Entity(Tile): class Entity(Tile):
def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int): def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int):
@ -66,7 +105,7 @@ class Entity(Tile):
self.game_map = game_map self.game_map = game_map
self.id = entity_id self.id = entity_id
def distance(self, other: 'EntityType') -> float: def distance(self, other: 'Entity') -> float:
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5 return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
def nearest_barrel(self) -> Optional['Barrel']: def nearest_barrel(self) -> Optional['Barrel']:
@ -78,7 +117,7 @@ class Entity(Tile):
def nearest_mine(self) -> Optional['Mine']: def nearest_mine(self) -> Optional['Mine']:
return self.nearest(self.game_map.mines) return self.nearest(self.game_map.mines)
def nearest(self, entities: Sequence['EntityType']) -> Optional['EntityType']: def nearest(self, entities: Sequence['Entity']) -> Optional['Entity']:
nearest = None nearest = None
best_dist = math.inf best_dist = math.inf
for entity in entities: for entity in entities:
@ -88,12 +127,18 @@ class Entity(Tile):
nearest = entity nearest = entity
return nearest return nearest
def __repr__(self):
return 'Entity(id={}, x={}, y={})'.format(self.id, self.x, self.y)
class Barrel(Entity): class Barrel(Entity):
def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int, quantity: int): def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int, quantity: int):
super().__init__(game_map, entity_id, x, y) super().__init__(game_map, entity_id, x, y)
self.quantity = quantity self.quantity = quantity
def __repr__(self):
return 'Barrel(id={}, x={}, y={}, quantity={})'.format(self.id, self.x, self.y, self.quantity)
class Ship(Entity): class Ship(Entity):
def __init__(self, game_map: GameMap, def __init__(self, game_map: GameMap,
@ -107,7 +152,7 @@ class Ship(Entity):
self.mine_wait = DEFAULT_MINE_WAIT self.mine_wait = DEFAULT_MINE_WAIT
self.shoot_wait = DEFAULT_SHOOT_WAIT self.shoot_wait = DEFAULT_SHOOT_WAIT
self.action = None # type: Optional[Action] self.action = None # type: Optional[Action]
self.target = None # type: Optional[TargetType] self.target = None # type: Optional[Tile]
def play_turn(self): def play_turn(self):
if self.action is Action.MINE: if self.action is Action.MINE:
@ -133,19 +178,26 @@ class Ship(Entity):
def slower(self): def slower(self):
self.action = Action.SLOWER self.action = Action.SLOWER
def shoot(self, target: 'EntityType'): def shoot(self, target: 'Entity'):
self.action = Action.FIRE self.action = Action.FIRE
self.target = target self.target = target
def move(self, target: 'TargetType'): def move(self, target: Tile):
self.action = Action.MOVE self.action = Action.MOVE
self.target = target self.target = target
def __repr__(self):
return ('Ship(id={}, x={}, y={}, orientation={}, speed={}, stock={}, mine={})'
.format(self.id, self.x, self.y, self.orientation, self.speed, self.stock, self.mine))
class Mine(Entity): class Mine(Entity):
def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int): def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int):
super().__init__(game_map, entity_id, x, y) super().__init__(game_map, entity_id, x, y)
def __repr__(self):
return 'Mine(id={}, x={}, y={})'.format(self.id, self.x, self.y)
class Cannonball(Entity): class Cannonball(Entity):
def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int, shooter_id: int, turns_before_impact: int): def __init__(self, game_map: GameMap, entity_id: int, x: int, y: int, shooter_id: int, turns_before_impact: int):
@ -153,9 +205,10 @@ class Cannonball(Entity):
self.turns_before_impact = turns_before_impact self.turns_before_impact = turns_before_impact
self.shooter_id = shooter_id self.shooter_id = shooter_id
def __repr__(self):
EntityType = TypeVar('EntityType', Entity, Barrel, Ship, Mine, Cannonball) return 'Barrel(id={}, x={}, y={}, shooter={}, before_impact={})'.format(self.id, self.x, self.y,
TargetType = Union[EntityType, Tile] self.shooter_id,
self.turns_before_impact)
def main(): def main():
@ -176,7 +229,7 @@ def main():
if entity_type == BARREL: if entity_type == BARREL:
rhum_quantity = int(arg_1) rhum_quantity = int(arg_1)
game_map.barrels.append(Barrel(game_map, entity_id, x, y, rhum_quantity)) game_map.add_barrel(Barrel(game_map, entity_id, x, y, rhum_quantity))
elif entity_type == SHIP: elif entity_type == SHIP:
orientation = int(arg_1) orientation = int(arg_1)
@ -185,23 +238,19 @@ def main():
mine = int(arg_4) == 1 mine = int(arg_4) == 1
ship = Ship(game_map, entity_id, x, y, orientation, speed, rhum_stock, mine) ship = Ship(game_map, entity_id, x, y, orientation, speed, rhum_stock, mine)
if mine: if mine:
my_ship = game_map.my_ships.get(entity_id, None) game_map.add_my_ship(ship)
if my_ship:
my_ship.orientation = orientation
my_ship.speed = speed
my_ship.stock = rhum_stock
else: else:
game_map.my_ships[entity_id] = ship game_map.add_opponent_ship(ship)
else:
game_map.opponent_ships.append(ship)
elif entity_type == MINE: elif entity_type == MINE:
game_map.mines.append(Mine(game_map, entity_id, x, y)) game_map.add_mine(Mine(game_map, entity_id, x, y))
elif entity_type == CANNONBALL: elif entity_type == CANNONBALL:
shooter_id = int(arg_1) shooter_id = int(arg_1)
turns_before_impact = int(arg_2) turns_before_impact = int(arg_2)
game_map.cannonballs.append(Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact)) game_map.add_cannonball(Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
perr(game_map.map)
# Play # Play
for ship in game_map.my_ships.values(): for ship in game_map.my_ships.values():