Example #1
0
    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)
Example #2
0
    def test_allies_in_range_no_allies(self):
        all_units = self.a_units[0:1] + self.b_units
        for index, unit in enumerate(all_units):
            self.map.place_unit(unit, Point(index, index))
        test_unit = all_units[0]
        test_unit.equip_weapon(RangedWeapon(10, 10, 10, 10))

        teams = [self.team_a, self.team_b]
        test = TargetFinder(self.map, teams)
        answer = test.allies_in_range(test_unit)
        self.assertEqual(answer, {})
Example #3
0
    def test_allies_in_range_answer_excludes_non_allies(self):
        all_units = self.a_units + self.b_units
        for index, unit in enumerate(all_units):
            self.map.place_unit(unit, Point(index, index))
        test_unit = all_units[0]
        test_unit.equip_weapon(RangedWeapon(10, 10, 10, 10))

        teams = [self.team_a, self.team_b]
        test = TargetFinder(self.map, teams)
        answer = test.allies_in_range(test_unit)
        expected = {self.a_units[1]: (2, 0), self.a_units[2]: (4, 0)}
        self.assertEqual(answer, expected)
Example #4
0
    def test_allies_in_range_by_weapon_range(self):
        origin, in_range, out_of_range = self.a_units
        for weapon_range in range(1, 5):
            self.map.remove_all_units()
            self.map.place_unit(origin, Point(0, 0))

            self.map.place_unit(in_range, Point(weapon_range, 0))
            self.map.place_unit(out_of_range, Point(weapon_range + 1, 0))
            tf = TargetFinder(self.map, [self.team_a, self.team_b])
            origin.equip_weapon(RangedWeapon(1, 1, weapon_range, 1))

            answer = tf.allies_in_range(origin)
            expected = {
                in_range: (weapon_range, 0),
            }
            self.assertEqual(answer, expected)
Example #5
0
    def test_allies_in_range_advantage(self):
        points_to_elevation = {Point(0, 0): 0, Point(1, 0): -1, Point(2, 0): 1}
        tiles = [
            Tile(elevation=elevation, point=point)
            for point, elevation in points_to_elevation.items()
        ]
        the_map = Map(3, 1, tiles)

        origin, below, above = self.a_units
        origin.equip_weapon(RangedWeapon(10, 10, 10, 10))

        the_map.place_unit(origin, Point(0, 0))
        the_map.place_unit(below, Point(1, 0))
        the_map.place_unit(above, Point(2, 0))

        tf = TargetFinder(the_map, [self.team_a, self.team_b])

        answer = tf.allies_in_range(origin)
        expected = {below: (1, 1), above: (2, -1)}
        self.assertEqual(answer, expected)
Example #6
0
    def test_enemies_in_range_by_weapon_range(self):
        origin = self.a_units[0]
        in_range, out_of_range = self.b_units[0:2]
        self.map.place_unit(origin, Point(0, 0))
        self.map.place_unit(in_range, Point(10, 0))
        self.map.place_unit(out_of_range, Point(11, 0))
        origin.equip_weapon(RangedWeapon(10, 10, 10, 10))
        tf = TargetFinder(self.map, [self.team_a, self.team_b])

        answer = tf.enemies_in_range(origin)
        expected = {
            in_range: (10, 0),
        }
        self.assertEqual(answer, expected)

        self.map.remove_unit(Point(10, 0))
        self.map.remove_unit(Point(11, 0))
        self.map.place_unit(in_range, Point(5, 5))
        self.map.place_unit(out_of_range, Point(5, 6))
        answer = tf.enemies_in_range(origin)
        self.assertEqual(answer, expected)
Example #7
0
    def test_enemies_in_range_by_obstructed_view(self):
        points_to_elevation = {Point(0, 0): 0, Point(1, 0): 1, Point(2, 0): 0}
        tiles = [
            Tile(elevation=elevation, point=point)
            for point, elevation in points_to_elevation.items()
        ]
        the_map = Map(3, 1, tiles)

        origin = self.a_units[0]
        in_range, out_of_range = self.b_units[0:2]
        the_map.place_unit(origin, Point(0, 0))
        the_map.place_unit(in_range, Point(1, 0))
        the_map.place_unit(out_of_range, Point(2, 0))
        origin.equip_weapon(RangedWeapon(10, 10, 1, 1))

        tf = TargetFinder(the_map, [self.team_a, self.team_b])

        answer = tf.enemies_in_range(origin)
        expected = {
            in_range: (1, -1),
        }
        self.assertEqual(answer, expected)
