planet-wars/backend/static/bot/simple.py

25 lines
708 B
Python
Raw Permalink Normal View History

2020-03-29 11:28:57 +02:00
import sys, json, random
2020-04-08 01:48:17 +02:00
def move(moves):
record = {'moves': moves}
2020-04-02 22:21:40 +02:00
print(json.dumps(record))
2020-03-29 11:28:57 +02:00
sys.stdout.flush()
for line in sys.stdin:
state = json.loads(line)
2020-03-31 12:49:10 +02:00
2020-03-29 11:28:57 +02:00
# find planet with most ships
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:
2020-04-08 01:48:17 +02:00
move([])
2020-03-29 11:28:57 +02:00
else:
planet = max(my_planets, key=lambda p: p['ship_count'])
dest = min(other_planets, key=lambda p: p['ship_count'])
2020-04-08 01:48:17 +02:00
move([{
2020-03-29 11:28:57 +02:00
'origin': planet['name'],
'destination': dest['name'],
'ship_count': planet['ship_count'] - 1
2020-04-08 01:48:17 +02:00
}])