From 9353934445220ecb840bfe49b579b7c48e0df9a1 Mon Sep 17 00:00:00 2001 From: Oriol Codina Vallori Date: Sun, 15 Oct 2023 14:02:36 +0200 Subject: [PATCH 1/3] lab before bonus --- vikingsClasses.py | 83 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 6 deletions(-) diff --git a/vikingsClasses.py b/vikingsClasses.py index b51cd5f..0a726cd 100644 --- a/vikingsClasses.py +++ b/vikingsClasses.py @@ -1,24 +1,95 @@ +import random + # Soldier class Soldier: - pass + + def __init__(self, health, strength): + self.health = health + self.strength = strength + + def attack(self): + return self.strength + + def receiveDamage(self, damage): + self.health -= damage + # Viking -class Viking: - pass +class Viking (Soldier): + + def __init__(self, name, health, strength): + super().__init__(health, strength) + self.name = name + + def receiveDamage(self, 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" + + def battleCry(self): + return "Odin Owns You All!" # Saxon -class Saxon: - pass +class Saxon(Soldier): + + def receiveDamage(self, damage): + self.health -= damage + if self.health > 0: + return f"A Saxon has received {damage} points of damage" + else: + return f"A Saxon has died in combat" + # War class War: - pass + + def __init__(self): + self.vikingArmy = [] + self.saxonArmy = [] + + def addViking(self, viking): + self.vikingArmy.append(viking) + + def addSaxon(self, saxon): + self.saxonArmy.append(saxon) + + def vikingAttack(self): + saxon = random.choice(self.saxonArmy) + viking = random.choice(self.vikingArmy) + + result = saxon.receiveDamage(viking.attack()) + + if saxon.health <= 0: + self.saxonArmy.remove(saxon) + + return result + + def saxonAttack(self): + viking = random.choice(self.vikingArmy) + saxon = random.choice(self.saxonArmy) + + result = viking.receiveDamage(saxon.attack()) + + if viking.health <= 0: + self.vikingArmy.remove(viking) + + return result + + def showStatus(self): + 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." \ No newline at end of file From 468e4f1e1a017bf29171053e6e5c9c2c28a545d4 Mon Sep 17 00:00:00 2001 From: Oriol Codina Vallori Date: Sun, 15 Oct 2023 21:05:43 +0200 Subject: [PATCH 2/3] Bonus done --- game.py | 65 +++++++++++++++++++++++++++++++++++++++++++++ vikingsClasses.py | 67 +++++++++++++++++++++++++++++------------------ 2 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 game.py diff --git a/game.py b/game.py new file mode 100644 index 0000000..04a3b62 --- /dev/null +++ b/game.py @@ -0,0 +1,65 @@ +import random +from vikingsClasses import War, Viking, Saxon + + +# Function to create teams +def create_teams(num_vikings, num_saxons): + viking_team = [] + saxon_team = [] + + # Create Viking team + for _ in range(num_vikings): + health = random.randint(50, 100) #health between 50 and 100 + strength = random.randint(20, 40) #strenght between 20 and 40 + viking_name = random.choice(viking_names) + viking = Viking(viking_name, health, strength) + viking_names.remove(viking_name) # Remove the selected name from the list + viking_team.append(viking) + + # Create Saxon team + for _ in range(num_saxons): + health = random.randint(30, 60) #health between 30 and 60 + strength = random.randint(5, 15) #strenght between 5 and 15 + saxon = Saxon(health, strength) + saxon_team.append(saxon) + + return viking_team, saxon_team + +# Function to run the game +def run_game(num_vikings, num_saxons): + # Create teams + viking_team, saxon_team = create_teams(num_vikings, num_saxons) + + # Create a War instance + war = War() + + # Add Vikings and Saxons to the war + for viking in viking_team: + war.addViking(viking) + + for saxon in saxon_team: + war.addSaxon(saxon) + + # Main game loop + while war.vikingArmy and war.saxonArmy: + # Randomly choose an attacker + attacker = random.choice(["Viking", "Saxon"]) + + if attacker == "Viking": + result = war.vikingAttack() + + else: + result = war.saxonAttack() + + print(result) + + # Determine the outcome + outcome = war.showStatus() + print(outcome) + +# Entry point of the game +viking_names = ["Javier", "Ricardo", "Edu", "Marco", "Marc", "Noe", "Ana", "Mira", "Martina", "Marta", "Lucia", "Claudia", "Ester", "Felix", "Pati", "Pere", "Amir", "Patricia", "Emma", "Vicotr", "Miquel", "León", "Junior", "Bego", "Fer", "Uri"] +if __name__ == "__main__": + num_vikings = 10 # Adjust the number of Vikings + num_saxons = 30 # Adjust the number of Saxons + run_game(num_vikings, num_saxons) diff --git a/vikingsClasses.py b/vikingsClasses.py index 0a726cd..b30edb5 100644 --- a/vikingsClasses.py +++ b/vikingsClasses.py @@ -1,31 +1,28 @@ - import random # Soldier - - class Soldier: - + # Initialize a Soldier with health and strength attributes. def __init__(self, health, strength): self.health = health self.strength = strength + #Soldier attack, returning their strength. def attack(self): return self.strength - + + #Soldier takes damage def receiveDamage(self, damage): self.health -= damage - # Viking - - -class Viking (Soldier): - +class Viking(Soldier): + #Viking inherits from Soldier, re-initialize because also takes 'name' property def __init__(self, name, health, strength): super().__init__(health, strength) self.name = name + #Re-defines Soldier damage. Viking receives damage and return istring. If dead return the propper string def receiveDamage(self, damage): self.health -= damage if self.health > 0: @@ -33,14 +30,18 @@ def receiveDamage(self, damage): else: return f"{self.name} has died in act of combat" + #Define a special method for Vikings to use a special attack. def battleCry(self): - return "Odin Owns You All!" + if random.random() <= 0.33: # 33% chance for the special attack + damage = random.randint(2, 10) #Randomly applyy between 2 and 10 damage + return f"📣 {self.name} has used 'battle cry' -Odin owns you all!-\n ⚔️ Inflicts {damage} to each Saxon" + return "" # Saxon - - class Saxon(Soldier): - + #Saxon inherits from Soldier, no need to re-initialize + + #Re-defines Soldier damage. Saxon receives damage and return istring. If dead return the propper string def receiveDamage(self, damage): self.health -= damage if self.health > 0: @@ -48,48 +49,64 @@ def receiveDamage(self, damage): else: return f"A Saxon has died in combat" - + #Define a special method for Vikings to use a special attack. + def reinforcement(self, war): + if random.random() <= 0.1: #10% chance for the special attack + num_reinforcements = random.randint(2, 10) #Randomly add between 2 and 10 weaker Saxons to the army with health 10 to 20 and strenght 1 to 3 + for _ in range(num_reinforcements): #Loop to trigger the addSaxon() method from War + war.addSaxon(Saxon(random.randint(10, 20), random.randint(1, 3))) + return f"❗ A sneaky Saxon has called for reinforcement.\n 🛡️ {num_reinforcements} weaker Saxons joined the battle" + return "" # War - - class War: - + #Initialize war with the armys def __init__(self): self.vikingArmy = [] self.saxonArmy = [] - + + #Add Viking/Saxon instances to each army def addViking(self, viking): self.vikingArmy.append(viking) def addSaxon(self, saxon): self.saxonArmy.append(saxon) + #Viking attack def vikingAttack(self): - saxon = random.choice(self.saxonArmy) + saxon = random.choice(self.saxonArmy) #Random select the Viking and the Saxon being attacked viking = random.choice(self.vikingArmy) + # Calculate the result of the attack and any special attck effects. result = saxon.receiveDamage(viking.attack()) + battle_cry_result = viking.battleCry() + if battle_cry_result: + result += "\n" + battle_cry_result #Special attack result if saxon.health <= 0: - self.saxonArmy.remove(saxon) + self.saxonArmy.remove(saxon) #If dead remove Saxon from army return result def saxonAttack(self): - viking = random.choice(self.vikingArmy) + viking = random.choice(self.vikingArmy) #Random select the Saxon and the Viking being attacked saxon = random.choice(self.saxonArmy) + # Calculate the result of the attack and any special attck effects. result = viking.receiveDamage(saxon.attack()) + reinforcement_result = saxon.reinforcement(self) # Pass the War object. + if reinforcement_result: + result += "\n" + reinforcement_result #Special attack result if viking.health <= 0: - self.vikingArmy.remove(viking) + self.vikingArmy.remove(viking) #If dead remove Viking from army return result - + + #Return the status of the battle. If any of the two armys empty returns the winner def showStatus(self): 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." \ No newline at end of file + return "Vikings and Saxons are still in the thick of battle." From 2ce703f066114bce71204e90262063caf954c947 Mon Sep 17 00:00:00 2001 From: Oriol Codina Vallori Date: Mon, 16 Oct 2023 12:45:09 +0200 Subject: [PATCH 3/3] minor fixes --- game.py | 4 ++-- vikingsClasses.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/game.py b/game.py index 04a3b62..00fe0c5 100644 --- a/game.py +++ b/game.py @@ -41,7 +41,7 @@ def run_game(num_vikings, num_saxons): war.addSaxon(saxon) # Main game loop - while war.vikingArmy and war.saxonArmy: + while war.vikingArmy and war.saxonArmy: #Loop doesn't stop if theres army in each list # Randomly choose an attacker attacker = random.choice(["Viking", "Saxon"]) @@ -58,7 +58,7 @@ def run_game(num_vikings, num_saxons): print(outcome) # Entry point of the game -viking_names = ["Javier", "Ricardo", "Edu", "Marco", "Marc", "Noe", "Ana", "Mira", "Martina", "Marta", "Lucia", "Claudia", "Ester", "Felix", "Pati", "Pere", "Amir", "Patricia", "Emma", "Vicotr", "Miquel", "León", "Junior", "Bego", "Fer", "Uri"] +viking_names = ["Javier", "Ricardo", "Edu", "Marco", "Marc", "Noe", "Ana", "Mira", "Martina", "Marta", "Lucia", "Claudia", "Ester", "Felix", "Pati", "Pere", "Amir", "Patricia", "Emma", "Vicotr", "Miquel", "León", "Junior", "Bego", "Fer","Sandra", "Uri"] if __name__ == "__main__": num_vikings = 10 # Adjust the number of Vikings num_saxons = 30 # Adjust the number of Saxons diff --git a/vikingsClasses.py b/vikingsClasses.py index b30edb5..23badd4 100644 --- a/vikingsClasses.py +++ b/vikingsClasses.py @@ -34,7 +34,7 @@ def receiveDamage(self, damage): def battleCry(self): if random.random() <= 0.33: # 33% chance for the special attack damage = random.randint(2, 10) #Randomly applyy between 2 and 10 damage - return f"📣 {self.name} has used 'battle cry' -Odin owns you all!-\n ⚔️ Inflicts {damage} to each Saxon" + return f"📣 {self.name} has used 'battle cry' -Odin owns you all!-\n ⚔️ Inflicts {damage} points of damage to each Saxon" return "" # Saxon