Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my_mini_project #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion 1-testsSoldier.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Define the Soldier class with the required constructor and methods
class Soldier:
def __init__(self, health, strength):
self.health = health
self.strength = strength

def attack(self):
return self.strength

def receiveDamage(self, damage):
self.health -= damage
# No need to return anything, implicitly returns None


# Test suite for the Soldier class
import unittest
from vikingsClasses import Soldier
from inspect import signature


Expand Down
85 changes: 41 additions & 44 deletions 2-testsVikings.py
Original file line number Diff line number Diff line change
@@ -1,67 +1,64 @@
import unittest
from vikingsClasses import Viking
from vikingsClasses import Saxon
from inspect import signature


class TestViking(unittest.TestCase):
class TestSaxon(unittest.TestCase):

@classmethod
def setUp(cls):
cls.name = 'Harald'
cls.strength = 150
cls.health = 300
cls.viking = Viking(cls.name, cls.health, cls.strength)

def testShouldReciveThreeParams(self):
self.assertEqual(len(signature(Viking).parameters), 3)
# Setup test values for Saxon
cls.health = 60
cls.strength = 25
cls.saxon = Saxon(cls.health, cls.strength)

def testName(self):
self.assertEqual(self.viking.name, self.name)
def testSaxonShouldReceiveTwoParams(self):
# Test to ensure Saxon constructor receives 2 parameters
self.assertEqual(len(signature(Saxon).parameters), 2)

def testHealth(self):
self.assertEqual(self.viking.health, self.health)
# Test if Saxon's health is set correctly
self.assertEqual(self.saxon.health, self.health)

def testStrenght(self):
self.assertEqual(self.viking.strength, self.strength)
def testStrength(self):
# Test if Saxon's strength is set correctly
self.assertEqual(self.saxon.strength, self.strength)

def testAttackShouldBeFunction(self):
self.assertEqual(callable(self.viking.attack), True)
def testAttack(self):
# Test if Saxon's attack method is callable
self.assertEqual(callable(self.saxon.attack), True)

def testAttackReciveNoParameters(self):
self.assertEqual(len(signature(self.viking.attack).parameters), 0)
def testAttackShouldReceiveNoParams(self):
# Test if Saxon's attack method takes no parameters
self.assertEqual(len(signature(self.saxon.attack).parameters), 0)

def testAttackShouldReturnStrength(self):
self.assertEqual(self.viking.attack(), self.strength)
def testAttackReturnStrength(self):
# Test if Saxon's attack method returns the strength
self.assertEqual(self.saxon.attack(), self.strength)

def testReceiveDamageIsFunction(self):
self.assertEqual(callable(self.viking.receiveDamage), True)

def testReceiveDamageReciveOneParam(self):
self.assertEqual(
len(signature(self.viking.receiveDamage).parameters), 1)

def testReciveDamageShouldRestHealth(self):
self.viking.receiveDamage(50)
self.assertEqual(self.viking.health, self.health - 50)

def testReciveDamageShouldReturnString50(self):
self.assertEqual(self.viking.receiveDamage(50), self.name +
' has received 50 points of damage')
# Test if Saxon's receiveDamage method is callable
self.assertEqual(callable(self.saxon.receiveDamage), True)

def testReciveDamageShouldReturnString70(self):
self.assertEqual(self.viking.receiveDamage(70), self.name +
' has received 70 points of damage')
def testReceiveDamageShouldReceiveOneParam(self):
# Test if Saxon's receiveDamage method takes 1 parameter (damage)
self.assertEqual(len(signature(self.saxon.receiveDamage).parameters), 1)

def testReceiveDamageShouldReturnStringDeath(self):
self.assertEqual(self.viking.receiveDamage(self.health),
self.name + ' has died in act of combat')
def testReceiveDamage(self):
# Test if Saxon's health decreases correctly after receiving damage
self.saxon.receiveDamage(1)
self.assertEqual(self.saxon.health, self.health - 1)

