From 9323e7c5d56a9b6061feb5f1fda22f80730d2e25 Mon Sep 17 00:00:00 2001 From: Zachary Watts Date: Sat, 18 Apr 2026 01:37:11 -0400 Subject: [PATCH] adding classes, yay --- main.py | 102 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index 3719d22..8f49419 100755 --- a/main.py +++ b/main.py @@ -1,7 +1,8 @@ #!/usr/bin/python3 import random -class PlayerCharacter: +# Player Character Classes +class Adventurer: def __init__(self, attributes={}) -> None: # using a get() method to pull an attribute else use a default value, https://python-academy.org/en/handbook/get self.strength = attributes.get('strength', roll_dice(3,6)) @@ -10,75 +11,94 @@ class PlayerCharacter: self.dexterity = attributes.get('dexterity', roll_dice(3,6)) self.constitution = attributes.get('constitution', roll_dice(3,6)) self.charisma = attributes.get('charisma', roll_dice(3,6)) - - def class_selector(self): - # take the 4 primary attribtues - base_attr = self.__dict__.copy() - del base_attr['constitution'] - del base_attr['charisma'] - # of those grab the highest, found this at https://stackoverflow.com/a/280156 - max_base_attr = max(base_attr, key=base_attr.get) - # use that to pick the best possible base class - best_class = { "strength" : "Fighter", "intelligence" : "Magic User", "wisdom" : "Cleric", "dexterity" : "Thief" } - best_base_class = best_class[max_base_attr] - # create a list of possible classes - possible_classes = [ best_base_class ] - # check if player qualifies for other classes - if self.constitution >= 9 : possible_classes.append("Dwarf") - if self.intelligence >= 9 : possible_classes.append("Elf") - if self.constitution >= 9 and self.dexterity >= 9 : possible_classes.append("Halfling") - # randomly select class - selected_class = random.choice(possible_classes) - return f"{selected_class}" + self.player_class = None -class Fighter(PlayerCharacter): +class Fighter(Adventurer): prime_requisite = "strength" def __init__(self, attributes: dict) -> None: PlayerCharacter.__init__(self, attributes) self.player_class = "fighter" self.hp = roll_dice(1, 8) -class MagicUser(PlayerCharacter): +class MagicUser(Adventurer): prime_requisite = "intelligence" def __init__(self, attributes: dict) -> None: PlayerCharacter.__init__(self, name, attributes) self.player_class = "magic-user" self.hp = roll_dice(1, 4) -class Cleric(PlayerCharacter): +class Cleric(Adventurer): prime_requisite = "wisdom" def __init__(self, attributes: dict) -> None: PlayerCharacter.__init__(self, name, attributes) self.player_class = "cleric" self.hp = roll_dice(1, 6) -class Thief(PlayerCharacter): +class Thief(Adventurer): prime_requisite = "dexterity" def __init__(self, attributes: dict) -> None: PlayerCharacter.__init__(self, name, attributes) self.player_class = "thief" self.hp = roll_dice(1, 6) +# Player Class Selector +class ClassSelector(): + def __init__(self, player: Adventurer) -> None: + self.player = player + self.prime_requisites = { "strength" : "Fighter", "intelligence" : "Magic User", "wisdom" : "Cleric", "dexterity" : "Thief" } + self.selected_class = self.selection() + + def get_best_prime_attribute(self): + attributes = self.player.__dict__.copy() + del attributes['player_class'] + # we should not evaluate these two stats + del attributes['constitution'] + del attributes['charisma'] + # of those remaining, return highest, found this at https://stackoverflow.com/a/280156 + return max(attributes, key=attributes.get) + + def selection(self): + best_prime_attribute = self.get_best_prime_attribute() + # create an array of possible player classes, add the best choice per player's best core attributes + class_to_add = self.prime_requisites[best_prime_attribute] + possible_classes = [class_to_add] + # check if player qualifies for other classes + if self.player.constitution >= 9 : possible_classes.append("Dwarf") + if self.player.intelligence >= 9 : possible_classes.append("Elf") + if self.player.constitution >= 9 and self.player.dexterity >= 9 : possible_classes.append("Halfling") + # randomly select class + selected_class = random.choice(possible_classes) + return f"{selected_class}" + +class PartyGenerator(): + def __init__(self, party_size=4) -> None: + self.size = party_size + self.adventurers = [] + + def gen_party(self): + while len(self.adventurers) < self.size: + new_player = Adventurer() + attempts = 0 + while new_player.player_class not in self.adventurers: + attempts += 1 + my_adv = ClassSelector(new_player).selected_class + new_player.player_class = my_adv + if new_player.player_class not in self.adventurers: + self.adventurers.append(new_player.player_class) + # i couldnt randomly generate a scenario where a character couldn't be added, but it seems possible, so this is the hard cut off + elif attempts > 10: + self.adventurers.append(new_player.player_class) + + def __str__(self): + return f"{self.adventurers}" + # functions def roll_dice(count, sides): return sum(random.randint(1,sides) for _ in range(count)) def main(): - adventurer_party = [] - expected_party_size = 4 - while len(adventurer_party) < expected_party_size: - new_player = PlayerCharacter() - selected_class = None - attempts = 0 - while selected_class not in adventurer_party: - attempts += 1 - selected_class = new_player.class_selector() - if selected_class not in adventurer_party: - adventurer_party.append(selected_class) - # i couldnt randomly generate a scenario where a character couldn't be added, but it seems possible, so this is the hard cut off - elif attempts > 10: - adventurer_party.append(selected_class) - - print(f"{adventurer_party}") + new_party = PartyGenerator() + new_party.gen_party() + print(new_party) main()