
Ugly Trivia
A trivia game with ugly code ready to be refactored
Each turn, a player rolls the dice and has to answer a question from the deck corresponding to their current position. If the answer is correct [s]he gets coins. If incorrect, [s]he is sent to prison. The board has 12 positions.
You should discover the rest of the details from the actual code, just like in real life. Good luck!
Instructions
- Download a version of the code to refactor in your language of choice.
- Add the necessary tests to make sure you don't break the code while you refactor.
- Improve the design by refactoring (without altering the behaviour of the code).
Golden Master Technique
We can't write reasonable unit tests without refactoring first. But we don't want to refactor without tests at all.
But we can test the application as a whole. Gain control over all external input sources (RNGs, system time, keyboard input and so on). Then save all outputs produced (console output, emails sent, files changed etc.). This is our Golden Master.
Now we can change the code and check whether it still yields the same results as before.
Code to Refactor
- trivia, by J.B. Rainsberger, in many languages