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


DECLARE SUB setInitialValues (deck() AS STRING * 2)
DECLARE SUB shuffle (deck() AS STRING * 2)
DECLARE SUB showDeck (deck() AS STRING * 2)
DECLARE FUNCTION deal$ (deck() AS STRING * 2)
 
DATA "AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS"
DATA "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH"
DATA "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC"
DATA "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD"
 
RANDOMIZE TIMER
 
REDIM cards(51) AS STRING * 2
REDIM cards2(51) AS STRING * 2
 
setInitialValues cards()
setInitialValues cards2()
shuffle cards()
PRINT "Dealt: "; deal$(cards())
PRINT "Dealt: "; deal$(cards())
PRINT "Dealt: "; deal$(cards())
PRINT "Dealt: "; deal$(cards())
showDeck cards()
showDeck cards2()
 
FUNCTION deal$ (deck() AS STRING * 2)
    'technically dealing from the BOTTOM of the deck... whatever
    DIM c AS STRING * 2
    c = deck(UBOUND(deck))
    REDIM PRESERVE deck(LBOUND(deck) TO UBOUND(deck) - 1) AS STRING * 2
    deal$ = c
END FUNCTION
 
SUB setInitialValues (deck() AS STRING * 2)
    DIM L0 AS INTEGER
 
    RESTORE
    FOR L0 = 0 TO 51
        READ deck(L0)
    NEXT
END SUB
 
SUB showDeck (deck() AS STRING * 2)
    FOR L% = LBOUND(deck) TO UBOUND(deck)
        PRINT deck(L%); " ";
    NEXT
    PRINT
END SUB
 
SUB shuffle (deck() AS STRING * 2)
    DIM w AS INTEGER
    DIM shuffled(51) AS STRING * 2
    DIM L0 AS INTEGER
 
    FOR L0 = 51 TO 0 STEP -1
        w = INT(RND * (L0 + 1))
        shuffled(L0) = deck(w)
        IF w <> L0 THEN deck(w) = deck(L0)
    NEXT
 
    FOR L0 = 0 TO 51
        deck(L0) = shuffled(L0)
    NEXT
END SUB