Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,103 @@
Object subclass: #Card
instanceVariableNames: 'thePip theSuit'
classVariableNames: 'pips suits'
poolDictionaries: ''
category: nil !
!Card class methods!
initialize
suits ifNil: [ suits := 'clubs,hearts,spades,diamonds' subStrings: $, ].
pips ifNil: [ pips := '2,3,4,5,6,7,8,9,10,jack,queen,king,ace' subStrings: $, ]
!
new
| o |
o := super new.
^o
!
new: card
| o |
o := self new.
o initWithPip: (card at: 1) andSuit: (card at: 2).
^o
!!
!Card class methods !
pips
Card initialize.
^pips
!
suits
Card initialize.
^suits
!!
!Card methods!
initWithPip: aPip andSuit: aSuit
( (pips includes: aPip asLowercase) &
(suits includes: aSuit asLowercase) )
ifTrue: [
thePip := aPip copy.
theSuit := aSuit copy
] ifFalse: [ 'Unknown pip or suit' displayOn: stderr .
Character nl displayOn: stderr ].
^self
!
asString
^('(%1,%2)' % { thePip . theSuit })
!
display
self asString display
!
displayNl
self display.
Character nl display
!!
Object subclass: #Deck
instanceVariableNames: 'deck'
classVariableNames: ''
poolDictionaries: ''
category: nil !
!Deck class methods !
new
|d|
d := super new.
d init.
^d
!!
!Deck methods !
init
deck := OrderedCollection new.
Card suits do: [ :suit |
Card pips do: [ :pip |
deck add: (Card new: { pip . suit })
]
]
!
deck
^deck
!
shuffle
1 to: self deck size do: [ :i |
|r2 o|
r2 := Random between: 1 and: self deck size.
o := self deck at: i.
self deck at: i put: (self deck at: r2).
self deck at: r2 put: o
].
^self
!
display
self deck do: [ :card |
card displayNl
]
!
deal
^self deck removeFirst
!!
"create a deck, shuffle it, remove the first card and display it"
Deck new shuffle deal displayNl.

View 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]
]

View 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' )