summaryrefslogtreecommitdiff
path: root/example-bots
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@zancanaro.id.au>2022-09-25 00:10:13 +1000
committerCarlo Zancanaro <carlo@zancanaro.id.au>2022-09-25 00:38:04 +1000
commit7daa5284f9eddf6d4b4e7838919e80ce25324bb0 (patch)
treeac92d9aa97621b4c314441a4cb7cdcab60b4f85e /example-bots
parente031af6e5e8324fe4cda66d9597904040b17ca80 (diff)
Lots of changes!HEADmaster
Diffstat (limited to 'example-bots')
-rw-r--r--example-bots/adaptive.py63
-rw-r--r--example-bots/manhattan.py46
-rw-r--r--example-bots/pathing.py40
-rw-r--r--example-bots/random.py11
-rw-r--r--example-bots/random_valid.py30
-rw-r--r--example-bots/up_bot.py11
6 files changed, 201 insertions, 0 deletions
diff --git a/example-bots/adaptive.py b/example-bots/adaptive.py
new file mode 100644
index 0000000..18f8717
--- /dev/null
+++ b/example-bots/adaptive.py
@@ -0,0 +1,63 @@
+import random
+
+def move_toward_closest_factory(whoami, width, height, board, rx, ry, capture_types):
+ if board[rx, ry].allegiance != whoami and board[rx, ry].city in capture_types:
+ return 'P'
+ to_do = [
+ (rx + 1, ry, 'R'),
+ (rx - 1, ry, 'L'),
+ (rx, ry - 1, 'U'),
+ (rx, ry + 1, 'D')
+ ]
+ random.shuffle(to_do)
+ seen = []
+ while to_do:
+ (x, y, move) = to_do.pop(0)
+ x %= width
+ y %= height
+ if (x, y) in seen or board[x, y].city not in ['+', '.']:
+ continue
+ seen.append((x, y))
+ if board[x, y].allegiance != whoami and board[x, y].city in capture_types:
+ return move
+ else:
+ to_add = [(x + 1, y, move),
+ (x - 1, y, move),
+ (x, y + 1, move),
+ (x, y - 1, move)]
+ random.shuffle(to_add)
+ to_do += to_add
+
+def calculate_orders(whoami, state):
+ # In order to start painting, we need to be leading by at least
+ # this percentage (per opposing player).
+ leading_factor_per_opponent = 0.1 # 10%
+ opponents = (len(state.robots_by_player) - 1)
+ factor = 1 - (leading_factor_per_opponent * opponents)
+
+ orders = []
+
+ board = state.board
+ width = state.width
+ height = state.height
+ my_robots = state.robots_by_player[whoami]
+ max_robots = max(
+ len(robots)
+ for player, robots in state.robots_by_player.items()
+ if player != whoami
+ )
+ my_factories = state.factories_by_player[whoami]
+ max_factories = max(
+ len(factories)
+ for player, factories in state.factories_by_player.items()
+ if player != whoami
+ )
+ if factor * len(my_robots) < max_robots or factor * len(my_factories) < max_factories:
+ capture_types = ['+']
+ else:
+ capture_types = ['+', '.']
+ for rx, ry, energy in my_robots:
+ move = move_toward_closest_factory(whoami, width, height, board, rx, ry, capture_types)
+ orders.append(move or random.choice('UDLRP'))
+
+ return orders
diff --git a/example-bots/manhattan.py b/example-bots/manhattan.py
new file mode 100644
index 0000000..a65d0e9
--- /dev/null
+++ b/example-bots/manhattan.py
@@ -0,0 +1,46 @@
+import random
+
+def distance(x1, y1, x2, y2):
+ return abs(x1 - x2) + abs(y1 - y2)
+
+def direction_to(rx, ry, fx, fy):
+ moves = []
+ if fx < rx:
+ moves.append('L')
+ elif fx > rx:
+ moves.append('R')
+ if fy < ry:
+ moves.append('U')
+ elif fy > ry:
+ moves.append('D')
+ return random.choice(moves)
+
+def closest_factory(whoami, state, rx, ry):
+ closest_factory = None
+ closest_distance = 9999999
+
+ for fx, fy in state.factories:
+ if state[fx, fy].allegiance != whoami:
+ d = distance(rx, ry, fx, fy)
+ if d < closest_distance:
+ closest_factory = (fx, fy)
+ closest_distance = d
+
+ return closest_distance, closest_factory
+
+def calculate_orders(whoami, state):
+ orders = []
+
+ my_robots = state.robots_by_player[whoami]
+ for rx, ry, energy in my_robots:
+ distance, factory = closest_factory(whoami, state, rx, ry)
+
+ if factory is None or random.random() < 0.1:
+ orders.append(random.choice('UDLRP'))
+ elif distance == 0:
+ orders.append('P')
+ else:
+ fx, fy = factory
+ orders.append(direction_to(rx, ry, fx, fy))
+
+ return orders
diff --git a/example-bots/pathing.py b/example-bots/pathing.py
new file mode 100644
index 0000000..d537940
--- /dev/null
+++ b/example-bots/pathing.py
@@ -0,0 +1,40 @@
+import random
+
+def move_toward_closest_factory(whoami, state, rx, ry):
+ if state[rx, ry].allegiance != whoami and state[rx, ry].city == '+':
+ return 'P'
+ to_do = [
+ (rx + 1, ry, 'R'),
+ (rx - 1, ry, 'L'),
+ (rx, ry - 1, 'U'),
+ (rx, ry + 1, 'D')
+ ]
+ random.shuffle(to_do)
+ seen = []
+ while to_do:
+ (x, y, move) = to_do.pop(0)
+ if (x, y) in seen or state[x, y].city == 'X':
+ continue
+ seen.append((x, y))
+ if state[x, y].allegiance != whoami and state[x, y].city == '+':
+ return move
+ else:
+ to_add = [(x + 1, y, move),
+ (x - 1, y, move),
+ (x, y + 1, move),
+ (x, y - 1, move)]
+ random.shuffle(to_add)
+ to_do += to_add
+
+def calculate_orders(whoami, state):
+ orders = []
+
+ my_robots = state.robots_by_player[whoami]
+ for rx, ry, energy in my_robots:
+ move = move_toward_closest_factory(whoami, state, rx, ry)
+ if move and random.random() > 0.1:
+ orders.append(move)
+ else:
+ orders.append(random.choice('UDLRP'))
+
+ return orders
diff --git a/example-bots/random.py b/example-bots/random.py
new file mode 100644
index 0000000..e61ff15
--- /dev/null
+++ b/example-bots/random.py
@@ -0,0 +1,11 @@
+import random
+
+def calculate_orders(whoami, state):
+ orders = []
+
+ my_robots = state.robots_by_player[whoami]
+ for robot in my_robots:
+ # Make a random move for each robot
+ orders.append(random.choice('ULDRP'))
+
+ return orders
diff --git a/example-bots/random_valid.py b/example-bots/random_valid.py
new file mode 100644
index 0000000..0a84b96
--- /dev/null
+++ b/example-bots/random_valid.py
@@ -0,0 +1,30 @@
+import random
+
+def calculate_orders(whoami, state):
+ orders = []
+
+ my_robots = state.robots_by_player[whoami]
+ for rx, ry, energy in my_robots:
+ if state[rx, ry].city == '+' and state[rx, ry].allegiance != whoami:
+ orders.append('P')
+ continue
+ potential_orders = []
+ if state[rx, ry].allegiance != whoami:
+ potential_orders.append('P')
+ if state[rx + 1, ry].city != 'X':
+ potential_orders.append('R')
+ if state[rx - 1, ry].city != 'X':
+ potential_orders.append('L')
+ if state[rx, ry + 1].city != 'X':
+ potential_orders.append('D')
+ if state[rx, ry - 1].city != 'X':
+ potential_orders.append('U')
+
+ if potential_orders:
+ # Make a random move for each robot
+ orders.append(random.choice(potential_orders))
+ else:
+ # There are no valid moves, so do nothing for this robot
+ orders.append('-')
+
+ return orders
diff --git a/example-bots/up_bot.py b/example-bots/up_bot.py
new file mode 100644
index 0000000..282d25b
--- /dev/null
+++ b/example-bots/up_bot.py
@@ -0,0 +1,11 @@
+def calculate_orders(whoami, state):
+ orders = []
+
+ # Get the robots you control.
+ my_robots = state.robots_by_player[whoami]
+
+ # Tell them all to go up.
+ for robot in my_robots:
+ orders.append('U')
+
+ return orders