>>>>> "Tobias" == iso8859-1 <iso8859-1@[EMAIL PROTECTED]
> writes:
Tobias> I managed to create some kind of die that uses the
Tobias> procedure roll to roll a die and then a query d(side :
Tobias> Integer) to get the result interpreted as an appropriate
Tobias> die.
Tobias> But how do I design the dice roller for these complexer
Tobias> expressions? In Java I would just do something like
Tobias> roll(count, side, offset : Integer) : Integer but that
Tobias> would change the internal state of the object. How do I
Tobias> seperate the queries from the commands cleanly in this
Tobias> case? Could someone give me an idea?
The usual way is for roll to set an attribute (say last_roll: INTEGER)
to the value produced. Then you call `roll' and follow by accessing the
value of `last_roll'.
If you need to access this object from multiple threads, then you need
to synchronize round the two calls. An alternative design for this
scenario is to pass a reference argument to receive the result.
Note that I wouldn't make `side' an argument of `roll'. rather I would
make it an attribute of the class. You set it in the creation
procedure, and have a different object for a 6-sided die, and a
20-sided die.
Although I wouldn't really want to transfer the 3d6, 1d100 examples
directly from an rpg to my program. Instead I would go for the
abstract of a random number distribution, and sample that directly
(you might name the distribution objects with names such as 3d6
though, within an rpg).
--
Colin Adams
Preston Lancashire


|