forked from lapaep/trainingsplatz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submission.py
59 lines (48 loc) · 2.56 KB
/
submission.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Imports helper functions
from kaggle_environments.envs.halite.helpers import *
BOTNAME = "Default"
# Returns best direction to move from one position (fromPos) to another (toPos)
# Example: If I'm at pos 0 and want to get to pos 55, which direction should I choose?
def getDirTo(fromPos, toPos, size):
fromX, fromY = divmod(fromPos[0],size), divmod(fromPos[1],size)
toX, toY = divmod(toPos[0],size), divmod(toPos[1],size)
if fromY < toY: return ShipAction.NORTH
if fromY > toY: return ShipAction.SOUTH
if fromX < toX: return ShipAction.EAST
if fromX > toX: return ShipAction.WEST
# Directions a ship can move
directions = [ShipAction.NORTH, ShipAction.EAST, ShipAction.SOUTH, ShipAction.WEST]
# Will keep track of whether a ship is collecting halite or carrying cargo to a shipyard
ship_states = {}
# Returns the commands we send to our ships and shipyards
def agent(obs, config):
size = config.size
board = Board(obs, config)
me = board.current_player
# If there are no ships, use first shipyard to spawn a ship.
if len(me.ships) == 0 and len(me.shipyards) > 0:
me.shipyards[0].next_action = ShipyardAction.SPAWN
# If there are no shipyards, convert first ship into shipyard.
if len(me.shipyards) == 0 and len(me.ships) > 0:
me.ships[0].next_action = ShipAction.CONVERT
for ship in me.ships:
if ship.next_action == None:
### Part 1: Set the ship's state
if ship.halite < 200: # If cargo is too low, collect halite
ship_states[ship.id] = "COLLECT"
if ship.halite > 500: # If cargo gets very big, deposit halite
ship_states[ship.id] = "DEPOSIT"
### Part 2: Use the ship's state to select an action
if ship_states[ship.id] == "COLLECT":
# If halite at current location running low,
# move to the adjacent square containing the most halite
if ship.cell.halite < 100:
neighbors = [ship.cell.north.halite, ship.cell.east.halite,
ship.cell.south.halite, ship.cell.west.halite]
best = max(range(len(neighbors)), key=neighbors.__getitem__)
ship.next_action = directions[best]
if ship_states[ship.id] == "DEPOSIT":
# Move towards shipyard to deposit cargo
direction = getDirTo(ship.position, me.shipyards[0].position, size)
if direction: ship.next_action = direction
return me.next_actions