## The Countdown to a Design

This is an analysis and design of a program to allow someone to play the numbers game from the British game show Countdown on a computer. First we’ll review the rules of the game and then we’ll explore techniques for implementing them on a computer.

The numbers game is played with a board of cards containing numbers face down. There are four cards containing either 25, 50, 75, or 100, and twenty cards containing two each of the numbers from 1 to 10. The player specifies six cards to be drawn, between zero and four from the large numbers and the rest drawn from the small numbers. The numbers drawn are revealed and a random number between 0 and 999 is generated. The player then attempts to combine the six numbers using the four arithmetic functions, +, *, -, and /, in order to get as close as possible to the number generated. Ten points are awarded for matching the number exactly, seven points are awarded for an answer between 1 and 5 numbers from the target, and five points are awarded for an answer between 6 and 10 numbers from the target.

The generation of the target is a simple matter since all modern computer languages have a pseudo random number generator which, when combined with some arbitrary parameter such as a portion from the extremely small end of the system clock, provides a truly arbitrary result. For example, if the user were to press the button to generate a target at a time when the nanosecond portion of the system clock contained 345, you might multiply that number by the next pseudo random number returned by the generator and then scale it to be between 0 and 999.

The cards would be handled differently. First, we would declare an array of four big cards and 20 little cards. We would initialize those arrays with the values of the cards that are specified in the rules. Then, we would run an shuffling algorithm, some arbitrary number of times on the big number array, and another arbitrary number of times on the small number array.

The shuffling algorithm would work by generating a pair of distinct random number between zero and three in the case of the big numbers and between zero and nineteen in the case of the little numbers. These numbers would serve as indexes into the respective arrays. For example, you might generate 1 and 3 for the big number array. This would indicate that you should swap the element with an index of 1 with the element with an index of 3. You would repeat this operation some arbitrary number of times generated by calling the random number generator, scaling by a large number and then adding a large number to insure a large minimum number. If you generated a number between 0 and 100 and scaled it by 1000000 and then added 37584 as a minimum you would guarantee that the array would be shuffled between 37584 and 1037584 times. You would shuffle the small numbers in a similar fashion.

Now, given a target number between 1 and 999 and six cards containing numbers that can be used to derive the target, it only remains to give the player 30 seconds to come up with a solution to the problem. But how do we determine if the puzzle even has a solution?

First, we take every combination of the six numbers drawn taken one, two, three, four, five, or six at a time and we combine them using every possible combination of operators. We take each result and store it as either an exact match, a match plus or minus five from the target, a match between six and ten from the target, or else we discard it. Thus, we now know if the puzzle is solvable and we can check the answer that the player gives.

And that is how we analyze and design a program to play the Countdown numbers game. I will use this analysis to actually implement the game. Another time, I’ll analyze and design a program to play the Countdown letters game.

Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Posted on Categories blog