Return to Blog Home

Novemator

Source Live

Novemator

Can you Math your way to a number?

Inspired by the game show Countdown where a target computer is generated and the players have to get as close to said target number as possible using only a few numbers, and the daily-problems of Wordle, I decided to make my own spin on both of these!

Concept

The game itself would randomly generate a number, and 9 numbers combined with operators. There would be at least one guarnteed way to generate the target number by pressing the buttons in the correct order, where the difficulty of the problem could easily be scaled by increasing the number of buttons needed to be pressed to reach the target number.

Additionally I wanted the problems to be random yet persist per day, like Wordle.

Implementation

Most of the fronten for this game was relativly simple - it was a single page of HTML, CSS, and JS - switching between three screens: difficulty selection, the gameplay, and the won screen.

Difficulty selection was the calculator but with the 2-9 buttons only available, while the won screen was an emoji-generated string of the order of buttons needed to be pressed.

The gameplay screen was the same calculator with an additional Clear and Shuffle button - allowing for resetting and getting another game for today.

Math

Actually generating the permutations was the challenging part, as I needed to guarntee that the randomly generated 9 buttons could be pressed in so many times and result in the target number.

Firstly I wrote the algorithim to generate all permutations of all lengths, and I would filter these down to the number of button presses for the current difficulty and choose from one of them.

This turned out to be quite an inefficient program - even after inspecting the performance and switching to Trie, which while improving performance still left the browser frozen for up to five seconds every time!

The solution in this case would have been to optimize my algorithim, but it was quicker to simply offload this work to a Worker, so that's what I did.

Workers

Thankfully for simple data-generation tasks working with Web Workers is relativly straightforward, one need only to create a new Worker, and post/listen to messages to it - and the same was for the worker itself, listening to messages and posting responses back.