Example #8
0
 def test_is_melee_weapon(self):
     melee = MeleeWeapon(1, 2, 3)
     ranged = RangedWeapon(2, 3, 4, 5)
     self.assertTrue(melee.is_melee_weapon())
     self.assertFalse(ranged.is_melee_weapon())
Example #9
0
 def test_ranged_init(self):
     new = RangedWeapon(2, 3, 4, 5)
     self.assertEqual(new.dmg, 2)
     self.assertEqual(new.action_pts, 3)
     self.assertEqual(new.range, 4)
     self.assertEqual(new.ammo, 5)
Example #10
0
 def setUp(self):
     self.ranged = RangedWeapon(dmg=3, action_pts=4, range_=5, ammo=5)
     self.melee = MeleeWeapon(dmg=4, action_pts=2)
Example #11
0
class TestWeapon(unittest.TestCase):
    def setUp(self):
        self.ranged = RangedWeapon(dmg=3, action_pts=4, range_=5, ammo=5)
        self.melee = MeleeWeapon(dmg=4, action_pts=2)

    def test_init_and_getters(self):
        new = Weapon(dmg=2, action_pts=3, range_=4, ammo=5)
        self.assertEqual(new.ammo, 5)
        self.assertEqual(new.max_ammo, 5)
        self.assertEqual(new.dmg, 2)
        self.assertEqual(new.action_pts, 3)
        self.assertEqual(new.range, 4)

    def test_use_weapon_inf_ammo(self):
        dmg = self.melee.use_weapon()
        self.assertEqual(dmg, 4)
        self.assertEqual(self.melee.ammo, float('inf'))

    def test_use_weapon_non_inf_ammo(self):
        self.assertEqual(self.ranged.ammo, 5)
        self.assertEqual(self.ranged.use_weapon(), 3)
        self.assertEqual(self.ranged.ammo, 4)

    def test_use_weapon_no_ammo(self):
        for _ in range(5):
            self.ranged.use_weapon()

        self.assertRaises(OutOfAmmo, self.ranged.use_weapon)

    def test_refill_ammo_inf(self):
        self.melee.use_weapon()
        self.melee.refill_ammo()
        self.assertEqual(self.melee.ammo, float('inf'))

    def test_refill_ammo_not_inf(self):
        self.ranged.use_weapon()
        self.ranged.refill_ammo()
        self.assertEqual(self.ranged.ammo, 5)

        for _ in range(5):
            self.ranged.use_weapon()

        self.ranged.refill_ammo()
        self.assertEqual(self.ranged.ammo, 5)

    def test_melee_init_defaults(self):
        new = MeleeWeapon(2, 3)
        self.assertEqual(new.dmg, 2)
        self.assertEqual(new.action_pts, 3)
        self.assertEqual(new.range, 1)
        self.assertEqual(new.ammo, float('inf'))

    def test_melee_init_no_defaults(self):
        new = MeleeWeapon(2, 3, 4, 5)
        self.assertEqual(new.dmg, 2)
        self.assertEqual(new.action_pts, 3)
        self.assertEqual(new.range, 4)
        self.assertEqual(new.ammo, 5)

    def test_ranged_init(self):
        new = RangedWeapon(2, 3, 4, 5)
        self.assertEqual(new.dmg, 2)
        self.assertEqual(new.action_pts, 3)
        self.assertEqual(new.range, 4)
        self.assertEqual(new.ammo, 5)

    def test_is_melee_weapon(self):
        melee = MeleeWeapon(1, 2, 3)
        ranged = RangedWeapon(2, 3, 4, 5)
        self.assertTrue(melee.is_melee_weapon())
        self.assertFalse(ranged.is_melee_weapon())
Example #12
0
from typing import Union

from battle.players.strategy import StupidStrategy
from battle.statstools.stat import PositiveStat
from battle.weapon import MeleeWeapon, RangedWeapon, Weapon, OutOfAmmo

FIST = MeleeWeapon(dmg=1, action_pts=1)
GUN = RangedWeapon(dmg=5, action_pts=2, range_=5, ammo=10)


class Soldier(object):
    def __init__(self,
                 action_pts: int = 3,
                 health: Union[float, int] = 100,
                 heal_pct: float = 5.0):
        self._strategy = StupidStrategy()
        self._action_pts = PositiveStat(action_pts)
        self._health = PositiveStat(health)

        self._healing_pct = heal_pct

        self._weapon = FIST

    @property
    def strategy(self):
        return self._strategy

    def get_perimeter_size(self) -> int:
        return self._weapon.range

    def get_sight_range(self) -> int: