Saturday, January 24, 2026

Dice As a Design Problem

The other day I ran into an interesting programming exercise on BlueSky.

The project description is at 2D Dice Grid Scoring Algorithm - 101 Computing It’s a cool project. I decided to code up a solution myself. Now there is sample starter code at that link in Python. I do my fun programming in C# so I started from scratch.

The first thing I had to do was to think about a Die class. I’ve written classes for dice projects many times before. It was a favorite item for me to use when teaching students about designing classes. Just about everyone is familiar with dice. I also brought in some samples to use as visual aids. I had some binary dice with only ones and zeros and some role playing dice in a variety of shapes and numbers of sides.

Students generally come up with the idea that they need to have a face value for the die. They generally also easily come up with the need to display that value and methods to change it to a random value. What they don’t always remember right away is that no all dice have six sides. Some dice have many more than six sides. Eventually they come up with two sided dice which we sometimes call coins.

I had a couple of example Die classes from other projects but I decided I wanted to be a bit more visual. So I created an object with the ability to display images. For this particular project I also added an extra method. I added a method to return if the face value was even – a Boolean value – true for even, false for odd. You know, just to make things interesting. Right now it is a method but I want to change it to a property to avoid unneeded parentheses.  I am not a fan of parentheses.

I did cheat a little. I had Copilot create some of the initial work on the code. Copilot, like my students, assumed a six sided die with values from one to six. I didn’t specify much so that’s understandable. It’s not really satisfying for me though so I will be putting some extra work into things to make the class more flexible. I will add constructors that let a program use different images and numbers of images. After all, just as not all die have six sides not all die have numbers or pips on them.

What would/do you add to die objects to make them more interesting or useful?

My project looks like this BTW.

No comments:

Post a Comment