示例#1
0
class Player:
    def __init__(self, name, race):
        self.race = race
        self.town = Town(race)
        self.gold = 1000
        self.hero = None
        self.name = name

    def hire_hero(self, name):
        self.hero = Hero(name)

    def upgrade_building(self, building):
        """ Accepts the building you want to upgrade """
        if building.price > self.gold:
            raise NoGold

        if not building.upgrade_available():
            raise BuildingAtMaxLevel

        self.gold -= building.price
        building.upgrade()

    def move_army_from_town_to_hero(self, unit_type, unit_count):
        if self.hero.is_in_town == False:
            raise HeroNotInTown

        if self.hero == None:
            raise NoHero

        if self.town.army[unit_type] < unit_count:
            raise NoUnits

        self.town.decrease_army(unit_type, unit_count)
        self.hero.increase_army(unit_type, unit_count)

    def move_army_from_hero_to_town(self, unit_type, unit_count):
        if self.hero == None:
            raise NoHero

        if self.hero.is_in_town == False:
            raise HeroNotInTown

        if self.hero.army[unit_type] < unit_count:
            raise NoUnits

        self.hero.decrease_army(unit_type, unit_count)
        self.town.increase_army(unit_type, unit_count)

    def train_army(self, unit_type, unit_count):
        if self.town.castle.units_available_to_train < unit_count:
            raise NoDailyUnits

        if ALL_RACE_UNITS[self.race][self.town.barracs.\
        army[unit_type]].price * unit_count > self.gold:
            raise NoGold

        self.town.army[unit_type] += unit_count

        self.gold -= ALL_RACE_UNITS[self.race][self.town.\
            barracs.army[unit_type]].price * unit_count
class TestTown(unittest.TestCase):
    def setUp(self):
        self.town = Town('elf')

    def test_town_buildings(self):
        self.assertIsInstance(self.town.wall, Wall)
        self.assertIsInstance(self.town.gold_mine, Gold_mine)
        self.assertIsInstance(self.town.castle, Castle)
        self.assertIsInstance(self.town.barracs, Barracs)

    def test_town_army(self):
        self.assertEqual(self.town.barracs.army, \
             ['archer','rogue', 'druid', 'assassin'])

    def test_increase_decrease_army(self):
        self.assertRaises(NoUnits, self.town.decrease_army, 1, 3)
        self.assertEqual([10, 0, 0, 0], self.town.army)
        self.town.increase_army(1, 20)
        self.assertEqual([10, 20, 0, 0], self.town.army)
        self.town.decrease_army(0, 5)
        self.assertEqual([5, 20, 0, 0], self.town.army)
        self.assertEqual([5, 20, 0, 0], self.town.army)
 def setUp(self):
     self.town = Town('elf')
示例#4
0
 def __init__(self, name, race):
     self.race = race
     self.town = Town(race)
     self.gold = 1000
     self.hero = None
     self.name = name