Question
Task 3 Add to the army.py file the class Army, which: Contains as variables the name (name) of the army's player together with an ADT
Task 3
Add to the army.py file the class Army, which:
- Contains as variables the name (name) of the army's player together with an ADT containing the unit purchases by the player (force).
- Implements method __correct_army_given(self, soldiers: int, archers: int, cavalry: int) -> bool, which returns True if the total costs of the units provided are less than or equal the allocated budget, and False otherwise.
- Implements method __assign_army(self, name:str, sold: int, arch: int, cav: int, formation: int) -> None, which:
- Sets formation of an army to either stack or queue form.
- Creates the ADT in accordance with formation and adds the units to it appropriately.
- Binds the name and force variables.
- Defines method choose_army(self, name: str, formation: int) -> None which reads in 3 integers, s, a, c (number of soldiers, archers, and cavalry wanted). With these numbers it then calls:
- __correct_army_given(s, a, c)
- and if this returns True:
- __assign_army(name, s, a, c, formation)
- Otherwise the player is repeatedly asked to provide the input again, with an identical message, until valid.
- Defines __str__, which returns a string containing the information of each army element in force. For example, if an army is in stack formation with 1 Soldier on top of 1 Cavalry, both with life 1 and experience 2, then str(army) should return "Soldier's life = 1 and experience = 2,Cavalry's life = 1 and experience = 2".
Testing
Add extra tests to test_task3.py to test the __correct_army_given and __str__ (which itself serves as test for __assign_army) methods. Also, test your choose_army using manual inputs.
Constraints and assumptions
- Assessment-wide constraints and assumptions.
- The __init__(self) -> None method should initialise name and force to None.
- The budget for this task is 30 "money".
- The ArrayStack class will need to be edited for __str__.
- Note that if the army is empty, __str__ should simply return the empty string.
- For this Task, assume formation will only ever be 0 (stack only). In a future task it can also be 1 (queue).
The method choose_army , given the name of a player (say "Peter") received as input, should display the following message:
Player Peter choose your army as S A C
where S is the number of soldiers
A is the number of archers
C is the number of cavalries
army.py file:
from abc import ABC, abstractmethod
@abstractmethod
class Fighter():
experience = 0
damage = 0
life = 0
unit_type = "Archer"
def __init__(self, life: int, experience: int) -> None:
assert life>=0 and experience>= 0
if life>=0:
self.life=life
else:
raise ValueError("The life must be greater or equal to zero")
if experience>=0:
self.experience=experience
else:
raise ValueError("The experience must be greater or equal to zero")
def is_alive(self) -> bool:
return self.life>=0
def lose_life(self, lost_life: int) -> None:
assert lose_life>=0
if lost_life>=0:
self.life -= lost_life
else:
raise ValueError("The lost life must be positive")
def get_life(self) -> int:
return self.life
def gain_experience(self, gained_experience: int) -> None:
assert gained_experience>=0
if gained_experience>=0:
self.experience += gained_experience
else:
raise ValueError("The gained experience must be greater or equal to zero")
def get_experience(self) -> int:
return self.experience
@abstractmethod
def get_speed(self) -> int:
return self.speed
@abstractmethod
def get_cost(self) -> int:
return self.cost
def get_attack_damage(self) -> int:
return self.attack_damage
def defend(self, damage: int) -> None:
assert defend>=0
if damage>=0:
self.life -= damage
else:
raise ValueError("The defence must be positive")
def get_unit_type(self) -> str:
return self.unit_type;
def __str__(self) -> str:
val= self.unit_type+"'s life ="+str(self.life)+" experience = "+str(self.experience)+" "
return val
class Soldier(Fighter):
def __init__(self, cost, life, experience):
Fighter.__init__(life, experience)
self.speed = 1 + self.experience
self.damage = 1 + self.experience
self.cost = cost
self.unit_type = "Soldier"
class Archer(Fighter):
def __init__(self, cost, life, experience):
Fighter.__init__(life, experience)
self.speed = 1 + self.experience
self.damage = 1 + self.experience
self.cost = cost
self.unit_type = "Archer"
class Cavalry(Fighter):
def __init__(self, cost, life, experience):
Fighter.__init__(life, experience)
self.speed = 1 + self.experience
self.damage = 2 * self.experience+1
self.cost = cost
self.unit_type = "Cavalry"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started