For performance reasons, we represent codes as integers between $`0`$ and $`M - 1`$. Writing the code in base $`C`$ we obtain the vector of peg colors. For example, in the classic settings $`N = 4`$ and $`C = 6`$, the code $`0123`$ is represented by $`0 \times 6^3 + 1 \times 6^2 + 2 \times 6^1 + 3 \times 6^0 = 51`$.
The feedbacks are also encoded as integers between $`0`$ and $`\frac{(N + 1)(N + 2)}{2} - 1`$. To reduce the size of arrays indexed by the possible feedbacks, we use the fact that $`w + b \le N`$. Here is an example encoding for $`N = 4`$.
| b/w| O| 1| 2| 3|
|---:|---:|---:|---:|---:|
| 0| 6| 7| 8| 9|
| 1| 3| 4| 5| -|
| 2| 1| 2| -| -|
| 3| 0| -| -| -|
Using this encoding, feedback 0 always means that the codebreaker guessed the code. According to the values of $`N`$ and $`C`$, some of the feedbacks above are impossible. For example, the codebreaker can never get a feedback $`\lagle N - 1, 1 \rangle`$. This encoding tries to find a good trade-off between implementation simplicity and memory economy.
The class `CodeToolkit` makes these encodings transparent. The main method is `int feedback(int code, int guess)` For performance reasons, feedbacks a precomputed an stored in a `M \times M` symmetrical `feedbacks` matrix. The class also contains some methods for encoding, decoding and displaying codes and feedbacks.