planet-wars/client/simple.py

30 lines
849 B
Python
Raw Permalink Normal View History

2019-09-16 21:18:35 +02:00
import sys, json, random
def move(command):
record = { 'moves': [command] }
2020-03-30 11:32:26 +02:00
f.write(json.dumps(record) + '\n')
f.flush()
2019-09-16 21:18:35 +02:00
print(json.dumps(record))
sys.stdout.flush()
2020-03-30 11:32:26 +02:00
f_name = f"bots/bot{random.randint(0, 10)}.txt"
2019-09-16 21:18:35 +02:00
f = open(f_name,"w+")
f.flush()
2020-03-30 11:32:26 +02:00
2019-09-16 21:18:35 +02:00
for line in sys.stdin:
state = json.loads(line)
# 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:
move(None)
else:
planet = max(my_planets, key=lambda p: p['ship_count'])
dest = min(other_planets, key=lambda p: p['ship_count'])
move({
'origin': planet['name'],
'destination': dest['name'],
'ship_count': planet['ship_count'] - 1
})