The Vikings and the Saxons are at War. Both are Soldiers but they have their own methods to fight. Vikings are ported to Python. YAY!!
In this laboratory you will work with the concept of inheritance in Python.
You will find the following files in the folder of this laboratory:
vikingsClasses.py1-testSoldier.py2-testVikings.py3-testSaxons.py4-testWar.py
You are free to use any of the code editors you have to open these files.
Modify the file vikingsClasses.py so that all the tests are correct.
- Modify
vikingsClasses.pyand save your changes.
Best way to know how our code is doing is to work with tests. You will test the vikingsClases.py file step by step.
You will only be editing the vikingsClasses.py. The files you will running to test your code are the following: 1-testsSoldier.py, 2-testsVikings.py, 3-testsSaxons.py & 4-testsWar.py, depending on how far you have written your code.
So, let's say you have already created the class for Soldiers.
- You wrote your code
- Make sure you save the changes in your editor
- In your terminal, run the test file for that class
$ python3 1-testsSoldier.py --vWhen the tests are all correct you will receive the following message in the terminal.
$ python3 1-testsSoldier.py --v
testAttackHasNoParams (__main__.TestSoldier) ... ok
testAttackRetunsStrength (__main__.TestSoldier) ... ok
testAttackShouldBeFunction (__main__.TestSoldier) ... ok
testCanReceiveDamage (__main__.TestSoldier) ... ok
testConstructorSignature (__main__.TestSoldier) ... ok
testHealth (__main__.TestSoldier) ... ok
testReceiveDamageReturnNone (__main__.TestSoldier) ... ok
testReceivesDamage (__main__.TestSoldier) ... ok
testReceivesDamageHasParams (__main__.TestSoldier) ... ok
testStrength (__main__.TestSoldier) ... ok
----------------------------------------------------------------------
Ran 10 tests in 0.001s
OK
When any test is incorrect you will receive the following message in the terminal. It means that you must keep making changes in the vikingsClasses.py file.
$ python3 1-testsSoldier.py --v
testAttackHasNoParams (__main__.TestSoldier) ... ok
testAttackRetunsStrength (__main__.TestSoldier) ... ok
testAttackShouldBeFunction (__main__.TestSoldier) ... ok
testCanReceiveDamage (__main__.TestSoldier) ... FAIL
testConstructorSignature (__main__.TestSoldier) ... ok
testHealth (__main__.TestSoldier) ... ok
testReceiveDamageReturnNone (__main__.TestSoldier) ... ok
testReceivesDamage (__main__.TestSoldier) ... ok
testReceivesDamageHasParams (__main__.TestSoldier) ... ok
testStrength (__main__.TestSoldier) ... ok
======================================================================
FAIL: testCanReceiveDamage (__main__.TestSoldier)
----------------------------------------------------------------------
Traceback (most recent call last):
File "1-testsSoldier.py", line 44, in testCanReceiveDamage
self.assertEqual(self.soldier.health, self.health + 50)
AssertionError: 250 != 350
----------------------------------------------------------------------
Ran 10 tests in 0.001s
Write the code
Now we have to write the correct code in the vikingsClasses.py file to make the test pass. The starter code you will find in the file is the following:
# Soldier
class Soldier:
# Viking
class Viking:
# Saxon
class Saxon:
# War
class War:
In this case, the test says that Soldier constructor function should receive 2 arguments (health & strength), so we have to write the correct code that passes this test. Let's make the Soldier constructor function receive two arguments:
# Soldier
class Soldier:
def __init__(self, health, strength):
# add code here
# Viking
class Viking:
# Saxon
class Saxon:
# War
class War:
Modify the Soldier constructor function and add 2 methods to its prototype: attack(), and receiveDamage().
- should receive 2 arguments (health & strength)
- should receive the
healthproperty as its 1st argument - should receive the
strengthproperty as its 2nd argument
- should be a function
- should receive 0 arguments
- should return the
strengthproperty of theSoldier
- should be a function
- should receive 1 argument (the damage)
- should remove the received damage from the
healthproperty - shouldn't return anything
A Viking is a Soldier with an additional property, their name. They also have a different receiveDamage() method and new method, battleCry().
Modify the Viking constructor function, have it inherit from Soldier, reimplement the receiveDamage() method for Viking, and add a new battleCry() method.
Vikingshould inherit fromSoldier
- should receive 3 arguments (name, health & strength)
- should receive the
nameproperty as its 1st argument - should receive the
healthproperty as its 2nd argument - should receive the
strengthproperty as its 3rd argument
(This method should be inherited from Soldier, no need to reimplement it.)
- should be a function
- should receive 0 arguments
- should return the
strengthproperty of theViking
(This method needs to be reimplemented for Viking because the Viking version needs to have different return values.)
- should be a function
- should receive 1 argument (the damage)
- should remove the received damage from the
healthproperty - if the
Vikingis still alive, it should return "NAME has received DAMAGE points of damage" - if the
Vikingdies, it should return "NAME has died in act of combat"
Learn more about battle cries.
- should be a function
- should receive 0 arguments
- should return "Odin Owns You All!"
A Saxon is a weaker kind of Soldier. Unlike a Viking, a Saxon has no name. Their receiveDamage() method will also be different than the original Soldier version.
Modify the Saxon, constructor function, have it inherit from Soldier and reimplement the receiveDamage() method for Saxon.
Saxonshould inherit fromSoldier
- should receive 2 arguments (health & strength)
- should receive the
healthproperty as its 1st argument - should receive the
strengthproperty as its 2nd argument
(This method should be inherited from Soldier, no need to reimplement it.)
- should be a function
- should receive 0 arguments
- should return the
strengthproperty of theSaxon
(This method needs to be reimplemented for Saxon because the Saxon version needs to have different return values.)
- should be a function
- should receive 1 argument (the damage)
- should remove the received damage from the
healthproperty - if the Saxon is still alive, it should return "A Saxon has received DAMAGE points of damage"
- if the Saxon dies, it should return "A Saxon has died in combat"
Now we get to the good stuff: WAR! Our War constructor function will allow us to have a Viking army and a Saxon army that battle each other.
Modify the War constructor and add 5 methods to its prototype:
addViking()addSaxon()vikingAttack()saxonAttack()showStatus()
When we first create a War, the armies should be empty. We will add soldiers to the armies later.
- should receive 0 arguments
- should assign an empty array to the
vikingArmyproperty - should assign an empty array to the
saxonArmyproperty
Adds 1 Viking to the vikingArmy. If you want a 10 Viking army, you need to call this 10 times.
- should be a function
- should receive 1 argument (a
Vikingobject) - should add the received
Vikingto the army - shouldn't return anything
The Saxon version of addViking().
- should be a function
- should receive 1 argument (a
Saxonobject) - should add the received
Saxonto the army - shouldn't return anything
A Saxon (chosen at random) has their receiveDamage() method called with the damage equal to the strength of a Viking (also chosen at random). This should only perform a single attack and the Saxon doesn't get to attack back.
- should be a function
- should receive 0 arguments
- should make a
SaxonreceiveDamage()equal to thestrengthof aViking - should remove dead saxons from the army
- should return result of calling
receiveDamage()of aSaxonwith thestrengthof aViking
The Saxon version of vikingAttack(). A Viking receives the damage equal to the strength of a Saxon.
- should be a function
- should receive 0 arguments
- should make a
VikingreceiveDamage()equal to thestrengthof aSaxon - should remove dead vikings from the army
- should return result of calling
receiveDamage()of aVikingwith thestrengthof aSaxon
Returns the current status of the War based on the size of the armies.
- should be a function
- should receive 0 arguments
- if the
Saxonarray is empty, should return "Vikings have won the war of the century!" - if the
Vikingarray is empty, should return "Saxons have fought for their lives and survive another day..." - if there are at least 1
Vikingand 1Saxon, should return "Vikings and Saxons are still in the thick of battle."
Create a game using the classes you defined. For this, you will need to:
- Create a new
file.py - Import the classes you defined earlier
- Define functions to create the workflow of the game: i.e. functions to create teams (maybe you can create random teams with your classmates' names), run the game, etc.
- REQUIRED:
vikingsClases.pymodified with your solution to the challenge question.
- https://docs.python.org/3/library/unittest.html
- https://www.python-course.eu/python3_inheritance.php
You can try to make your own tests for your code by creating another test file.

