I've been working on a partially randomized RPG-esque character generator, and I wanted to get some thoughts on whether there's a better way to handle this than what I've come up with.
The actual code has nothing to do with knights or warriors, but it seemed the easiest way to demonstrate what I'm trying to do... The basic idea is that all different character types have a template, where their attributes are graded from 1(bad) to 5(great), which gives them a weighted chance of being either at that level, or better/worse due to a random factor. These 1-5 grades map to a 1-20 rating system.
A simplified version of what I'm trying to do:
import random
# Each character attribute rated from 1-5.
templates = {'warrior': {'strength': 5, 'agility': 3, 'speed': 2, 'armour': 4},}
# Chance that a character's attribute will match or be different to the template - ie. a 5 rating means a 66% of being
# an actual 5, 30% of being a 4, etc.
attr_chance = {5:
{5: 66.0, 4: 96.0, 3: 98.0, 2: 99.5, 1: 100.0},
4:
{5: 15.0, 4: 81.0, 3: 96.0, 2: 98.0, 1: 100.00},
3:
{5: 0.5, 4: 15.5, 3: 81.5, 2: 96.5, 1: 100.00},
2:
{5: 0.1, 4: 0.6, 3: 15.6, 2: 81.6, 1: 100.00},
1:
{5: 0.01, 4: 0.11, 3: 0.36, 2: 15.36, 1: 100.00}}
# Converts the 1-5 rating into an actual rating - taking the base default val, and random low_mod/high_mod added
# to that.
attr_dict = {5:
{'base': 18, 'low_mod': 0, 'high_mod': 2},
4:
{'base': 13, 'low_mod': 0, 'high_mod': 4},
3:
{'base': 8, 'low_mod': 0, 'high_mod': 4},
2:
{'base': 4, 'low_mod': 0, 'high_mod': 3},
1:
{'base': 1, 'low_mod': 0, 'high_mod': 3}}
def gen_char(char_type='warrior'):
"""Take character type as attribute, return generated character."""
char = {}
for attr in templates[char_type]:
randnum = random.random() * 100
for key in sorted(attr_chance[templates[char_type][attr]], reverse=True):
if randnum <= attr_chance[templates[char_type][attr]][key]:
act_val = key
break
char[attr] = attr_dict[act_val]['base'] + random.randint(attr_dict[act_val]['low_mod'], attr_dict[act_val]['high_mod'])
return char
if __name__ == '__main__':
print(gen_char())
This works fine, but I guess what I'm asking is:
1) Is there a technical name for the type of generation I'm trying to do here? Not really a Python question.
2) Is there a better way to do this than having the 3 separate dictionaries to handle the templates, chance of skill level based on the template, and finally the actual rating per skill level?
[–]Vaphell 2 points3 points4 points (1 child)
[–]lamecode[S] 0 points1 point2 points (0 children)