June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
19
Task/Playing-cards/Factor/playing-cards.factor
Normal file
19
Task/Playing-cards/Factor/playing-cards.factor
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
USING: formatting grouping io kernel math qw random sequences
|
||||
vectors ;
|
||||
IN: rosetta-code.playing-cards
|
||||
|
||||
CONSTANT: pips qw{ A 2 3 4 5 6 7 8 9 10 J Q K }
|
||||
CONSTANT: suits qw{ ♥ ♣ ♦ ♠ }
|
||||
|
||||
: <deck> ( -- vec ) 52 iota >vector ;
|
||||
|
||||
: card>str ( n -- str )
|
||||
13 /mod [ suits nth ] [ pips nth ] bi* prepend ;
|
||||
|
||||
: print-deck ( seq -- )
|
||||
13 group [ [ card>str "%-4s" printf ] each nl ] each ;
|
||||
|
||||
<deck> ! make new deck
|
||||
randomize ! shuffle the deck
|
||||
dup pop drop ! deal from the deck (and discard)
|
||||
print-deck ! print the deck
|
||||
37
Task/Playing-cards/Smalltalk/playing-cards-2.st
Normal file
37
Task/Playing-cards/Smalltalk/playing-cards-2.st
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
Object subclass: Deck [
|
||||
|
||||
| cards |
|
||||
|
||||
Deck class >> of: aCardClass
|
||||
[^self new
|
||||
initializeWith: aCardClass;
|
||||
yourself]
|
||||
|
||||
initializeWith: aCardClass
|
||||
[cards := OrderedCollection from: aCardClass standardSet]
|
||||
|
||||
displayOn: aStream
|
||||
[cards
|
||||
do: [:each | each displayOn: aStream]
|
||||
separatedBy: [aStream space]]
|
||||
|
||||
shuffle
|
||||
[1 to: cards size - 1 do:
|
||||
[:a || b |
|
||||
b := Random between: a and: cards size.
|
||||
cards swap: a with: b]]
|
||||
|
||||
deal
|
||||
[^cards removeLast]
|
||||
]
|
||||
|
||||
Object subclass: Card [
|
||||
|
||||
Card class >> standardSet
|
||||
[^#(
|
||||
'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'
|
||||
) deepCopy]
|
||||
]
|
||||
14
Task/Playing-cards/Smalltalk/playing-cards-3.st
Normal file
14
Task/Playing-cards/Smalltalk/playing-cards-3.st
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
st> myDeck := Deck of: Card
|
||||
a Deck
|
||||
st> myDeck displayNl
|
||||
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 displayNl
|
||||
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 ()
|
||||
st> 5 timesRepeat: [myHand add: myDeck deal]
|
||||
5
|
||||
st> myHand
|
||||
OrderedCollection ('Ad' 'Jd' 'Ks' 'Th' '6d' )
|
||||
Loading…
Add table
Add a link
Reference in a new issue