liveBook · Manning

State table

As explained in Learn C++ by Example

A state table is a table that associates a finite game-history state with recorded outcomes or predictions.

A state table associates a finite game-history state with recorded outcomes or predictions. In the pennies game, each state records the penultimate outcome, whether the player changed choices, and the last outcome; its value stores the player’s two most recent choice patterns. section 8.2

The table supports prediction by following a small update-and-lookup cycle:

  • Initialize the eight valid states with unset outcomes, shrug choices, and unset outcomes. section 8.2
  • Look up the current state; if it is absent during warmup, return two Shrugs so the game makes a random choice. section 8.2.2
  • For a valid state, shift the previous second choice forward and record whether the latest turn stayed the same or changed. section 8.2.2
  • Treat matching stored choices as a prediction; treat different choices as uncertainty and choose randomly. section 8.2.2

Always choosing heads

If a player always chooses heads, only four of the eight rows become populated because the choice remains Same. The matching stored choices eventually let the machine predict Same; a player who changes every turn similarly fills the other four rows with Changes. section 8.2

Table 8.1 The eight possible states in the pennies game (view table figure)

Penultimate outcome

Choice

Last outcome

Lose

Same

Lose

Lose

Same

Win

Lose

Change

Lose

Lose

Change

Win

Win

Same

Lose

Win

Same

Win

Win

Change

Lose

Win

Change

Win

Browse the eight possible combinations of penultimate outcome, choice change, and last outcome that form the table’s keys. section 8.2

A C++ implementation represents the key as a tuple of Outcome, Choice, and Outcome, and stores each key with a pair of recent choices in an unordered_map. A State class separates lookup from updating, while the mind reader updates the state after every turn. section 8.2.2 section 8.2.3

How Learn C++ by Example covers it

How deep it goes
Defines eight states and explains lookup, warmup, prediction, and incremental updates without exploring unordered-map internals.
Examples
Uses a matching-pennies mind reader, repeated heads, alternating choices, eight state rows, and incremental C++ listings.
What it assumes
Assumes a game loop and player choices while teaching state encoding, warmup handling, and table updates through the example.
What you can do after
Build it from scratch as a lookup-driven predictor that updates after each game turn.
How it connects
Connects game history, unordered maps, tuples, prediction, and random choice.

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.