Carol

Carol is a small program I wrote to try out Palm Pilot programming. It is based on the numbers game from the popular Channel 4 gameshow, Countdown. The object is to combine six selected values using the normal mathematical operations of addition, subtraction, multiplication and (exact) division to get as close as possible to a target value, all within thirty seconds.

The six selected values are chosen from two categories, the large numbers and the small numbers. The large numbers drawn at random from the four values, 25, 50, 75 and 100. The small numbers are drawn at random from the twenty values, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, each repeated twice. The player therefore selects how many large values are required, none, one, two, three or four, the remaining values being drawn from the small values. One large value and five small values give the easiest game. The target value is a randomly generated number between 100 and 999.

Each selected value may only be used once in the solution, however it is not necessary to use all the values. Indeed it is considered more elegant to use as few values as possible.

Instructions

Pressing the New button generates a new game. The number of large values can be selected using the drop list. The solution should be entered into the text box. The solution conforms to the following syntax:

```        literal-expression :
decimal-digit-sequence
'-' literal-expression

primary-expression :
literal-expression
'(' expression ')'
'#'

multiplicative-expression :
primary-expression
multiplicative-expression 'x' primary-expression
multiplicative-expression '*' primary-expression
multiplicative-expression '/' primary-expression

multiplicative-expression

expression :

expression-list :
expression
expression-list expression
```
So the solution consists of a list of expressions each with the normal arithmetic operations. Each expression can use values created in previous expressions. The symbol `#` stands for the result of the previous expression. The result is the value of the last expression. For example, all the following three expression lists evaluate to 952:
```        ( 100 + 6 ) * 9 - 2

100 + 6
106 * 9 - 2

100 + 6
# * 9 - 2
```
Note that in the Palm Pilot version, clicking a number or one of the operation buttons inserts it into the solution area. When the solution has been entered it can be checked by pressing the Check button. Pressing the Clear button clears the solution area.

Pressing the Solve button causes Carol to try to find a solution. There are two algorithms used, both of which are rather simplistic. The first 'exhaustive' algorithm goes through all the possible combinations searching for an exact solution. The second 'quick' algorithm is slightly more directed, but can miss some more complex solutions. The exhaustive algorithm is default on Windows, the quick algorithm is default on Palm Pilot (the exhaustive algorithm just takes too long), and the JavaScript version doesn't have the Solve functionality. You can change the algorithm from the About Carol... box on Windows or from the Options menu on Palm Pilot.

On the Windows and JavaScript versions the values can be explicitly set by writing to the text boxes. In the Palm Pilot it is necessary to select Enable select from the Options menu. Then when the New button is pressed the values all change to ?. Typing a number in the solution area and tapping a ? sets that value (in fact if you type a sequence of n numbers in then tapping a ? sets the next n values). Pressing the New button again will replace any remaining ? values with random values.