Playing cards: Difference between revisions

Line 1,106:
format('~a of ~a~n', [Pip, Suit]).
</lang>
=={{header|PureBasic}}==
This approach keeps track of the cards in an abbrieviated form but allows them to be expanded to a more wordy form when they are dealt or shown.
<lang PureBasic>#MaxCards = 52 ;Max Cards in a deck
Structure card
pip.s
suit.s
EndStructure
 
Structure _membersDeckClass
*vtable.i
Size.i ;# of cards present
cards.card[#MaxCards] ;deck content
EndStructure
 
Interface DeckObject
Init()
shuffle()
deal.s(isAbbr = #True)
show(isAbbr = #True)
EndInterface
 
Procedure.s _formatCardInfo(*card.card, isAbbr = #True)
;isAbbr determines if the card information is abbrieviated to 2 characters
Static pips.s = "2 3 4 5 6 7 8 9 10 Jack Queen King Ace"
Static suits.s = "Diamonds Clubs Hearts Spades"
Protected c.s
If isAbbr
c = *card\pip + *card\suit
Else
c = StringField(pips,FindString("23456789TJQKA", *card\pip, 1), " ") + " of "
c + StringField(suits,FindString("DCHS", *card\suit, 1)," ")
EndIf
ProcedureReturn c
EndProcedure
 
Procedure setInitialValues(*this._membersDeckClass)
Protected i, c.s
Restore cardDat
For i = 0 To #MaxCards - 1
Read.s c
*this\cards[i]\pip = Left(c, 1)
*this\cards[i]\suit = Right(c, 1)
Next
EndProcedure
 
Procedure.s dealCard(*this._membersDeckClass, isAbbr)
;isAbbr is #True if the card dealt is abbrieviated to 2 characters
Protected c.card
c = *this\cards[*this\Size]
*this\Size - 1
ProcedureReturn _formatCardInfo(@c, isAbbr)
EndProcedure
 
Procedure showDeck(*this._membersDeckClass, isAbbr)
;isAbbr determines if cards are shown with 2 character abbrieviations
Protected i
For i = 0 To *this\Size
Print(_formatCardInfo(@*this\cards[i], isAbbr))
If i <> *this\Size: Print(", "): EndIf
Next
PrintN("")
EndProcedure
 
Procedure shuffle(*this._membersDeckClass)
;works with decks of any size
Protected w, i
Dim shuffled.card(*this\Size)
For i = *this\Size To 0 Step -1
w = Random(i)
shuffled(i) = *this\cards[w]
If w <> i
*this\cards[w] = *this\cards[i]
EndIf
Next
For i = 0 To *this\Size
*this\cards[i] = shuffled(i)
Next
EndProcedure
 
Procedure newDeck()
Protected *newDeck._membersDeckClass = AllocateMemory(SizeOf(_membersDeckClass))
If *newDeck
*newDeck\vtable = ?vTable_deckClass
*newDeck\Size = #MaxCards - 1
setInitialValues(*newDeck)
EndIf
ProcedureReturn *newDeck
EndProcedure
 
DataSection
vTable_deckClass:
Data.i @setInitialValues()
Data.i @shuffle()
Data.i @dealCard()
Data.i @showDeck()
cardDat:
Data.s "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "TD", "JD", "QD", "KD", "AD"
Data.s "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "TC", "JC", "QC", "KC", "AC"
Data.s "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "TH", "JH", "QH", "KH", "AH"
Data.s "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "TS", "JS", "QS", "KS", "AS"
EndDataSection
 
OpenConsole()
 
Define deck.DeckObject = newDeck()
Define deck2.DeckObject = newDeck()
 
If deck = 0 Or deck2 = 0
PrintN("Unable to create decks")
End
EndIf
 
deck\shuffle()
PrintN("Dealt: " + deck\deal(#False))
PrintN("Dealt: " + deck\deal(#False))
PrintN("Dealt: " + deck\deal(#False))
PrintN("Dealt: " + deck\deal(#False))
deck\show()
deck2\show()
 
Repeat: Until Inkey() <> ""</lang>
Sample output:
<pre>Dealt: Queen of Hearts
Dealt: 6 of Hearts
Dealt: 6 of Diamonds
Dealt: 2 of Spades
5D, QS, 9C, 7D, 3S, TC, 6S, 8H, 3D, KH, 2D, 2H, 5S, 4D, 4H, KS, 6C, 9S, KD, JH,
3C, TD, 4C, AH, JD, 8S, 3H, AS, QC, 4S, 8D, AD, 5H, 9H, 7C, 8C, 9D, TH, 5C, JS,
7S, TS, QD, JC, 2C, AC, 7H, KC
2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, TD, JD, QD, KD, AD, 2C, 3C, 4C, 5C, 6C, 7C, 8C,
9C, TC, JC, QC, KC, AC, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, TH, JH, QH, KH, AH, 2S,
3S, 4S, 5S, 6S, 7S, 8S, 9S, TS, JS, QS, KS, AS</pre>
 
=={{header|Python}}==
Anonymous user