示例#1
0
    def setUp(self):
        #instantiate all the army
        self.fighter_solider = soldier()
        self.fighter_archer = archer()
        self.fighter_cavalry = cavalry()
        #get the price of each type of army
        self.cost_solider = self.fighter_solider.getCost()
        self.cost_archer = self.fighter_archer.getCost()
        self.cost_cavalry = self.fighter_cavalry.getCost()

        while True:
            # Here we need to validate if the input is not integer
            try:
                self.n_soldier = int(input("S: "))
                self.n_archer = int(input("A: "))
                self.n_cavalry = int(input("C: "))
            except:
                print("Please Enter valid input.")
                pass
            # Here we need to validate the total cost if it is greater than the budget
            try:
                print("")
                self.n_soldier = int(self.n_soldier)
                self.n_archer = int(self.n_archer)
                self.n_cavalry = int(self.n_cavalry)
                # to find out the total cost
                total_cost = (self.n_soldier * self.cost_solider + self.n_archer * self.cost_archer +
                          self.n_cavalry * self.cost_cavalry)
                # check if the inputs are positive number
                if self.n_soldier < 0 or self.n_archer < 0 or self.n_cavalry < 0:
                   raise IndexError
                # check if the total cost is greater than the budget
                elif total_cost > self.cost:
                    raise ValueError
                break
            except ValueError:
                
                print("Dude! You don't have enough money to build up the army.")
                
                
            except IndexError:
                
                print("You cannot buy negative number of military.")
                
                
        # push each army object on the stack
        if self.n_cavalry > 0:
            for i in range(int(self.n_cavalry)):
                # instantiate an cavalry object and push to the stack
                self.stack.push(cavalry())
        if self.n_archer > 0:
            for j in range(int(self.n_archer)):
                # instantiate an archer object and push to the stack
                self.stack.push(archer())
        if self.n_soldier > 0:
            for k in range(int(self.n_soldier)):
                # instantiate an soldier object and push to the stack
                self.stack.push(soldier())

        return self.stack
示例#2
0
 def test_defend_fail_soldier(self):
     damage = 1
     test_object = soldier()
     test_object.exp = 2
     test_object = test_object.defend(damage)
     self.assertEqual(0,test_object)
示例#3
0
 def test_defend_success_soldier(self):
     damage = 1
     test_object = soldier().defend(damage)
     self.assertEqual(1,test_object)
示例#4
0
 def test_getSpeed_2_soldier(self):
     test_object = soldier()
     test_object.exp = 3
     test_object = test_object.getSpeed()
     self.assertEqual(4,test_object)
示例#5
0
 def test_getSpeed_1_soldier(self):
     test_object = soldier().getSpeed()
     self.assertEqual(1,test_object)
示例#6
0
 def test_getCost_soldier(self):
     test_object = soldier().getCost()
     self.assertEqual(1,test_object)
示例#7
0
 def test_attack_2_soldier(self):
     test_object = soldier()
     test_object.exp = 2
     test_object = test_object.attack()
     self.assertEqual(3,test_object)
示例#8
0
 def test_attack_1_soldier(self):
     test_object = soldier().attack()
     self.assertEqual(1,test_object)
示例#9
0
 def test_getExp_soldier(self):
     test_object = soldier().gainExperience(3)
     self.assertEqual(3,test_object)
示例#10
0
 def test_lostLife_soldier(self):
     test_object = soldier().loseLive(2)
     self.assertEqual(1,test_object)
示例#11
0
 def test_isAlive_False_soldier(self):
     test_object = soldier()
     test_object.life = 0
     test_object = test_object.isAlive()
     self.assertFalse(test_object)
示例#12
0
 def test_isAlive_True_soldier(self):
     test_object = soldier().isAlive()
     self.assertTrue(test_object)
示例#13
0
 def test_initialise_exp_soldier(self):
     test_object = soldier().exp
     self.assertEqual(0,test_object)
示例#14
0
 def test_initialise_life_soldier(self):
     test_object = soldier().life
     self.assertEqual(3,test_object)
示例#15
0
            command = int(input("Please Enter your choice here: "))
            break
        except:
            print("Please enter valid input.")

    if command == 1:
        # here we do exception handling if the budget is not enough for buying an soldier
        while True:
            try:
                if money < 1:
                    raise ValueError
                break
            except ValueError:
                raise ValueError("Your Balance is less than 1")
        # create an object of soldier
        S1 = soldier()
        # After each purchasing, reduce the money from the wallet
        money -= S1.getCost()
        # append the army in the list, which consisted of your military.
        army_list.append(str("solider"))

    if command == 2:
        # here we do exception handling if the budget is not enough for buying an archer
        try:
            if money < 2:
                raise ValueError
        except ValueError:
            raise ValueError("Your Balance is less than 2")
        # create an object of archer
        A1 = archer()
        # after each purchasing, reduce the money from the wallet