I’m starting a series of projects that I will complete over a single weekend to provide a sort of “repository” of example code I have written and software I can produce. These applications are intentionally not meant to be complicated, they are actually directly intended to make it simple to see how to write simple C++ applications with Qt 6, or more specifically, how I write C++ applications with Qt 6.
The first project I did this with is posted here, a Simple Dice Roller Application. This application can be downloaded from Github here. I’m not currently providing precompiled binaries but I will do that eventually as well.
Tour of the Project
The project is made up of three primary class objects: The MainWindow, the DiceWidget, and the DiceRoll. The MainWindow is the primary window of the Dice Roller application and provides the main user interface, as well as the majority of code. All of the button event handlers are coded in the MainWindow class, and most of the application’s event logic is within this class. Since this application is not very big, the MainWindow class ended up being a bit of a God Object, but even with that it is only 200 lines of code if you count all the whitespace, so the actual amount of logic is not that big.
The DiceWidget class is a Widget class that provides a widget you can roll a particular kind of dice with. The Dice Roller allows you to roll all of the conventional D&D dice, however it is also possible to use any arbitrary number of faces (after all, the app just generates a random number between 1 and X, where X is the number of faces). Each widget also allows the user to have up to 99 of a particular dice, and can set an addition or a multiplier on a per-die basis or to the total of the whole set of dice being rolled. This allows for a large number of unique dice rolls that can calculate bonuses immediately.
The DiceRoll class is actually a simple data class that randomly selects a set of numbers each time it is created. After it is created, the data is constant and cannot be changed. The actual random number generation is done by the DiceRoll class directly, using a C++ standard Mersenne Twister engine to generate the random values. Weighing in at only 60 lines of code, this class is quite compact and efficient, and reliably generates random values very quickly.
The Next Project
The next Weekend Project is already decided: A Simple Text Editor! This should be about as (not) complicated as this weeks project, so similarly it should not take all weekend to do and more like just a Saturday afternoon. I have a life so I have a lot to do despite my desire to do this on a consistent basis. We will see how long this lasts!