## Case Study CS00006: Deal Card Hands

The problem: Shuffle a deck of playing cards and deal them into hands

The solution:

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:

\$\$Set hand size and number of hands
5=>CardsPerHand; 4=>Hands;

\$\$Create unshuffled cards
:for('HSDC'[.suit]){
:for('23456789XJQKA'[.card]){
suit card <}
}< =>Cards;

\$\$Shuffle the cards
Cards[52 *-> 52? ``>]=>Deck;

\$\$Set sorting sequence
'23456789XJQKACDHS'=> #csglee;

\$\$Deal the hands to the card table
:for(Hands`[.HandNum]){
Deck<-(Deck# << CardsPerHand)=>hand;
Deck<-(hand# -)=>Deck;
hand @& >%< >>> =>hand;
'Hand ' HandNum ': ' (hand,,) %*}=>Table;

\$\$Show the card table
Table,,\\$;

\$\$Reset the sorting sequence to default
'' => #csglee;

The Output:
Hand 1: 2D 6H 9S XC KC
Hand 2: 3S 4H 6C JC QC
Hand 3: 3H 4D 5S KS AS
Hand 4: 3C 7S 8H JD JH

The play-by-play:

1. 5=>CardsPerHand; 4=>Hands;
Accept input parameters into appropriately named variables
2. :for('HSDC'[.suit]){
:for('23456789XJQKA'[.card]){
suit card <}
}< =>Cards;

This double :for loop first takes the suits :for('HSDC'[.suit]){and then the cards :for('23456789XJQKA'[.card]){ and pairs them up side by side suit card <}. This creates a two item sequence of strings which I convert to a single string by < disclosing it. The result is a sequence of cards comprising a suit. I < disclose it because I just want the cards (not sequences of suits). I assign this result to cards =>Cards; which becomes my deck.
3. I shuffle the Cards[52 *-> 52? ``>] . into =>Deck;
4. '23456789XJQKACDHS'=> #csglee;sets the GLEE sorting (collating) sequence appropriately for playing cards and card suits.

5. :for(Hands`[.HandNum]){
Deck<-(Deck# << CardsPerHand)=>hand;
Deck<-(hand# -)=>Deck;
hand @& >%< >>> =>hand;
'Hand ' HandNum ': ' (hand,,) %*}=>Table;
Table ,,\ \$;
'' => #csglee;

:for(Hands`[.HandNum]){
begins a :for loop. The iterator is formed by using the number of Hands and applying the ` index generator to produce the integers from 1 to the number of hands. Indexing this with a field [.HandNum] produces a namespace that is then passed into the block. This namespace is copied into the block's local namespace. For each iteration, :for creates the next value of HandNum in the block's local namespace. This is used in forming the table display. Deck<-(Deck# << CardsPerHand) => hand; takes the smaller of the number of cards in the deck or the CardsPerHand from the front of the deck and assigns it to hand. Deck<-(hand# -)=>Deck; drops that hand off of the front of the deck. hand @& >%< >>> =>hand; reverses the number and suit in each card label in the hand and then sorts the hand in ascending order (according to the special Glee collating sequence we set earlier). 'Hand ' HandNum ': ' (hand,,) %*}=>Table; formats the output of each hand on the table. Table ,,\ \$; displays the hands on the table. Finally '' => #csglee; sets the collating sequence back to default.

This completes the example. To better understand these operators and other things you can do with them, consult the operator pages according to the type of data you see being operated on.