liveBook · Manning

Mind reader

As explained in Learn C++ by Example

A mind reader is a game component that predicts a player's next binary choice from the player's previous choices and game state.

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

How Learn C++ by Example covers it

How deep it goes
Explains state lookup, warmup handling, updates, prediction branches, and random fallback, stopping short of broader learning algorithms.
Examples
Demonstrates a 0-or-1 guessing game with state-table warmup, repeat-or-change patterns, and counted wins.
What you can do after
Build it from scratch as a tested C++ game component with injectable random generation and incremental state updates.
How it connects
Connects state tracking with unordered maps, random number generation, templates, and bitwise XOR.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.