[Newbies] Smalltalk is a Mystery to Me

Todd Blanchard 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  
level.

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:

initialize
	| hands deck |
	deck := Card bridgeDeck shuffled asOrderedCollection. "just like  
english"
	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  
removeFirst]]].

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.

bridgeDeck

	^(Interval from: 0 to: 51) collect: [:i | self code: i]

code: aNumber

	^self new code: aNumber

some useful instance methods on card might be

rank
	^(code // 4) + 1

suit
	^(code \\ 4) + 1

printOn: aStream

	| ranks suits |
	ranks := #( Ace 2 3 4 5 6 7 8 9 10 Jack Queen King ).
	suits := #( Clubs Hearts Diamonds Spades ).

	aStream
		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'  
or whatever.

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.

-Todd Blanchard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20080822/f80130c8/attachment-0001.htm


More information about the Beginners mailing list