An unordered_map stores key-value pairs and locates values through a hash table. In a matching-pennies mind reader, it records game states so the computer can predict a later choice. section 8.0
Use an unordered_map by understanding three cooperating parts:
- Buckets hold elements, while a hash function selects the bucket associated with a key. Collisions are allowed, but a better hash can improve performance. section 8.2.1
- The container template takes key, value, hash, and key-equality types; std::hash and std::equal_to provide the defaults. section 8.2.1
- A user-defined key must support equality and normally needs a hash implementation. A tuple key can use equality directly, but requires a custom std::hash specialization. section 8.2.1
A state table for matching pennies
Represent each state as {penultimate outcome, choice, last outcome}, then map it to the player’s last two choices. An initial table can contain all eight combinations, each paired with an unset choice pair. As turns accumulate, matching outcomes support a prediction; when the state has no useful matching history, the computer chooses randomly. section 8.2 section 8.2.1
Table 8.1 The eight possible states in the pennies game (view table figure)
Browse the eight possible combinations of the two recorded outcomes and whether the player changed or repeated a choice. section 8.2
