def units_in_range(self, unit: Soldier): weapon = unit.get_weapon() weapon_range = weapon.range origin = self._map.get_point(unit) if weapon.is_melee_weapon(): range_method = self._rf.get_attack_ranges_melee_units_only else: range_method = self._rf.get_attack_ranges_ranged_units_only distance_to_point_advantage = range_method(origin, weapon_range) del distance_to_point_advantage[0] return { self._map.get_unit(point_advantage[0]): (distance, point_advantage[1]) for distance, point_advantage_list in distance_to_point_advantage.items() for point_advantage in point_advantage_list }
class TestSoldier(unittest.TestCase): def setUp(self): self.soldier = Soldier() def test_init_default_settings(self): self.assertIs(self.soldier.get_weapon(), FIST) self.assertEqual(self.soldier.get_health(), 100) self.assertEqual(self.soldier.get_action_points(), 3) self.assertEqual(self.soldier._healing_pct, 5.0) def test_init_set_parameters(self): unit = Soldier(5, 200, 10.0) self.assertEqual(unit.get_health(), 200) self.assertEqual(unit.get_action_points(), 5) self.assertEqual(unit._healing_pct, 10.0) def test_get_perimeter_size(self): unit = Soldier() self.assertEqual(unit.get_perimeter_size(), 1) unit.equip_weapon(GUN) self.assertEqual(unit.get_perimeter_size(), 5) def test_get_sight_rangs(self): unit = Soldier() self.assertEqual(unit.get_sight_range(), 10) def test_equip_weapon(self): stick = MeleeWeapon(3, 2) self.soldier.equip_weapon(stick) self.assertEqual(self.soldier.get_weapon(), stick) def test_receive_dmg(self): self.soldier.receive_dmg(99) self.assertEqual(self.soldier.get_health(), 1) self.soldier.receive_dmg(2) self.assertEqual(self.soldier.get_health(), 0) self.soldier.receive_dmg(0) self.assertEqual(self.soldier.get_health(), 0) def test_receive_dmg_negative_number_raises_value_error(self): self.assertRaises(ValueError, self.soldier.receive_dmg, -10) def test_attack(self): opponent = Soldier() self.assertEqual(self.soldier.get_weapon().dmg, 1) self.soldier.attack(opponent) self.assertEqual(opponent.get_health(), 99) self.soldier.attack(opponent) self.assertEqual(opponent.get_health(), 98) def test_attack_with_different_weapon(self): opponent = Soldier() new_weapon = MeleeWeapon(10, 1) self.soldier.equip_weapon(new_weapon) self.assertEqual(self.soldier.get_weapon().dmg, 10) self.soldier.attack(opponent) self.assertEqual(opponent.get_health(), 90) self.soldier.attack(opponent) self.assertEqual(opponent.get_health(), 80) def test_attack_uses_ammo_and_action_points(self): opponent = Soldier() self.soldier.equip_weapon(GUN) self.assertEqual(self.soldier.get_weapon().ammo, 10) self.assertEqual(self.soldier.get_weapon().action_pts, 2) self.assertEqual(self.soldier.get_action_points(), 3) self.soldier.attack(opponent) self.assertEqual(self.soldier.get_weapon().ammo, 9) self.assertEqual(self.soldier.get_action_points(), 1) def test_attack_out_of_ammo_reloads_and_uses_action_points(self): soldier = Soldier(action_pts=10) weapon = MeleeWeapon(5, 1, 1, 2) soldier.equip_weapon(weapon) for _ in range(2): soldier.attack(self.soldier) self.assertEqual(weapon.ammo, 0) self.assertEqual(self.soldier.get_health(), 90) self.assertEqual(soldier.get_action_points(), 8) soldier.attack(self.soldier) self.assertEqual(weapon.ammo, 2) self.assertEqual(self.soldier.get_health(), 90) self.assertEqual(soldier.get_action_points(), 7) def test_is_dead_true(self): self.soldier.receive_dmg(self.soldier.get_health()) self.assertTrue(self.soldier.is_dead()) self.soldier.receive_dmg(1) self.assertTrue(self.soldier.is_dead()) def test_is_dead_false(self): self.assertFalse(self.soldier.is_dead()) self.soldier.receive_dmg(self.soldier.get_health() - 1) self.assertFalse(self.soldier.is_dead()) def test_heal_lt_max_health(self): self.soldier.receive_dmg(2) self.soldier.heal(0) self.soldier.heal(1) self.assertEqual(self.soldier.get_health(), 99) def test_heal_eq_max_health(self): self.soldier.receive_dmg(2) self.soldier.heal(2) self.assertEqual(self.soldier.get_health(), 100) def test_heal_gt_max_health(self): self.soldier.receive_dmg(2) self.soldier.heal(3) self.assertEqual(self.soldier.get_health(), 100) def test_heal_dead_person(self): self.soldier.receive_dmg(120) self.soldier.heal(100) self.assertEqual(self.soldier.get_health(), 0) def test_heal_neg_health(self): self.assertRaises(ValueError, self.soldier.heal, -1) def test_can_act(self): self.assertTrue(self.soldier.can_act(3)) self.assertTrue(self.soldier.can_act(0)) self.assertFalse(self.soldier.can_act(4)) def test_can_act_dead(self): self.soldier.receive_dmg(1000) self.assertFalse(self.soldier.can_act(0)) def test_move(self): self.soldier.move(2) self.assertEqual(self.soldier.get_action_points(), 1) self.soldier.reset_move_points() def test_reset_move(self): self.soldier.move(2) self.soldier.reset_move_points() self.assertEqual(self.soldier.get_action_points(), 3) def test_rest_alive(self): self.soldier.move(2) self.soldier.receive_dmg(6) self.soldier.rest() self.assertEqual(self.soldier.get_action_points(), 3) self.assertEqual(self.soldier.get_health(), 99) def test_rest_dead(self): self.soldier.move(2) self.soldier.receive_dmg(1000) self.soldier.rest() self.assertEqual(self.soldier.get_action_points(), 3) self.assertEqual(self.soldier.get_health(), 0) def test_Base(self): base = Base() self.assertEqual(base.get_action_points(), 0) self.assertEqual(base.get_health(), float('inf'))
class TestPerimeterListener(TestCase): def setUp(self): self.gun = RangedWeapon(2, 2, 2, 2) self.melee = Soldier() self.melee.equip_weapon(FIST) self.ranged = Soldier() self.ranged.equip_weapon(self.gun) def test_set_up(self): self.assertEqual(self.gun.range, 2) self.assertIsInstance(self.gun, RangedWeapon) self.assertEqual(FIST.range, 1) self.assertIsInstance(FIST, MeleeWeapon) self.assertEqual(self.melee.get_weapon(), FIST) self.assertEqual(self.melee.get_perimeter_size(), 1) self.assertEqual(self.ranged.get_weapon(), self.gun) self.assertEqual(self.ranged.get_perimeter_size(), 2) def test_init_creates_empty_table_with_unique_sets(self): points = [ Point(0, 0), Point(1, 0), Point(2, 0), Point(0, 1), Point(1, 1), Point(2, 1) ] tiles = [Tile(point=pt) for pt in points] map_ = Map(3, 2, tiles) listener = PerimeterListener(map_) expected = {pt: set() for pt in points} self.assertEqual(listener._watchers_at_point, expected) listener._watchers_at_point[Point(0, 0)].add(1) self.assertEqual(listener._watchers_at_point[Point(1, 0)], set()) def test_set_perimeter_melee(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) origin = Point(1, 1) expected_ranges = { 0: [(Point(1, 1), 0)], 1: [(Point(0, 1), 0), (Point(2, 1), -1), (Point(1, 2), -1)] } self.assertEqual( RangeFinder(map_=the_map).get_attack_ranges_melee(origin, range_=1), expected_ranges) listener = PerimeterListener(the_map) listener.set_perimeter(self.melee, origin) expected = {key: set() for key in points_to_elevation} expected[Point(0, 1)] = {self.melee} expected[Point(2, 1)] = {self.melee} expected[Point(1, 2)] = {self.melee} self.assertEqual(listener._watchers_at_point, expected) def test_set_perimeter_ranged(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) origin = Point(0, 0) expected_ranges = { 0: [(Point(0, 0), 0)], 1: [(Point(1, 0), -1), (Point(0, 1), -1)], 2: [(Point(1, 1), -1), (Point(0, 2), -1)] } self.assertEqual( RangeFinder(map_=the_map).get_attack_ranges_ranged(origin, range_=2), expected_ranges) listener = PerimeterListener(the_map) listener.set_perimeter(self.ranged, origin) expected = {key: set() for key in points_to_elevation} expected[Point(0, 1)] = {self.ranged} expected[Point(1, 0)] = {self.ranged} expected[Point(0, 2)] = {self.ranged} expected[Point(1, 1)] = {self.ranged} self.assertEqual(listener._watchers_at_point, expected) def test_set_perimeter_two_units(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) listener = PerimeterListener(the_map) melee_pt = Point(1, 1) ranged_pt = Point(0, 0) listener.set_perimeter(self.melee, melee_pt) listener.set_perimeter(self.ranged, ranged_pt) expected = {key: set() for key in points_to_elevation} expected[Point(0, 1)] = {self.ranged, self.melee} expected[Point(1, 0)] = {self.ranged} expected[Point(0, 2)] = {self.ranged} expected[Point(1, 1)] = {self.ranged} expected[Point(2, 1)] = {self.melee} expected[Point(1, 2)] = {self.melee} self.assertEqual(listener._watchers_at_point, expected) def test_get_attackers(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) listener = PerimeterListener(the_map) melee_pt = Point(1, 1) ranged_pt = Point(0, 0) listener.set_perimeter(self.melee, melee_pt) listener.set_perimeter(self.ranged, ranged_pt) self.assertEqual(listener.get_attackers(Point(2, 2)), set()) self.assertEqual(listener.get_attackers(Point(0, 1)), {self.ranged, self.melee}) self.assertEqual(listener.get_attackers(Point(0, 2)), {self.ranged}) self.assertEqual(listener.get_attackers(Point(1, 2)), {self.melee}) def test_rm_perimeter_one_unit(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) listener = PerimeterListener(the_map) melee_pt = Point(1, 1) listener.set_perimeter(self.melee, melee_pt) empty = {pt: set() for pt in points_to_elevation} non_empty = {pt: set() for pt in points_to_elevation} non_empty[Point(0, 1)] = {self.melee} non_empty[Point(2, 1)] = {self.melee} non_empty[Point(1, 2)] = {self.melee} self.assertEqual(listener._watchers_at_point, non_empty) listener.rm_perimeter(self.melee) self.assertEqual(listener._watchers_at_point, empty) def test_rm_perimeter_two_units(self): points_to_elevation = { Point(0, 0): 0, Point(1, 0): 5, Point(2, 0): 0, Point(0, 1): 1, Point(1, 1): 1, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 3, Point(2, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) listener = PerimeterListener(the_map) melee_pt = Point(1, 1) ranged_pt = Point(0, 0) listener.set_perimeter(self.melee, melee_pt) listener.set_perimeter(self.ranged, ranged_pt) with_ranged = {pt: set() for pt in points_to_elevation} with_ranged[Point(0, 1)] = {self.ranged, self.melee} with_ranged[Point(1, 0)] = {self.ranged} with_ranged[Point(0, 2)] = {self.ranged} with_ranged[Point(1, 1)] = {self.ranged} with_ranged[Point(2, 1)] = {self.melee} with_ranged[Point(1, 2)] = {self.melee} no_ranged = {pt: set() for pt in points_to_elevation} no_ranged[Point(0, 1)] = {self.melee} no_ranged[Point(2, 1)] = {self.melee} no_ranged[Point(1, 2)] = {self.melee} self.assertEqual(listener._watchers_at_point, with_ranged) listener.rm_perimeter(self.ranged) self.assertEqual(listener._watchers_at_point, no_ranged)