From e62702bfa94ed70b8102006e63eb39805e5e8c89 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Mon, 7 Apr 2014 17:52:56 +1000 Subject: factories produce robots This is kind of broken until combat is implemented. --- robots/game.py | 18 ++++++++++++++++++ robots/state.py | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/robots/game.py b/robots/game.py index 733068c..827562a 100644 --- a/robots/game.py +++ b/robots/game.py @@ -1,6 +1,7 @@ from collections import defaultdict, deque from contextlib import contextmanager from copy import deepcopy +from random import random from robots.constants import Action, City, DIRECTIONS, Energy, ENERGY_BY_ACTION from robots.state import GameState @@ -144,6 +145,22 @@ class Game: for name, robots in self.state.robots_by_player.items(): robots[:] = robots_by_player[name] + def factories_produce_robots(self): + state_robots = self.state.robots + + for x, y in self.state.factories: + if state_robots[(x, y)]: + continue + + owner = self.state.allegiances.get((x, y)) + if owner is None: + continue + + if random() < 0.2: + self.state.robots_by_player[owner].append( + (x, y, Energy.INITIAL) + ) + def robots_gain_energy(self): for robots in self.state.robots_by_player.values(): robots[:] = [ @@ -163,6 +180,7 @@ class Game: # 2. They perform the requested commands. self.apply_actions(actions) # 3. Factories produce new robots. + self.factories_produce_robots() # 4. Combats are resolved. # 5. Each robot gains energy, up to the maximum. self.robots_gain_energy() diff --git a/robots/state.py b/robots/state.py index 1fe9c26..5a1450d 100644 --- a/robots/state.py +++ b/robots/state.py @@ -75,6 +75,15 @@ class GameState: def height(self): return len(self.cities) + @property + def factories(self): + return { + (x, y) + for y, row in enumerate(self.cities) + for x, city in enumerate(row) + if city == City.FACTORY + } + @property def n_alive_players(self): """How many players are still alive.""" -- cgit v1.2.3