Miscellaneous questions...
Ramon Leon
rleon at insario.com
Fri Sep 9 17:28:06 UTC 2005
> Just a few things to throw out here:
>
> -> Still wondering about the Squeakland image; the download
> is less than
> 1MB, so obviously it's not in there (right?).
>
> -> When you're developing classes and testing stuff out, do
> you expose
> -> all
> the instance variables? And then later, remove the accessors
> you don't want the user to have? I notice that to test/debug
> it's often easiest to have access to everything from the
> Workspace, but really don't want that to be exposed to
> clients of the class.
>
> -> If you were creating a deck (of cards) class and a card
> class, would
> you put the card's rank and suit in as strings or as symbols,
> keeping in mind that you were later going to reveal the class
> to students for educational purposes? I know I want the deck
> to be card neutral, i.e., to be usable for a deck of any sort
> of cards, but the cards themselves (for standard decks), for
> convenience, are described as being (rank) "King" and
> (suit) "Hearts". But perhaps that should be #king and
> #hearts. (I tend to worry about making global symbols: seems
> like courting trouble.)
>
> Nothing real urgent, just stuff that has occurred to me.
Funny you should mention this, as one of my first sample programs to
write to familiarize myself with Smalltalk, I did exactly that, was
writing a poker game to see if I could, see how'd I'd do it all OO and
Smalltalk style, so I'll share my Hand, Deck, and Card classes, but I'm
new at Smalltalk, so I hope the code isn't too bad, but I appreciate any
comments on proper Smalltalk style.
Object subclass: #TPCard
instanceVariableNames: 'num suit'
classVariableNames: ''
poolDictionaries: ''
category: 'SentorsaTexasPoker'!
!TPCard commentStamp: '<historical>' prior: 0!
I am a card, I can be any card of any suit.!
!TPCard methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:14'!
asString
^num asString, ' of ', suit! !
!TPCard methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:11'!
num
^num! !
!TPCard methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:53'!
num: aString
num := aString! !
!TPCard methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:11'!
suit
^suit! !
!TPCard methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:54'!
suit: aString
suit := aString! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
TPCard class
instanceVariableNames: ''!
!TPCard class methodsFor: 'instance creation' stamp: 'RJL 7/3/2005
10:10'!
newNum: aNum suit: aSymbol
^self basicNew
initialize;
num: aNum;
suit: aSymbol;
yourself.! !
Object subclass: #TPDeck
instanceVariableNames: 'random deck'
classVariableNames: ''
poolDictionaries: ''
category: 'SentorsaTexasPoker'!
!TPDeck commentStamp: '<historical>' prior: 0!
I am a deck of cards, I can deal cards to a hand and shuffle the deck
randomly!
!TPDeck methodsFor: 'private' stamp: 'RJL 7/30/2005 10:18'!
cardNumbers
"All possible card numbers."
^#(#One #Two #Three #Four #Five #Six #Seven #Eight #Nine #Ten
#Jack #Queen #King)! !
!TPDeck methodsFor: 'private' stamp: 'RJL 7/30/2005 10:19'!
cardSuits
"All possible card suits."
^#(#Clubs #Hearts #Spades #Diamonds)! !
!TPDeck methodsFor: 'accessing' stamp: 'RJL 7/30/2005 10:19'!
dealCard
"Pull a random card out of the deck."
^deck removeAt: (random nextInt: deck size).
! !
!TPDeck methodsFor: 'accessing' stamp: 'RJL 7/30/2005 10:20'!
dealToHand: aHand cards: aNum
"Deal a given number of cards to a hand."
aNum timesRepeat: [aHand addCard: self dealCard].
aHand rank.
! !
!TPDeck methodsFor: 'accessing' stamp: 'RJL 7/30/2005 10:21'!
shuffle
"Create a new deck, generate all cards for it, dealCard deals
random cards from this
deck, so shuffle really only needs to build the deck, not
actually shuffle them"
deck := OrderedCollection new.
self cardNumbers
inject: self cardSuits into: [:suits :card |
suits do: [:suit | deck add: (TPCard newNum:
card suit: suit)]].! !
!TPDeck methodsFor: 'initialize-release' stamp: 'RJL 8/1/2005 12:00'!
initialize
super initialize.
random := Random new.
self shuffle.
! !
Object subclass: #TPHand
instanceVariableNames: 'name cards game rank'
classVariableNames: ''
poolDictionaries: ''
category: 'SentorsaTexasPoker'!
!TPHand commentStamp: '<historical>' prior: 0!
I am a hand, a collection of cards with a rank. I use a game to compare
myself to other hands and decide if I outrank him.!
!TPHand methodsFor: 'accessing' stamp: 'RJL 7/3/2005 10:54'!
addCard: aCard
cards add: aCard.! !
!TPHand methodsFor: 'accessing' stamp: 'RJL 7/4/2005 00:07'!
asString
^ name, ':', String cr, String tab, (cards join: String cr,
String tab)! !
!TPHand methodsFor: 'accessing' stamp: 'RJL 7/3/2005 22:01'!
cards
^cards! !
!TPHand methodsFor: 'accessing' stamp: 'RJL 7/3/2005 14:13'!
game: aGame
game := aGame! !
!TPHand methodsFor: 'accessing' stamp: 'RJL 7/4/2005 01:14'!
name: aString rank: aNum
name := aString.
rank := aNum.! !
!TPHand methodsFor: 'initialize-release' stamp: 'RJL 8/1/2005 12:00'!
initialize
super initialize.
cards := OrderedCollection new.! !
!TPHand methodsFor: 'scoring' stamp: 'RJL 7/3/2005 22:01'!
rank
game rankHand: self.! !
!TPHand methodsFor: 'comparing' stamp: 'RJL 7/3/2005 14:06'!
< aHand
^ game hand: aHand beats: self! !
!TPHand methodsFor: 'comparing' stamp: 'RJL 7/3/2005 14:12'!
= aHand
^(self > aHand) not and: [(aHand > self) not] ! !
!TPHand methodsFor: 'comparing' stamp: 'RJL 7/3/2005 14:05'!
> aHand
^ game hand: self beats: aHand! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
TPHand class
instanceVariableNames: ''!
!TPHand class methodsFor: 'instance creation' stamp: 'RJL 7/3/2005
14:09'!
newGame: aGame
^self basicNew
initialize;
game: aGame;
yourself.! !
More information about the Squeak-dev
mailing list
|