def testBattleCry(self):
self.assertEqual(callable(self.viking.battleCry), True)
def testReceiveDamageString45(self):
# Test the string returned by Saxon's receiveDamage method when receiving 45 damage
self.assertEqual(self.saxon.receiveDamage(45), 'A Saxon has received 45 points of damage')

def testBattleCryReturnString(self):
self.assertEqual(self.viking.battleCry(), 'Odin Owns You All!')
def testReceiveDamageString10(self):
# Test the string returned by Saxon's receiveDamage method when receiving 10 damage
self.assertEqual(self.saxon.receiveDamage(10), 'A Saxon has received 10 points of damage')

def testReceiveDamageStringDied(self):
# Test the string returned when Saxon dies after receiving full health damage
self.assertEqual(self.saxon.receiveDamage(self.health), 'A Saxon has died in combat')

if __name__ == '__main__':
unittest.main()
Binary file added __pycache__/vikingsClasses.cpython-312.pyc
Binary file not shown.
108 changes: 71 additions & 37 deletions vikingsClasses.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,94 @@
import random

# Soldier


# Soldier class definition
class Soldier:
def __init__(self, health, strength):
# your code here

# Constructor to initialize health and strength of the soldier
self.health = health
self.strength = strength

def attack(self):
# your code here
# Method to return the soldier's attack strength
return self.strength

def receiveDamage(self, damage):
# your code here

# Method to reduce the soldier's health by the given damage
self.health -= damage
# No need to return anything, implicitly returns None

# Viking
# Saxon class definition, inherits from Soldier
class Saxon(Soldier):
def __init__(self, health, strength):
# Constructor to initialize health and strength for Saxon
super().__init__(health, strength) # Call the parent constructor to initialize health and strength

def receiveDamage(self, damage):
# Saxon-specific method to receive damage
self.health -= damage
if self.health > 0:
return f"A Saxon has received {damage} points of damage"
else:
return "A Saxon has died in combat"

# Viking class definition, inherits from Soldier
class Viking(Soldier):
def __init__(self, name, health, strength):
# your code here

def battleCry(self):
# your code here

def receiveDamage(self, damage):
# your code here

# Saxon

class Saxon(Soldier):
def __init__(self, health, strength):
# your code here
# Constructor to initialize health and strength for Viking
super().__init__(health, strength)
self.name = name # Vikings have a name

def receiveDamage(self, damage):
# your code here
# Viking-specific method to receive damage
self.health -= damage
if self.health > 0:
return f"{self.name} has received {damage} points of damage"
else:
return f"{self.name} has died in act of combat"

# Davicente
def battleCry(self):
return "Odin Owns You All!"

class War():
# War class definition
class War:
def __init__(self):
# your code here
self.vikingArmy = [] # Array to store Viking soldiers
self.saxonArmy = [] # Array to store Saxon soldiers

def addViking(self, viking):
# your code here

# Adds a Viking to the vikingArmy
self.vikingArmy.append(viking)

def addSaxon(self, saxon):
# your code here

# Adds a Saxon to the saxonArmy
self.saxonArmy.append(saxon)

def vikingAttack(self):
# your code here

# Viking attack: randomly selects a Viking and a Saxon
if self.saxonArmy:
viking = random.choice(self.vikingArmy)
saxon = random.choice(self.saxonArmy)
result = saxon.receiveDamage(viking.attack())
# Remove dead Saxon from the army
if saxon.health <= 0:
self.saxonArmy.remove(saxon)
return result

def saxonAttack(self):
# your code here
# Saxon attack: randomly selects a Saxon and a Viking
if self.vikingArmy:
saxon = random.choice(self.saxonArmy)
viking = random.choice(self.vikingArmy)
result = viking.receiveDamage(saxon.attack())
# Remove dead Viking from the army
if viking.health <= 0:
self.vikingArmy.remove(viking)
return result

def showStatus(self):
# your code here
pass


# Displays the status of the war
if not self.saxonArmy:
return "Vikings have won the war of the century!"
elif not self.vikingArmy:
return "Saxons have fought for their lives and survive another day..."
else:
return "Vikings and Saxons are still in the thick of battle."