Here is what I have so far:
class Die (object):
def __init__(self,sides):
self.sides = sides
def roll(self):
return random.randint(1,self.sides)
def __add__(self,other):
return Dice(self,other)
def __unicode__(self):
return "1d%d" % (self.sides)
def __str__(self):
return unicode(self).encode('utf-8')
class Dice (object):
def __init__(self, num_dice, sides):
self.die_list = [Die(sides)]*num_dice
def __init__(self, *dice):
self.die_list = dice
def roll(self):
return reduce(lambda x, y: x.roll() + y.roll(), self.die_list)
But when I try to do Dice(3,6)
and subsequently call the roll
action it says it can't because 'int' object has no attribute 'roll'
. That means it's going into the varargs constructor first. What can do I do here to make this work, or is there another alternative?