From dacc05a41b77bf2e86e27ac354db9b047c661a7d Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 28 Dec 2021 14:57:41 +0100 Subject: refactor workspace code --- planetwars-cli/assets/pw_workspace.toml | 12 ++++------ planetwars-cli/assets/simplebot.py | 33 -------------------------- planetwars-cli/assets/simplebot/botconfig.toml | 2 ++ planetwars-cli/assets/simplebot/simplebot.py | 33 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 41 deletions(-) delete mode 100644 planetwars-cli/assets/simplebot.py create mode 100644 planetwars-cli/assets/simplebot/botconfig.toml create mode 100644 planetwars-cli/assets/simplebot/simplebot.py (limited to 'planetwars-cli/assets') diff --git a/planetwars-cli/assets/pw_workspace.toml b/planetwars-cli/assets/pw_workspace.toml index 85a4ab6..d82840f 100644 --- a/planetwars-cli/assets/pw_workspace.toml +++ b/planetwars-cli/assets/pw_workspace.toml @@ -1,10 +1,6 @@ -[bots] +[paths] +maps_dir = "maps" +matches_dir = "matches" -# define a bot called simplebot [bots.simplebot] - -# The working directory for the bot. -path = "./bots/simplebot" - -# What command to use for running the bot -argv = ["python", "simplebot.py"] \ No newline at end of file +path = "bots/simplebot" diff --git a/planetwars-cli/assets/simplebot.py b/planetwars-cli/assets/simplebot.py deleted file mode 100644 index b2a6b8f..0000000 --- a/planetwars-cli/assets/simplebot.py +++ /dev/null @@ -1,33 +0,0 @@ -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 - }) diff --git a/planetwars-cli/assets/simplebot/botconfig.toml b/planetwars-cli/assets/simplebot/botconfig.toml new file mode 100644 index 0000000..b3a4163 --- /dev/null +++ b/planetwars-cli/assets/simplebot/botconfig.toml @@ -0,0 +1,2 @@ +name = "simplebot" +run_command = "python3 simplebot.py" \ No newline at end of file diff --git a/planetwars-cli/assets/simplebot/simplebot.py b/planetwars-cli/assets/simplebot/simplebot.py new file mode 100644 index 0000000..b2a6b8f --- /dev/null +++ b/planetwars-cli/assets/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 + }) -- cgit v1.2.3