|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import sys |
| 4 | +from abc import ABCMeta, abstractmethod |
| 5 | +from random import randint |
| 6 | + |
| 7 | +""" |
| 8 | +Dice classes. |
| 9 | +
|
| 10 | +These are just plain dice, not dice notation, even thought they may be used |
| 11 | +for handling that too. |
| 12 | +
|
| 13 | +The classes in this file will allow creating and using dice and rollable |
| 14 | +entities, which are able to generate a random value. |
| 15 | +
|
| 16 | +There are two dice classes, the Dice is just the bare dice, while the |
| 17 | +RollableDice is an extension, which allows rolling the dice. |
| 18 | +""" |
| 19 | + |
| 20 | +__author__ = 'Benardo Martínez Garrido' |
| 21 | +__license__ = 'MIT' |
| 22 | + |
| 23 | +if sys.version_info[0] >= 3: |
| 24 | + xrange = range |
| 25 | + |
| 26 | + |
| 27 | +class Rollable(object): |
| 28 | + """ |
| 29 | + Interface for rollable classes. |
| 30 | +
|
| 31 | + While rolling implies using dice to generate a random value, this interface |
| 32 | + just takes care of generating a random value. |
| 33 | +
|
| 34 | + This way it not only can support any kind of dice, but also more complex |
| 35 | + constructions such as dice notation expressions, where calling the roll |
| 36 | + method would execute the full expression. |
| 37 | +
|
| 38 | + As such, the value generated by rolling may be anything. |
| 39 | + """ |
| 40 | + __metaclass__ = ABCMeta |
| 41 | + |
| 42 | + def __init__(self): |
| 43 | + pass |
| 44 | + |
| 45 | + @abstractmethod |
| 46 | + def roll(self): |
| 47 | + """ |
| 48 | + Generates a random value. |
| 49 | +
|
| 50 | + This can be anything, the only expectation is that the output |
| 51 | + is randomized somehow. |
| 52 | + """ |
| 53 | + raise NotImplementedError('The roll method must be implemented') |
| 54 | + |
| 55 | + |
| 56 | +class Dice(object): |
| 57 | + """ |
| 58 | + A group of dice, all with the same number of sides. Such a group is just |
| 59 | + composed of a quantity of dice, and their number of sides. |
| 60 | +
|
| 61 | + Both the quantity and the number of sides are expected to be positive, as |
| 62 | + any other value would make no sense. |
| 63 | +
|
| 64 | + No other limitation is expected. In the real world the number of sides |
| 65 | + which a die may physically have are limited by the rules of geometry, |
| 66 | + but there is no reason to take care of that. |
| 67 | + """ |
| 68 | + |
| 69 | + def __init__(self, quantity, sides): |
| 70 | + super(Dice, self).__init__() |
| 71 | + self._quantity = quantity |
| 72 | + self._sides = sides |
| 73 | + |
| 74 | + def __str__(self): |
| 75 | + return '%sd%s' % (self.quantity, self.sides) |
| 76 | + |
| 77 | + def __repr__(self): |
| 78 | + return '<class %s>(quantity=%r, sides=%r)' % \ |
| 79 | + (self.__class__.__name__, self.quantity, self.sides) |
| 80 | + |
| 81 | + @property |
| 82 | + def quantity(self): |
| 83 | + """ |
| 84 | + The number of dice which compose this group. |
| 85 | +
|
| 86 | + This is expected to be a positive value or zero. |
| 87 | +
|
| 88 | + :return: the number of dice |
| 89 | + """ |
| 90 | + return self._quantity |
| 91 | + |
| 92 | + @quantity.setter |
| 93 | + def quantity(self, quantity): |
| 94 | + self._quantity = quantity |
| 95 | + |
| 96 | + @property |
| 97 | + def sides(self): |
| 98 | + """ |
| 99 | + The number of sides each die has. |
| 100 | +
|
| 101 | + All the dice in the group have the same number of sides. |
| 102 | +
|
| 103 | + This is expected to be a positive value or zero. |
| 104 | +
|
| 105 | + :return: the number of sides |
| 106 | + """ |
| 107 | + return self._sides |
| 108 | + |
| 109 | + @sides.setter |
| 110 | + def sides(self, sides): |
| 111 | + self._sides = sides |
| 112 | + |
| 113 | + |
| 114 | +class RollableDice(Dice, Rollable): |
| 115 | + """ |
| 116 | + A rollable dice group. |
| 117 | +
|
| 118 | + The result of calling the roll method will be an integer, which will be |
| 119 | + between 1 and the number of sides. Actually one number will be generated |
| 120 | + like that as many times as the value of the quantity field, and all those |
| 121 | + values will be added, and then returned. |
| 122 | + """ |
| 123 | + |
| 124 | + def __init__(self, quantity, sides): |
| 125 | + super(RollableDice, self).__init__(quantity, sides) |
| 126 | + |
| 127 | + def roll(self): |
| 128 | + result = 0 |
| 129 | + |
| 130 | + if self.quantity == 0 or self.sides == 0: |
| 131 | + result = 0 |
| 132 | + elif self.quantity is None or self.sides is None: |
| 133 | + result = None |
| 134 | + elif self.quantity > 0 and self.sides > 0: |
| 135 | + for x in xrange(self.quantity): |
| 136 | + result += randint(1, self.sides) |
| 137 | + else: |
| 138 | + result = None |
| 139 | + |
| 140 | + return result |
0 commit comments