aboutsummaryrefslogtreecommitdiff
path: root/planetwars-cli/assets/simplebot.py
blob: b2a6b8f36efe9b98429d2773ef9ac343a0f64df4 (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
import sys, json

def move(command):
    """ print a command record to stdout """
    moves = []
    if command is not None:
        moves.append(command)

    print(json.dumps({ 'moves': moves }))
    # flush the buffer, so that the gameserver can receive the json-encoded line.
    sys.stdout.flush()


for line in sys.stdin:
    state = json.loads(line)
    # you are always player 1.
    my_planets = [p for p in state['planets'] if p['owner'] == 1]
    other_planets = [p for p in state['planets'] if p['owner'] != 1]

    if not my_planets or not other_planets:
        # we don't own any planets, so we can't make any moves.
        move(None)
    else:
        # find my planet that has the most ships
        planet = max(my_planets, key=lambda p: p['ship_count'])
        # find enemy planet that has the least ships
        destination = min(other_planets, key=lambda p: p['ship_count'])
        # attack!
        move({
            'origin': planet['name'],
            'destination': destination['name'],
            'ship_count': planet['ship_count'] - 1
        })