summaryrefslogtreecommitdiff
path: root/example-bots/manhattan.py
blob: a65d0e963f99f8ffe0caebdadbe64bfadc078a60 (about) (plain)
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
import random

def distance(x1, y1, x2, y2):
    return abs(x1 - x2) + abs(y1 - y2)

def direction_to(rx, ry, fx, fy):
    moves = []
    if fx < rx:
        moves.append('L')
    elif fx > rx:
        moves.append('R')
    if fy < ry:
        moves.append('U')
    elif fy > ry:
        moves.append('D')
    return random.choice(moves)

def closest_factory(whoami, state, rx, ry):
    closest_factory = None
    closest_distance = 9999999

    for fx, fy in state.factories:
        if state[fx, fy].allegiance != whoami:
            d = distance(rx, ry, fx, fy)
            if d < closest_distance:
                closest_factory = (fx, fy)
                closest_distance = d

    return closest_distance, closest_factory

def calculate_orders(whoami, state):
    orders = []

    my_robots = state.robots_by_player[whoami]
    for rx, ry, energy in my_robots:
        distance, factory = closest_factory(whoami, state, rx, ry)

        if factory is None or random.random() < 0.1:
            orders.append(random.choice('UDLRP'))
        elif distance == 0:
            orders.append('P')
        else:
            fx, fy = factory
            orders.append(direction_to(rx, ry, fx, fy))

    return orders