Skip to content
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
65 changes: 65 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -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: #Loop doesn't stop if theres army in each list
# 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","Sandra", "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)
108 changes: 98 additions & 10 deletions vikingsClasses.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,112 @@
import random

# Soldier


class Soldier:
pass
# 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):
#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:
return f"{self.name} has received {damage} points of damage"
else:
return f"{self.name} has died in act of combat"

class Viking:
pass
#Define a special method for Vikings to use a special attack.
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} points of 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:
return f"A Saxon has received {damage} points of damage"
else:
return f"A Saxon has died in combat"

class Saxon:
pass

#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)

class War:
pass
def addSaxon(self, saxon):
self.saxonArmy.append(saxon)

#Viking attack
def vikingAttack(self):
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) #If dead remove Saxon from army

return result

def saxonAttack(self):
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) #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."