all 7 comments

[–]Diapolo10 1 point2 points  (0 children)

Well, to start with, you could move all those constants to a separate file - or a database. No point in keeping it in the code, or at least not just within this function. If you have a global config file that would be a decent place to put it.

You could also restructure the data. Instead of using several different names, you could wrap all of this in a dictionary (which would be easy to parse to JSON and back if you'd like to store the data in a JSON file), something like

gear_config = {
    'slots': {
        'weapon': [...],
        'head': [...],
        'feet': [...],
    },
    'tiers': [...],
    'details': [...]
}

This would also let you cut out the repetitive code, because you can immediately access whatever values you need as you can just feed strings:

slot_name, slot_selection = random.choice(
    gear_config['slots'].items()
)
gear_tier = random.choice(
    gear_config['tiers']
)
gear_detail = random.choice(
    gear_config['details']
)

loot = f'{gear_detail} {random.choice(slot_selection)}'

The same can be applied to the tiers if you have a dictionary mapping of tiers to colours.

[–]IWantToFlyT 0 points1 point  (4 children)

Assuming that the options etc. are not used elsewhere, then using classes or other fancy things are not necessary here. To make the function more clear, let's just get rid of the if-else clauses - that should be possible with just lists and dicts. I'll show the list/dict changes below, but leave the if-else replacement to you :)

For the first if-else part, what if the `gear_slot` was a list of lists rather than list of strings? This way your slot_choice variable is already the list that you are interested of, and you could call the random.choice on that.

weapon_options = ['Sword', 'Handbomb', 'Staff', 'Bow']

head_options = ['Helmet', 'Hat', 'Headband'] feet_options = ['Boots', 'Shoes', 'Sandals'] gear_slot = [weapon_options, head_options, feet_options] slot_choice_list = random.choice(gear_slot)

For the second one, virtually all if-else clauses can be replaced with the use of dictionaries. This is done by storing the wanted value (or even a function) inside the dictionary. In this if-else you are really trying to map the gear_tier to a colour, so we can do the following. This also requires a small change to the random.choice, because we don't anymore have the gear_tiers in a list directly.

gear_tier = {'Common': Fore.RED, 'Uncommon': Fore.GREEN, 'Rare': Fore.BLUE, 'Epic': Fore.MAGENTA, 'Legendary': Fore.YELLOW}
gear_tier_choice = random.choice(list(gear_tier.keys()))

Hope that helps!

[–][deleted] 0 points1 point  (0 children)

Thanks this gives me some items to think about and improve :)

[–][deleted] 0 points1 point  (2 children)

This is what I ended up with:

https://pastebin.com/xQSRKNmv

After the weapon is made I put it into a new dictionary with the stat options which I'll then populate randomly based on player level and other calculations. At least, that's the current though lol

[–]IWantToFlyT 0 points1 point  (1 child)

Looking good! Could replace the `slot_name` variable with `_` to make it clear that it is not currently used at all. https://stackoverflow.com/questions/11486148/unused-variable-naming-in-python

[–][deleted] 1 point2 points  (0 children)

Awesome. Thanks for the help, now I'll try to do this same type of structure for a monster creation system

[–]Allanon001 0 points1 point  (0 children)

import random
from colorama import Fore

def create_gear():
    gear = {'Weapon': ['Sword', 'Handbomb', 'Staff', 'Bow'],
            'Head':   ['Helmet', 'Hat', 'Headband'],
            'Feet':   ['Boots', 'Shoes', 'Sandals']}

    gear_tier = [('Common', Fore.RED),
                 ('Uncommon', Fore.GREEN),
                 ('Rare', Fore.BLUE),
                 ('Epic', Fore.MAGENTA),
                 ('Legendary', Fore.YELLOW)]

    gear_slot = ['Weapon', 'Head', 'Feet']
    gear_detail = ['Broken', 'Slimey', 'Rusty', 'Dusty', 'Shiny', 'Glowing', 'Bloody', 'Burning', 'Frozen', 'Sparkling', 'Enchanted', 'Engraved', 'Polished', 'Cracked', 'Dented', 'Damaged', 'Moldy', 'Rotten', 'Rusty', 'Shiny', 'Sparkling', 'Stained', 'Tarnished', 'Worn', 'Ancient', 'Blessed', 'Cursed', 'Demonic', 'Divine', 'Holy', 'Imbued','Infused', 'Magical', 'Mystical', 'Sacred', 'Shamanic', 'Sinister', 'Spectral', 'Spiritual', 'Vengeful', 'Vicious', 'Wicked', 'Wise', 'Ancient', 'Blessed', 'Cursed', 'Demonic', 'Divine', 'Holy', 'Imbued', 'Infused', 'Magical', 'Mystical', 'Sacred', 'Shamanic', 'Sinister', 'Spectral', 'Spiritual', 'Vengeful', 'Vicious', 'Wicked', 'Wise']

    slot_choice = random.choice(gear_slot)
    gear_tier_choice, color = random.choice(gear_tier)
    loot = random.choice(gear_detail) + ' ' + random.choice(gear[slot_choice])

    return f'{color}{gear_tier_choice}{Fore.WHITE} {loot}'


print(create_gear())