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


 
Module PlayCards {
      Font "Arial"  ' Ensure characters exist for Suits
      Cls 15,0
      Pen 0
      Inventory Suits = "♠":=0, "♥":=4, "♦":=4, "♣":=0 'suit -> color
      Inventory Cards = "two":=2, "three":=3, "four":=4, "five":=5
      Append Cards, "six":=6, "seven":=7, "eight":=8, "nine":=9
      Append Cards, "ten":=10, "jack":=10, "queen":=10, "king":=10, "ace":=1
      DealerMoney=0
      PrintCardOnly = Lambda Suits, Cards (k, nl=True)-> {
                For k {
                      Pen Suits(.suit!) {
                              If nl then {
                                    Print Part @(10), Eval$(Suits, .suit)+Eval$(Cards, .card)
                                    Print
                              } Else Print Eval$(Suits, .suit)+Eval$(Cards, .card),
                         }
              }
      }
      ' Using a stack object
      StackPack = Stack
      Module AppendArray (N, A) {
            Stack N {Data !A}
      }
      Class OneCard {
            suit=-1, Card
      Class:
           Module OneCard {
                 \\ ? for optional reading
                 read ? .suit, .card
           }
      }
      Decks=1
      Dim Pack(Len(Cards)*Len(Suits)*Decks) 
      k=0
      \\ fill cards to Pack()
      For times=1 To Decks {
            N=each(Suits)
            While N {
                  M=each(Cards)
                  While M {
                        Pack(k)=OneCard(N^, M^)
                        k++
                  }
            }     
      }
      DisplayAll() ' in order
      Suffle()
      DisplayAll() ' at random positions
      Print
      Card=OneCard()
      Print "Cards in Deck:";Len(StackPack)
      For i=1 to 60 {
            NextCard()
            Print "Get Card:";
            Call PrintCardOnly(Card)
            Print
            Print "Cards in Deck:";Len(StackPack)
            DisplayDeck()
            Print
      }
 
      Sub Suffle()
            Print
            Local N=Len(Pack())-1, N2, i, j, total=N*4+4, cur=1
            For j=1 To 4 {
                  For i=0 To N {
                        If cur Mod 4=3 Then Print Over format$("Suffle {0:0}%",cur/total*100)
                        N2=random(0, N)
                        While N2=i {N2=random(0, N)}
                        Swap Pack(i), Pack(N2)
                        cur++
                  }
            }
            AppendArray StackPack, Pack()
            Print
      End Sub
      Sub DisplayDeck()
            local m=each(StackPack)
            While m  {
                      Call PrintCardOnly(StackItem(m), False)
            }
      End Sub
      Sub DisplayAll()
            For k=0 To Len(Pack())-1 {
                  PrintCard(k)
            }
      End Sub
      Sub PrintCard(k)
            For Pack(k) {
                  Pen Suits(.suit!) {
                        Print Eval$(Suits, .suit)+Eval$(Cards, .card),
                  }
             }
      End Sub
      Sub NextCard()
                  If Len(StackPack)=0 Then {
                        Suffle()
                        Stack StackPack {
                              Drop Random(0, 51)
                        }
                  }
                  Stack StackPack {
                        Read Card
                  }
      End Sub
}
PlayCards
 