## Case Study CS00008: The Set Game

The problem: Find valid sets in a dealt hand: This problem is referred to as the Set Game. It is described at the Set Enterprises website. An APL solution is given at Jim Weigang's page. Basically there are four types of sets each containing three items. They form a deck of 81 cards which are shuffled and 12 are dealt to the table. The object is for participants to find a way of mating up three cards such that attributes are all different or all the same.

The solution:

1. Create the deck of cards and shuffle it.
2. Take 12 cards into a hand
3. Find all the possible combinations of 12 cards taken 3 at a time.
4. For each combination select where attribute counts are all not 2.
5. Display the winning sets.

Note: You can cut and paste these code fragments into the code pane of the Glee interpreter and experiment as you go along to see the actual operations live.

The Glee code:

\$\$ 4 different symbols on each card
'Red'   'Green'     'Purple'    =>color;
'Ovals' 'Squiggles' 'Diamonds'  =>symbol;
'One'   'Two'       'Three'     =>number;

12=>CardsPerHand; 3=> CardsPerSet;

\$\$ Create the 81 card deck; shuffle it; and deal a hand
0..80+81 %< 3  #ea"
->4+1=>i;
" =>deck;

deck[deck# %%(deck#)?100 ``>] =>deck;
deck <- CardsPerHand =>hand;

hand[CardsPerHand ! CardsPerSet]=>sets;
sets[sets #ea "< ``& /# ^| 2 ~"<] => valid;
"Hand:"\$; (hand >>> ,,)\$; "-"%%30\$;"Sets:"\$; (valid  ,,)\$;

The Output:
Hand:
Green Ovals Three Striped
Green Squiggles Three Open
Green Squiggles Two Solid
Purple Diamonds One Open
Purple Diamonds One Striped
Purple Diamonds Two Open
Purple Ovals One Solid
Purple Ovals Three Solid
Purple Ovals Three Striped
Purple Squiggles One Striped
Red Diamonds One Open
Red Squiggles Two Open
--------------------------------
Sets:
Red Diamonds One Open
Purple Ovals Three Striped
Green Squiggles Two Solid

Purple Ovals One Solid
Purple Squiggles One Striped
Purple Diamonds One Open

Purple Squiggles One Striped
Purple Diamonds Two Open
Purple Ovals Three Solid

The play-by-play:

1. \$\$ 4 different symbols on each card
'Red'   'Green'     'Purple'   =>color;
'Ovals' 'Squiggles' 'Diamonds' =>symbol;
'One'   'Two'       'Three'    =>number;

Define four variables to store the three instances each of color, symbol, number and shading. These are just 3 element sequences of strings
2. 12=>CardsPerHand; 3=> CardsPerSet;
An unnecessary step to make the code a little more readable (and changable).
3. \$\$ Create the 81 card deck; shuffle it; and deal a hand
0..80+81 %< 3  #ea"
->4+1=>i;