class Battlefield: #TODO change it to BATTLEFIELD_RADIUS and make sure that battlefield coordinates can be smaller than 0 SIZE = 50 BATTLE_DURATION = 50 def __init__(self, attacker, defender): self._time = 0 self.attacker = Attacker(attacker, self.time(), Battlefield.SIZE) self.defender = Defender(defender, self.time(), 0) def time(self): return self._time def process(self): """process a single "tick" of the battle""" self.attacker.process(self) self.defender.process(self) def start(self): result = {} try: while self.time() < Battlefield.BATTLE_DURATION: self.process() self._time += 1 except BattleOver as x: result['loser'] = x.loser result['time'] = self._time return result
def test_move_attacker(self): current_time = 0 unit = Attacker(Ship(), current_time, Battlefield.SIZE) target = Battlefield.SIZE - (unit.speed() * 6) unit.move_to(target, current_time) self.assertEqual(unit.position, Battlefield.SIZE) current_time += 3 # 3 seconds unit.move_to(target, current_time) self.assertEqual(unit.position, Battlefield.SIZE - (unit.speed() * current_time)) current_time += 10 # 13 seconds unit.move_to(target, current_time) self.assertEqual(unit.position, target)
def __init__(self, attacker, defender): self._time = 0 self.attacker = Attacker(attacker, self.time(), Battlefield.SIZE) self.defender = Defender(defender, self.time(), 0)