Created
April 4, 2014 00:43
-
-
Save bitoffdev/9965827 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
class Die(object): | |
def __init__(self, sides=None): | |
if isinstance(sides, int): | |
self.sides = sides | |
else: | |
self.sides = 6 | |
def roll(self): | |
return randint(1, self.sides) | |
class Dice(object): | |
def __init__(self, *args): | |
self.pieces = [] | |
for item in args: | |
self.add(item) | |
def add(self, newdie): | |
if isinstance(newdie, Die): | |
self.pieces.append(newdie) | |
def rollAll(self): | |
return [i.roll() for i in self.pieces] | |
#Example usage: roll 3 normal dice | |
if __name__ == '__main__': | |
print Dice(Die(), Die(), Die()).rollAll() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment