A mind reader predicts a player's next binary choice from recent choices and game results. It stores short state histories, then predicts whether the player will repeat or change direction. section 8.2.2 section 8.2.3
A prediction cycle works as follows:
- Look up the current state in an unordered map; return two Shrugs when the state is still warming up. section 8.2.2
- Classify the player's turn as Same or Change, update the recent state, and record whether the prediction won or lost. section 8.2.3
- Follow Same or Change predictions, and use a random binary choice when the state provides no reliable pattern. section 8.2.2 section 8.2.3
A playable guessing game
The player repeatedly enters 0 or 1. The mind reader displays its prediction, compares it with the player's choice, updates its state table, and prepares the next prediction while counting guesses and wins. section 8.2.3
Listing 8.11 A mind-reading class
template <std::invocable<> T, typename U>
class MindReader {
State state_table;
T generator;
U distribution;
int prediction = flip(); #1
state_t state{ #1
Outcome::Unset, #1
Choice::Shrug, #1
Outcome::Unset #1
}; #1#2
int previous_go = -1; #1#2
int flip() #1
{ #1
return distribution(generator); #1
}
public:
MindReader(T gen, U dis)
: generator(gen), distribution(dis)
{
}
int get_prediction() const
{
return prediction;
}
bool update(int player_choice);
};
Browse the C++ MindReader class, including injected randomness, initial state, prediction access, and update operations. section 8.2.3
