def test_unteam_player_undeployed(self): unit = Soldier() unit_2 = Soldier() self.team.add_player(unit) self.team.add_player(unit_2) self.team.unteam_player(unit) self.assertEqual(self.team.undeployed, [unit_2])
def test_allies_in_range_melee_weapon(self): points_to_elevation = { Point(1, 0): -3, Point(0, 1): 4, Point(1, 1): 0, Point(2, 1): -4, Point(1, 2): 3 } tiles = [ Tile(elevation=elevation, point=point) for point, elevation in points_to_elevation.items() ] the_map = Map(3, 3, tiles) origin, below, above = self.a_units far_below = Soldier() far_above = Soldier() self.team_a.add_player(far_above) self.team_a.add_player(far_below) origin.equip_weapon(MeleeWeapon(10, 10, 10, 10)) the_map.place_unit(origin, Point(1, 1)) the_map.place_unit(below, Point(1, 0)) the_map.place_unit(above, Point(1, 2)) the_map.place_unit(far_above, Point(0, 1)) the_map.place_unit(far_below, Point(2, 1)) tf = TargetFinder(the_map, [self.team_a, self.team_b]) answer = tf.allies_in_range(origin) expected = {below: (1, 1), above: (1, -1)} self.assertEqual(answer, expected)
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_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_spawn(self): units = [Soldier(), Soldier(), Soldier()] for soldier in units: self.team.add_player(soldier) for _ in range(3): self.team.spawn() self.assertEqual(self.map.get_unit(Point(1, 0)), units[0]) self.assertEqual(self.map.get_unit(Point(0, 1)), units[1]) self.assertEqual(self.map.get_unit(Point(2, 1)), units[2]) self.assertRaises(ValueError, self.team.spawn)
def test_spawn_with_obstacles_at_distance_one(self): obstacles = self.team.home.at_distance(1) for point in obstacles: self.map.place_unit(Soldier(), point) units = [Soldier() for _ in range(3)] for soldier in units: self.team.add_player(soldier) for _ in range(len(units)): self.team.spawn() expected_points = [Point(0, 0), Point(2, 0), Point(0, 2)] for soldier, point in zip(units, expected_points): self.assertEqual(self.map.get_unit(point), soldier)
def setUp(self): self.map_length = 20 self.map = Map(self.map_length, self.map_length, [Tile() for _ in range(self.map_length**2)]) self.team_a = Team(Point(self.map_length - 1, 0), self.map) self.team_b = Team(Point(0, self.map_length - 1), self.map) team_size = 3 self.a_units = [Soldier() for _ in range(team_size)] self.b_units = [Soldier() for _ in range(team_size)] for a_unit, b_unit in zip(self.a_units, self.b_units): self.team_a.add_player(a_unit) self.team_b.add_player(b_unit)
def test_spawn_no_room_on_map(self): units = [Soldier() for _ in range(10)] for soldier in units: self.team.add_player(soldier) for _ in range(8): self.team.spawn() self.assertRaises(ValueError, self.team.spawn)
def test_get_unit(self): unit = Soldier() point = Point(1, 1) self.map.place_unit(unit, point) self.assertIs(unit, self.map.get_unit(point)) self.assertIsNone(self.map.get_unit(Point(0, 0)))
def do_actions(self, unit: Soldier): action_list = self.get_action_list(unit) while unit.get_action_points(): for action in action_list: actionator = Actionator(unit, action, self._pm, self._map, [self._team_1, self._team_2]) actionator.go()
def test_spawn_uses_undeployed_as_a_queue(self): units = [Soldier(), Soldier(), Soldier()] for soldier in units: self.team.add_player(soldier) self.assertEqual(self.team.undeployed, units) self.team.spawn() self.assertEqual(self.team.undeployed, units[1:]) self.assertEqual(self.team.deployed, units[0:1]) self.team.spawn() self.assertEqual(self.team.undeployed, units[2:]) self.assertEqual(self.team.deployed, units[0:2]) self.team.spawn() self.assertEqual(self.team.undeployed, []) self.assertEqual(self.team.deployed, units)
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_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_get_movement_points_not_affected_by_unit_on_origin(self): map_ = Map(2, 2, [Tile() for _ in range(4)]) origin = Point(0, 0) map_.place_unit(Soldier(), origin) answer = MovementCalculator(map_).get_movement_points(origin, 100) expected = { Point(0, 0): 0, Point(0, 1): 1, Point(1, 0): 1, Point(1, 1): 2 } self.assertEqual(answer, expected)
def test_get_movement_points_with_path_with_occupied_space_in_place(self): elevations = { Point(0, 0): 2, Point(1, 0): 0, Point(2, 0): 3, Point(0, 1): 1, Point(1, 1): 0, Point(2, 1): 2, Point(0, 2): 2, Point(1, 2): 0, Point(2, 2): 1 } tiles = [ Tile(point=point, elevation=elevation) for point, elevation in elevations.items() ] map_ = Map(3, 3, tiles) occupied_point = Point(1, 1) origin = Point(1, 2) map_.place_unit(Soldier(), origin) map_.place_unit(Soldier(), occupied_point) expected = { Point(0, 0): (6, [W, S, S]), Point(1, 0): (7, [W, S, S, E]), Point(2, 0): (6, [E, S, S]), Point(0, 1): (4, [W, S]), Point(2, 1): (4, [E, S]), Point(0, 2): (3, [W]), Point(1, 2): (0, []), Point(2, 2): (2, [E]) } self.assertEqual( MovementCalculator(map_).get_movement_points_with_path(origin, 10), expected)
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 }
def units_in_sight(self, unit: Soldier): """ :param unit: :return: units_to_distances """ sight_range = unit.get_sight_range() origin = self._map.get_point(unit) distance_to_points = self._rf.get_sight_ranges_units_only( origin, sight_range) del distance_to_points[0] return { self._map.get_unit(point): distance for distance, points in distance_to_points.items() for point in points }
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 setUp(self): self.soldier = Soldier()
def test_unteam_player_not_in_team(self): unit = Soldier() self.assertRaises(ValueError, self.team.unteam_player, unit)
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'))
def test_place_unit_and_then_spawn_raises_error(self): unit = Soldier() self.team.add_player(unit) self.map.place_unit(unit, Point(1, 0)) self.assertRaises(MapPlacementError, self.team.spawn)
def test_add_player_dupe(self): unit = Soldier() self.team.add_player(unit) self.team.add_player(unit) self.assertEqual(self.team.undeployed, [unit])
def test_get_sight_rangs(self): unit = Soldier() self.assertEqual(unit.get_sight_range(), 10)
def test_is_on_team_false(self): unit = Soldier() self.team.add_player(unit) self.assertFalse(self.team.is_on_team(Soldier()))
def test_spawn_return_value(self): unit = Soldier() self.team.add_player(unit) self.assertEqual(self.team.spawn(), (unit, Point(1, 0)))
def test_add_player_multiple_players(self): unit = Soldier() unit_2 = Soldier() self.team.add_player(unit) self.team.add_player(unit_2) self.assertEqual(self.team.undeployed, [unit, unit_2])
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_is_on_team_true(self): unit = Soldier() self.team.add_player(unit) self.assertTrue(self.team.is_on_team(unit)) self.team.spawn() self.assertTrue(self.team.is_on_team(unit))