adding classes, yay

This commit is contained in:
Zachary Watts
2026-04-18 01:37:11 -04:00
parent 74881a607c
commit 9323e7c5d5

102
main.py
View File

@@ -1,7 +1,8 @@
#!/usr/bin/python3 #!/usr/bin/python3
import random import random
class PlayerCharacter: # Player Character Classes
class Adventurer:
def __init__(self, attributes={}) -> None: 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 # 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)) 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.dexterity = attributes.get('dexterity', roll_dice(3,6))
self.constitution = attributes.get('constitution', roll_dice(3,6)) self.constitution = attributes.get('constitution', roll_dice(3,6))
self.charisma = attributes.get('charisma', roll_dice(3,6)) self.charisma = attributes.get('charisma', roll_dice(3,6))
self.player_class = None
def class_selector(self): class Fighter(Adventurer):
# 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}"
class Fighter(PlayerCharacter):
prime_requisite = "strength" prime_requisite = "strength"
def __init__(self, attributes: dict) -> None: def __init__(self, attributes: dict) -> None:
PlayerCharacter.__init__(self, attributes) PlayerCharacter.__init__(self, attributes)
self.player_class = "fighter" self.player_class = "fighter"
self.hp = roll_dice(1, 8) self.hp = roll_dice(1, 8)
class MagicUser(PlayerCharacter): class MagicUser(Adventurer):
prime_requisite = "intelligence" prime_requisite = "intelligence"
def __init__(self, attributes: dict) -> None: def __init__(self, attributes: dict) -> None:
PlayerCharacter.__init__(self, name, attributes) PlayerCharacter.__init__(self, name, attributes)
self.player_class = "magic-user" self.player_class = "magic-user"
self.hp = roll_dice(1, 4) self.hp = roll_dice(1, 4)
class Cleric(PlayerCharacter): class Cleric(Adventurer):
prime_requisite = "wisdom" prime_requisite = "wisdom"
def __init__(self, attributes: dict) -> None: def __init__(self, attributes: dict) -> None:
PlayerCharacter.__init__(self, name, attributes) PlayerCharacter.__init__(self, name, attributes)
self.player_class = "cleric" self.player_class = "cleric"
self.hp = roll_dice(1, 6) self.hp = roll_dice(1, 6)
class Thief(PlayerCharacter): class Thief(Adventurer):
prime_requisite = "dexterity" prime_requisite = "dexterity"
def __init__(self, attributes: dict) -> None: def __init__(self, attributes: dict) -> None:
PlayerCharacter.__init__(self, name, attributes) PlayerCharacter.__init__(self, name, attributes)
self.player_class = "thief" self.player_class = "thief"
self.hp = roll_dice(1, 6) 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 # functions
def roll_dice(count, sides): def roll_dice(count, sides):
return sum(random.randint(1,sides) for _ in range(count)) return sum(random.randint(1,sides) for _ in range(count))
def main(): def main():
adventurer_party = [] new_party = PartyGenerator()
expected_party_size = 4 new_party.gen_party()
while len(adventurer_party) < expected_party_size: print(new_party)
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}")
main() main()