Improve mines avoidance

This commit is contained in:
Gabriel Augendre 2017-08-28 17:37:39 +02:00
parent 47fb9df56c
commit 2f00def791
No known key found for this signature in database
GPG key ID: F360212F958357D4

View file

@ -93,7 +93,7 @@ class GameMap:
self.map[entity.coord] = entity
def get_at(self, tile: 'Tile'):
return self.map[tile.coord]
return self.map.get(tile.coord, Tile(tile.x, tile.y))
class Tile:
@ -101,6 +101,9 @@ class Tile:
self.y = y
self.x = x
def distance(self, other: 'Tile') -> float:
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
@property
def coord(self):
return '{};{}'.format(self.x, self.y)
@ -149,15 +152,15 @@ class Entity(Tile):
self.game_map = game_map
self.id = entity_id
def distance(self, other: 'Entity') -> float:
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
@property
def nearest_barrel(self) -> Optional['Barrel']:
return self.nearest(self.game_map.barrels)
@property
def nearest_opponent(self) -> Optional['Ship']:
return self.nearest(self.game_map.opponent_ships)
@property
def nearest_mine(self) -> Optional['Mine']:
return self.nearest(self.game_map.mines)
@ -206,25 +209,26 @@ class Ship(Entity):
perr('pos', self.tile)
perr('mine_wait', self.mine_wait)
perr('shoot_wait', self.shoot_wait)
perr('on path', self.tiles_on_path)
perr('distance oppo', self.distance(self.nearest_opponent))
perr('on path', self.tiles_on_path())
for tile in self.tiles_on_path():
for tile in self.tiles_on_path:
if isinstance(tile, Mine) or isinstance(tile, Cannonball):
action, target = self.avoid(tile)
break
if not action:
if (self.mine_wait > 0 and self.shoot_wait > 0) or rand > 0.4:
if 0.2 < rand <= 0.4 and self.shoot_wait <= 0 and self.distance(self.nearest_opponent.tiles_on_path[-1]) < 10:
action = Action.FIRE
target = self.nearest_opponent.tiles_on_path[-1]
elif rand <= 0.2 and self.mine_wait <= 0:
action = Action.MINE
else:
target = Tile(random.randint(0, 23), random.randint(0, 21))
if self.game_map.barrels:
target = self.nearest_barrel()
target = self.nearest_barrel
action = Action.MOVE
elif rand > 0.2:
action = Action.FIRE
target = self.nearest_opponent()
else:
action = Action.MINE
if action is Action.MINE:
self.mine_wait = DEFAULT_MINE_WAIT
@ -243,28 +247,31 @@ class Ship(Entity):
action = Action.MOVE
target = None
if self.orientation is Orientation.RIGHT:
target = tile.top_right()
elif self.orientation is Orientation.TOP_RIGHT:
target = tile.right()
elif self.orientation is Orientation.TOP_LEFT:
target = tile.left()
elif self.orientation is Orientation.LEFT:
target = tile.top_left()
elif self.orientation is Orientation.BOTTOM_LEFT:
elif self.orientation is Orientation.TOP_RIGHT:
target = tile.left()
elif self.orientation is Orientation.BOTTOM_RIGHT:
elif self.orientation is Orientation.TOP_LEFT:
target = tile.bottom_left()
elif self.orientation is Orientation.LEFT:
target = tile.bottom_right()
elif self.orientation is Orientation.BOTTOM_LEFT:
target = tile.right()
elif self.orientation is Orientation.BOTTOM_RIGHT:
target = tile.top_right()
return action, target
@property
def tile(self):
return Tile(self.x, self.y)
@property
def tiles_on_path(self):
tile = self.tile
perr('tile', tile)
tiles = []
for _ in range(self.speed + 1):
tiles = [tile]
if self.speed == 0:
return tiles
for _ in range(self.speed + 2):
if self.orientation is Orientation.RIGHT:
tile = tile.right()
elif self.orientation is Orientation.TOP_RIGHT:
@ -277,7 +284,7 @@ class Ship(Entity):
tile = tile.bottom_left()
elif self.orientation is Orientation.BOTTOM_RIGHT:
tile = tile.bottom_right()
tiles.append(tile)
tiles.append(self.game_map.get_at(tile))
return tiles
@ -345,6 +352,7 @@ def main():
shooter_id = int(arg_1)
turns_before_impact = int(arg_2)
game_map.add_cannonball(Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
perr('detected cannonball', Cannonball(game_map, entity_id, x, y, shooter_id, turns_before_impact))
perr(game_map.map)