Skip to content

Instantly share code, notes, and snippets.

@stephengruppetta
Created June 14, 2023 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephengruppetta/9a05569222e87d923d03c5247e121f6a to your computer and use it in GitHub Desktop.
Save stephengruppetta/9a05569222e87d923d03c5247e121f6a to your computer and use it in GitHub Desktop.
# hogwarts_magic.py
import random
class Wizard:
def __init__(self, name, patronus, birth_year):
self.name = name
self.patronus = patronus
self.birth_year = birth_year
self.house = None
self.wand = None
self.skill = 0.2 # 0.0 (bad) to 1.0 (good)
def increase_skill(self, amount):
self.skill += amount
if self.skill > 1.0:
self.skill = 1.0
def assign_wand(self, wand):
self.wand = wand
print(f"{self.name} has a {self.wand} wand.")
def assign_house(self, house):
self.house = house
house.add_member(self)
def cast_spell(self, spell):
if self.wand:
effect = self.wand.cast_spell(spell, self)
if effect:
print(f"{self.name} cast {effect}!")
else:
print(f"{self.name} failed to cast {spell.name}!")
else:
print(f"{self.name} has no wand!")
class Professor(Wizard):
def __init__(self, name, patronus, birth_year, subject):
super().__init__(name, patronus, birth_year)
self.subject = subject
def assess_student(self, student, mark):
# ToDo: Finish writing this method
...
class House:
def __init__(self, name, founder, colours, animal):
self.name = name
self.founder = founder
self.colours = colours
self.animal = animal
self.members = []
self.points = 0
def add_member(self, member):
if member not in self.members:
self.members.append(member)
def remove_member(self, member):
self.members.remove(member)
def update_points(self, points):
self.points += points
def get_house_details(self):
return {
"name": self.name,
"founder": self.founder,
"colours": self.colours,
"animal": self.animal,
"points": self.points
}
class Wand:
def __init__(self, wood, core, length, power=0.5):
self.wood = wood
self.core = core
self.length = length
self.power = power # 0.0 (weak) to 1.0 (strong)
def cast_spell(self, spell, wizard):
if spell.is_successful(self, wizard):
return spell.effect
return None # Explicitly return None (for readability)
class Spell:
def __init__(self, name, effect, difficulty):
self.name = name
self.effect = effect
self.difficulty = difficulty # 0.0 (easy) to 1.0 (hard)
def is_successful(self, wand, wizard):
success_rate = (
(1 - self.difficulty)
* wand.power
* wizard.skill
)
return random.random() < success_rate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment