summaryrefslogtreecommitdiff
path: root/example-bots/pathing.py
blob: d53794084dc4ed629f6d183c3fec6c279bc5847d (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
import random

def move_toward_closest_factory(whoami, state, rx, ry):
    if state[rx, ry].allegiance != whoami and state[rx, ry].city == '+':
        return 'P'
    to_do = [
        (rx + 1, ry, 'R'),
        (rx - 1, ry, 'L'),
        (rx, ry - 1, 'U'),
        (rx, ry + 1, 'D')
    ]
    random.shuffle(to_do)
    seen = []
    while to_do:
        (x, y, move) = to_do.pop(0)
        if (x, y) in seen or state[x, y].city == 'X':
            continue
        seen.append((x, y))
        if state[x, y].allegiance != whoami and state[x, y].city == '+':
            return move
        else:
            to_add = [(x + 1, y, move),
                      (x - 1, y, move),
                      (x, y + 1, move),
                      (x, y - 1, move)]
            random.shuffle(to_add)
            to_do += to_add

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

    my_robots = state.robots_by_player[whoami]
    for rx, ry, energy in my_robots:
        move = move_toward_closest_factory(whoami, state, rx, ry)
        if move and random.random() > 0.1:
            orders.append(move)
        else:
            orders.append(random.choice('UDLRP'))

    return orders