Task

Create a data structure and the associated methods to define and manipulate a deck of   playing cards.

The deck should contain 52 unique cards.

The methods must include the ability to:

   make a new deck
   shuffle (randomize) the deck
   deal from the deck
   print the current contents of a deck 
Each card must have a pip value and a suit value which constitute the unique value of the card.

Related tasks:

 Card shuffles
 Deal cards_for_FreeCell
 War Card_Game
 Poker hand_analyser
 Go Fish


procedure main(arglist)
 
   cards   := 2                                             # cards per hand
   players := 5                                             # players to deal to
 
   write("New deck : ", showcards(D := newcarddeck()))      # create and show a new deck
   write("Shuffled : ", showcards(D := shufflecards(D)))    # shuffle it 
 
   H := list(players) 
   every H[1 to players] := []                              # hands for each player
 
   every ( c := 1 to cards 	) & ( p := 1 to players ) do    
      put(H[p], dealcard(D))                                # deal #players hands of #cards
 
   every write("Player #",p := 1 to players,"'s hand : ",showcards(H[p]))
   write("Remaining: ",showcards(D))                        # show the rest of the deck
end
 
record card(suit,pip)                                       #: datatype for a card suit x pip
 
procedure newcarddeck()			                    #: return a new standard deck
local D
 
   every put(D := [], card(suits(),pips()))
   return D
end
 
procedure suits()				            #: generate suits
   suspend !["H","S","D","C"]
end
 
procedure pips()					    #: generate pips
   suspend !["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
end
 
procedure shufflecards(D)			            #: shuffle a list of cards
   every !D :=: ?D                                          # see INL#9
   return D
end
 
procedure dealcard(D)				            #: deal a card (from the top)
   return get(D)
end
 
procedure showcards(D)			                    #: return a string of all cards in the given list (deck/hand/etc.)
local s
   every (s := "") ||:= card2string(!D) || " " 
   return s
end
 
procedure card2string(x)			            #: return a string version of a card
   return x.pip || x.suit
end