[Newbies] Re: Smalltalk is a Mystery to Me

nicolas cellier ncellier at ifrance.com
Fri Aug 22 17:18:48 UTC 2008

Tcykgreis at aol.com a écrit :
> I have had a curiosity about Smalltalk for many years so I recently 
> downloaded and installed Squeak. That's when the trouble began. I have 
> written applications that deal bridge hands and either display the hands 
> on screen or save them in a couple of different formats. I originally 
> wrote the 'words' in Forth. I later tried Ruby and rewrote most of the 
> programs in Ruby. I did it as a learning experience. I sat out to do the 
> same thing in Squeak, again as a learning experience, but have made 
> virtually no progress. I create the class 'Bridge' with the subclass of 
> dealer. 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.
> I can't get started, and I mean zero progress, because I can't create 
> and load deck. It seems like the documentation is never quit up to date. 
> I read about curly braces and tried deck := {0. 1. 2. ... }. When I try 
> to accept it, first deck is questioned and then after deck I get 
> something about not expecting anything else.

What kind of Object is a Bridge? to me it sounds more like a namespace 
than an Object.
What kind of object is a Dealer? Maybe you mean a Deal?
What are the instance variable of your class?
What are its main methods (the messages than Dealer will respond to) you 
want to implement?

You really have to think in term of Objects and messages, otherwise your 
Smalltalk experience won't be that nice.

Maybe you want to create an initialize method in Dealer, assuming deck 
is an instance variable:

	deck := (1 to: 52) asOrderedCollection shuffled.

Then, when you create a Dealer object (Dealer new), its instance 
variable will be initialized.

> I know there is a word 'asByteArray:' and I assume a number would 
> specify the size of the array but nowhere can I find anything about the 
> order in which the information should be provided. I tried deck 
> asByteArray: 52 but I don't know if it worked. If it did work, how do I 
> load the bytes into it? How do I look at a byte in a particular location 
> in the array? Can I remove a byte from position x and/or insert a byte 
> at position y and everything moves to accommodate the change.

| ba |
ba := ByteArray new: 52. "Create a ByteArray with 52 bytes"
ba at: 5 put: 23. "set the fith element to 23"

(1 to: 52) asByteArray. "Try to evaluate this..."

(1 to: 52) asByteArray shuffled. "That might be of interest"

If you want to insert and remove, I recommend you begin with an 
OrderedCollection. OrderedCollection are growable and support adding and 
removing elements easily (you'll have to browse existing messages or get 
help from Squeak by example book which is excellent for beginners).

An Array has a fixed size and must be copied if you want to 
insert/remove elements.
You can inquire about #copyReplaceFrom:to:with:

ba := ba copyReplaceFrom: 3 to: 4 with: ByteArray new. "remove third and 
fourth elements"

ba := ba copyReplaceFrom: 3 to: 2 with: {11. 22.} asByteArray. "insert 
11 and 22 at third and fourth position"

> In Forth and Ruby, I was able to store the hands as a 2D bit array, 4 
> suits and 13 bits. If the card was present the bit was set. When I dealt 
> the cards, the appropriate bits were set.. This worked really well. The 
> suits came out already sorted. The strength of a suit turned out to be 
> related to the value stored for the suit. The number of cards in the 
> suit could be found by counting set bits. I have yet to find 
> bit-manipulating words in Squeak/Smalltalk.
> As an aside, the least number of bits that must be used to store a 
> complete deal is 104 or 13 bytes. The bits are arranged in 52 2-bit 
> groups. The position in the array represents the value of the card and 
> the bits determine which hands gets the card represented by that 
> position. When you shuffle the 2-bit groups must be kept in tact. I 
> could easily do this in Forth but could not do it in Ruby. If you are 
> going to save a few million hands, it is nice to be able to do so in 
> this most compact form.

I strongly recommend you write a non optimized version to begin with.
Squeak has enough support for collection to shuffle sort add remove 
select reject etc...

Then only when you get used to objects and messages, you should inquire 
about optimizing space with bits in bit arrays.

Note that it's absolutely doable in Squeak, but you'll may have to write 
some of the base methods. I think Yoshiki Oshima have quite an advanced 
implementation of BitArray, take a look at SqueakMap (from World menu 

> If I could just figure out where to find the answers to these beginner 
> question, I would really appreciate it. It would also be nice if I could 
> see some examples of these methods.
> Charlie

Maybe a look at free book Squeak by Example would be a good start to get 
acquainted to Smalltalk.


> ------------------------------------------------------------------------
> It's only a deal if it's where /you/ want to go. Find your travel deal 
> *here* <http://information.travel.aol.com/deals?ncid=aoltrv00050000000047>.
> ------------------------------------------------------------------------
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

More information about the Beginners mailing list