Introduction
TODO: Introduce game rules and related work.
Notations
Let
be the number of pegs in the code and
be the number of possible peg colors. A code is a vector in
. The number of possible codes is
. The game is played as follows:
- The codemaker chooses a secret code .
- The codebreaker tries to guess in as few as possible iterations. At each iteration:
- The codebreaker produces a guess code .
- The codemaker provides a feedback whereis the number of pegs which are correct in both color and position andis the number of pegs which are correct in color but not in position.
- The game ends when the codebreaker guesses the secret code (gets feedback ) or when a predefined maximum number of iterationsis reached.
The notion of "correct in color but not in position" is a bit tricky because color repetitions are allowed. To avoid ambiguity, here is a possible feedback implementation:
int[] feedback(int[] code, int[] guess) {
int[] vCode = new int[C]; // count vector of colors in code
int[] vGuess = new int[C]; // count vector of colors in guess
int black = 0;
for (int i = 0; i < N; i++) {
if (code[i] == guess[i]) {
black++;
} else {
vCode[code[i]]++;
vGuess[guess[i]]++;
}
}
int white = 0;
for (int c = 0; c < C; c++) {
white += min(vCode[c], vGuess[c]);
}
int[] f = {black, white};
return f;
}