Enumerations
Let's assume you have been given the task of writing a subsystem for a game of cards (like bridge, for example). Your task is to determine if a given card from the deck was used already. That means you need some means to represent the deck of cards and remember for each card if it was used or not. A deck of cards consists of 52 cards divided into 4 suits (clubs, diamonds, hearts, and spades). Each suit consists of the cards from 2 to 10, the Jack, Queen, King, and Ace of the suit.
To store the fact that a card was used you can use an array of 52 Boolean values. Or even better (more intuitive) you could use a two-dimensional array of 4 rows (one per suit) and 13 columns (one for each card in the suit). Deciding that you use the latter ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access