Playing cards: Difference between revisions

Slightly improve Smalltalk version 2
(Add a second solution in Smalltalk)
(Slightly improve Smalltalk version 2)
Line 5,400:
===Version 2===
{{works with|GNU Smalltalk}}
<lang smalltalk>Object subclass: Deck [
Object subclass: Deck [
 
| cards |
Line 5,411 ⟶ 5,410:
 
initializeWith: aCardClass
[cards := aCardClassOrderedCollection standardDeckfrom: deepCopyaCardClass asOrderedCollectionstandardSet]
 
displayOn: aStream
Line 5,430 ⟶ 5,429:
Object subclass: Card [
 
Card class >> standardDeckstandardSet
[^#(
'2d' '3d' '4d' '5d' '6d' '7d' '8d' '9d' 'Td' 'Jd' 'Qd' 'Kd' 'Ad'
Line 5,436 ⟶ 5,435:
'2h' '3h' '4h' '5h' '6h' '7h' '8h' '9h' 'Th' 'Jh' 'Qh' 'Kh' 'Ah'
'2c' '3c' '4c' '5c' '6c' '7c' '8c' '9c' 'Tc' 'Jc' 'Qc' 'Kc' 'Ac'
) deepCopy]
]</lang>
]
</lang>
 
The Card class should obviously be more intricate to fulfil the needs of a CardGame class. :-)
 
Use example:
<lang Smalltalk>st> myDeck := Deck of: Card
st> myDeck := Deck of: Card
a Deck
st> myDeck displayStringdisplayNl
'2d 3d 4d 5d 6d 7d 8d 9d Td Jd Qd Kd Ad 2s 3s 4s 5s 6s 7s 8s 9s Ts Js Qs Ks As 2h 3h 4h 5h 6h 7h 8h 9h Th Jh Qh Kh Ah 2c 3c 4c 5c 6c 7c 8c 9c Tc Jc Qc Kc Ac'
st> myDeck shuffle
a Deck
st> myDeck displayStringdisplayNl
'6c 7d Ac 4c 9s 2s Tc 9c Jh 3h Kh 7h 3s 5s 3d Kd Jc Qs As Qd 3c Kc Qh 2d 9h 4h 8c 7s Ah 9d Js 6h 8s 8h 5c 2c 4s 8d 5d Ts 4d Qc Td 7c 2h 5h 6s 6d Th Ks Jd Ad'
st> myHand := OrderedCollection new
OrderedCollection ()
Line 5,457 ⟶ 5,454:
5
st> myHand
OrderedCollection ('Ad' 'Jd' 'Ks' 'Th' '6d' )</lang>
</lang>
 
=={{header|Swift}}==
Anonymous user