aboutsummaryrefslogtreecommitdiff
path: root/simplebot/simplebot.py
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-02-24 20:44:13 +0100
committerIlion Beyst <ilion.beyst@gmail.com>2022-02-24 20:44:13 +0100
commitb07276f3b364dc55cad09599ffb509e1bb857c4b (patch)
tree8122dabaef7b937aa87ab3aa4bfbe1a459cd15b9 /simplebot/simplebot.py
parentb679103a4ce1ee87394c318f41b4cd12d2d41709 (diff)
downloadplanetwars.dev-b07276f3b364dc55cad09599ffb509e1bb857c4b.tar.xz
planetwars.dev-b07276f3b364dc55cad09599ffb509e1bb857c4b.zip
add simplebot source code
Diffstat (limited to 'simplebot/simplebot.py')
-rw-r--r--simplebot/simplebot.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/simplebot/simplebot.py b/simplebot/simplebot.py
new file mode 100644
index 0000000..b2a6b8f
--- /dev/null
+++ b/simplebot/simplebot.py
@@ -0,0 +1,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
+ })