Skip to content

Instantly share code, notes, and snippets.

@1328
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save 1328/dc9208a3d9f1f7675994 to your computer and use it in GitHub Desktop.

Select an option

Save 1328/dc9208a3d9f1f7675994 to your computer and use it in GitHub Desktop.
ecg
from random import randint, choice
class Race(object):
_races = [
{
'stats':{
'INT':-1,
'LEA':-1,
'ROB':1,
'STR':3
},
'skills':{
'command':20
},
'name':'Wooky',
'hp_range':(5,10),
},
{
'stats':{
'INT':2,
'LEA':-1,
'ESP':2,
'LUC':-1,
'MAG':-2,
'ROB':2,
'STR':1
},
'skills':{
'steal':-10,
'logic':20,
'robot friend':10
},
'name':'Vulkin',
'hp_range':(5,8),
},
]
def __init__(self, **kwargs):
self.name = kwargs['name']
self.skills = kwargs['skills']
self.stats = kwargs['stats']
self.hp_range = kwargs['hp_range']
@property
def hp(self):
'''return a randomly generated hp within hp range selected from race,
note: moved from profession just b/c I did not feel like implementing a
profession.
It may make more sense to move this to player.random, but I wanted to
show you how properties work
'''
return randint(*self.hp_range)
@classmethod
def random(cls):
selected = choice(cls._races)
return cls(**selected)
class Player(object):
_stats = ['ADA', 'DEX', 'ESP',]
def __init__(self, **kwargs):
self.name = kwargs['name']
self.stats = kwargs['stats']
self.skills = kwargs['skills']
self.race = kwargs['race']
self.hp = kwargs['hp']
@classmethod
def random(cls, name):
# pick a random race
race = Race.random()
stats = {key:cls.roll(6,3) for key in cls._stats}
# now flatten out the race object so that we just take the different
# attributes and directly put them into a Player object
for stat, modifier in race.stats.items():
stats[stat] = stats.get(stat, cls.roll(6,3)) + modifier
return cls(name = name,
stats = stats,
skills = race.skills,
race = race.name,
hp = race.hp
)
@staticmethod
def roll(high, count = 1):
'''roll a high sided dice count times, return sum '''
return sum(randint(1, high) for _ in range(count))
def __str__(self):
template = '''{name}: A {race}, with {hp} hp,
skills are {skills}.
Stats are {stats}.'''.format(
name = self.name,
race = self.race,
hp = self.hp,
skills = ', '.join(skill for skill in self.skills),
stats = ', '.join(
'{}:{}'.format(s,v) for s,v in self.stats.items())
)
return template
r = Player.random('steve')
print(r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment