🗜️

Develop - Core Algorithm

Any given card in Set has four attributes each with three different possible values. This means there are 81 different options. In our program, each unique card has a unique ID that has all the information about the attributes of the card. An ID is a number between 0 and 80, however, it is stored as a base-3 (also known as ternary) integer instead of normal base 10. This means every ID is a 4-digit base-3 integer between 0000 and 2222. Each digit in this number corresponds to a different attribute, and the value of that digit is the value of that attribute. See the below table for reference. Each column is in order and represents one digit in an ID. For example, a card with three solid green diamonds would have an ID of 0022.

Card reference table

How to judge a set

As mentioned above, each card has a unique index between base_3 0000 to 2222 according to their attribute. So, after adding 3 cards, the range of the sum is 0000 to 6666. If each attribute is the same (e.x. three green cards), the sum of the color attribute will be 0 + 0 + 0 = 0. Similarly, same attribute can also result in 1 + 1 + 1 = 3, or 2 + 2 + 2 = 6. If all values of an attribute are different, the sum of that digit will become 0 + 1 + 2 = 3. So, if 3 cards can form a set. The sum of their base_3 values can only have digits 0, 3, and 6. (e.x. 0303, 0633, 3000, …) Therefore, by counting whether the number of the sum has four “0, 3, or 6”, we can judge whether the three cards can form a set.

The Deck Object

The game of Set comes with one deck of 81 unique cards. A Deck object houses a deck of Set cards. When a Deck is initialized, an 81-element array of integers where the value of the array at index i is equal to i. The important method in the Deck object is `draw`.

The command-line version no longer available. Relevant coding (core algorithm) is archived at 📔Develop - CMD Line Archive