summaryrefslogtreecommitdiff
path: root/example-bots/adaptive.py
blob: 18f8717634429a7d6cd345877ad315227b79ae45 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import random

def move_toward_closest_factory(whoami, width, height, board, rx, ry, capture_types):
    if board[rx, ry].allegiance != whoami and board[rx, ry].city in capture_types:
        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)
        x %= width
        y %= height
        if (x, y) in seen or board[x, y].city not in ['+', '.']:
            continue
        seen.append((x, y))
        if board[x, y].allegiance != whoami and board[x, y].city in capture_types:
            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):
    # In order to start painting, we need to be leading by at least
    # this percentage (per opposing player).
    leading_factor_per_opponent = 0.1 # 10%
    opponents = (len(state.robots_by_player) - 1)
    factor = 1 - (leading_factor_per_opponent * opponents)

    orders = []

    board = state.board
    width = state.width
    height = state.height
    my_robots = state.robots_by_player[whoami]
    max_robots = max(
        len(robots)
        for player, robots in state.robots_by_player.items()
        if player != whoami
    )
    my_factories = state.factories_by_player[whoami]
    max_factories = max(
        len(factories)
        for player, factories in state.factories_by_player.items()
        if player != whoami
    )
    if factor * len(my_robots) < max_robots or factor * len(my_factories) < max_factories:
        capture_types = ['+']
    else:
        capture_types = ['+', '.']
    for rx, ry, energy in my_robots:
        move = move_toward_closest_factory(whoami, width, height, board, rx, ry, capture_types)
        orders.append(move or random.choice('UDLRP'))

    return orders