Rolling the Dice

Be me, someone whom like to play pen-and-paper RPGs, like Dungeons & Dragons or GURPS.

Also, be me, avid computer programmer.

I’ve created dice rolling programs in quite a few languages, C#, Java, and a few others.

Being a web programmer, my strongest language is PHP. I also needed to learn the workings of Symfony Console, a PHP package that let’s you run PHP applications in the console.

The rolling of the dice was pretty strait forward:

Cool, so now we can roll a variable-sided die. But how do we get it to work in the console?

We first need to set up our application in Composer, using a composer.json file like so:

After we install everything, we can start using the Symfony application methods.

I’m going to create a file called dice. No extension, just dice.

I’m going to have two classes, the RollCommand and Dice. I’ve specified in the composer.json file that my code will be in the src folder, so I will create the classes there.

Here’s the first version of those classes I wrote:

Sweet, let’s test that.

That works well! We know what the unmodified roll was, and what each die rolled. Information that is sometimes important, as with critical rolls.

The repository for this post can be found on GitHub here .

The repository for this post can be found on GitLab here .

The repository for this post can be found on Lupe Code’s GitLab mirror here .