[Newbies] Smalltalk is a Mystery to Me
tblanchard at mac.com
Sat Aug 23 03:35:43 UTC 2008
On Aug 21, 2008, at 5:30 PM, Tcykgreis at aol.com wrote:
> I try to initialize by filling a byteArray with 52 numbers, 0
> through 51. I tended to create additional methods to shuffle and
> deal the cards to four more byte arrays named north, east, south,
> and west. Eventually I will need another method to "stack the deck."
> I will also need a counter to keep track of the deal number.
Fundamentally, I would say you are working at too low of a level.
Using numbers to represent cards is an implementation detail you used
in your previous implementations, but in Smalltalk we work at a higher
A deck, hand, pile, all can be implemented as just an
OrderedCollection. You might start with a Deal or BridgeRound or
something. It might have some methods like:
| hands deck |
deck := Card bridgeDeck shuffled asOrderedCollection. "just like
north := OrderedCollection new.
south := OrderedCollection new.
east := OrderedCollection new.
west := OrderedCollection new.
trick := OrderedCollection new.
"deal the whole deck"
hands := (Array with: north with: east with: south with: west).
[deck isEmpty] whileFalse:
[hands do: [:hand | deck ifNotEmptyDo: [:d | hand add: d
which sets up all the collections of cards in a typical bridge deal.
trick is the middle pile in the table. Maybe you need it, maybe not.
Card is an object that contains your numerical value and has some
methods that return the suit and rank. A class method could return an
array of 52.
^(Interval from: 0 to: 51) collect: [:i | self code: i]
^self new code: aNumber
some useful instance methods on card might be
^(code // 4) + 1
^(code \\ 4) + 1
| ranks suits |
ranks := #( Ace 2 3 4 5 6 7 8 9 10 Jack Queen King ).
suits := #( Clubs Hearts Diamonds Spades ).
nextPutAll: (ranks at: self rank) asString;
nextPutAll: ' of ';
nextPutAll: (suits at: self suit) asString.
which will cause each card to be displayed in the form 'Ace of Clubs'
Down the road, maybe you want to keep more information about players,
like what tricks they've taken, so replace the OrderedCollections with
a BridgePlayer with more instance variables etc...
Hopefully that will get you going.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners