Your job is to take the OOA and OOD that makes up the design document, and implement the system. The interface to the various classes should be as specified, but how you implement the functionality (what the classes and objects do) is up to you. To make life a bit less uncertain, The Donald provided a likely scenario for you to try out your code with.
The system deals with three types of objects: Card, CardDeck, and Player. Using the first-person language, the objects would describe themselves this way:
suit: rank:
message it creates a new one of me, and sets my suit and rank
attributes to the supplied values. I also know my suit
and rank, and can tell them when asked.
initialize message it creates a
new batch of cards and remembers them in the
InitialCardDeck class variable. My class also knows how
to create a new one of me, and does so when it receives a
new message. I deal the card on top when I get the
next message. When I'm told to return: a
card, I place it at the bottom of my pile. I know how to
shuffle myself, which randomizes the order of my cards.
I also can tell how many cards I hold, and will say whether I'm empty
when I get the isEmpty message.
new one of me. I know how to take: a card
and add it to my hand. I also know how to returnAll: of
my cards to a deck of cards. When asked for my points I
sum the ranks of all the cards I currently hold and answer the total.
SampleSpaceWithReplacement and
SampleSpaceWithoutReplacement give the ability to
repeatedly select something at random from a Collection, with and
without replacement, respectively. Of course, you can only select
from a Collection without replacement as many times as there are
elements in the collection.
File in the code you found in the above link, and try out the
following to get a feel for how the classes work:
| die | die := SampleSpaceWithReplacement data: (1 to: 6). 1 to: 10 do: [ :i | Transcript show: (die next printStringRadix: 10); show: ' ' ]. Transcript cr. | urn | urn := SampleSpaceWithoutReplacement data: #(red red red blue blue). 1 to: (urn size) do: [ :i | Transcript show: 'Size = '; show: (urn size printStringRadix: 10). Transcript show: ' Selected = '; show: (urn next); cr ]. Transcript cr.What happens when you try to select from an empty urn?
cards
instance variable. The class also needs to be
initialized before being used for the first time. Check
out the example in the next section. Notice that
InitialCardDeck is capitalized. That implies that it is
a class variable. A class variable is a variable associated
with an entire class rather than an instance of that class. (Sort of
like the shared segment of an executable). The class variable is used
to store an initial card deck, which can be copied to new instances of
the CardDeck class rather than creating it from scratch
each time. Objects can understand the copy message,
which returns a copy of an object. Use this to copy the
InitialCardDeck to new instances of the class. Read up on class
variables if you're still a bit confused.
cards
instance variable to an empty OrderedCollection. This is
useful when creating a new player, and when the player returns all
their cards to the deck.
printOn: aTextCollector "Write my value on the TextCollector argument." aTextCollector show: 'Suit = ', suit. aTextCollector show: ' Rank = ', (rank printStringRadix: 10). aTextCollector cr. aTextCollector flush.To the Player class, add the following method for showing the player's hand:
showHand Transcript show: ' Read em and weep...'. Transcript show: (self points printStringRadix: 10), ' points!'; cr. cards do: [ :eachCard | eachCard printOn: Transcript ].
| cardDeck cards | cardDeck := OrderedCollection new: 52. #(heart club spade diamond) do: [ :eachSuit | 1 to: 13 do: [ :n | cardDeck add: (Card suit: eachSuit rank: n) ]]. cards := SampleSpaceWithoutReplacement data: cardDeck. 1 to: 5 do: [ :i | (cards next) printOn: Transcript ].
| cardDeck | CardDeck initialize. cardDeck := CardDeck new. 13 timesRepeat: [ (cardDeck next) printOn: Transcript ]. cardDeck shuffle. 13 timesRepeat: [ (cardDeck next) printOn: Transcript ].
| players gameCards winners |
players := Set new.
4 timesRepeat: [ players add: Player new ].
gameCards := CardDeck new.
gameCards shuffle.
[ winners := players select: [ :each | each points between: 18 and: 21 ].
winners isEmpty and: [ gameCards isEmpty not ]]
whileTrue:
[ players do:
[ :each | each points < 21 ifTrue: [ each take: gameCards next ]]].
winners do: [ :player | player showHand ].
(1:30-3)
cat output.txt Game.st | mail -s "lab 4 - YOUR NAME" joita@cc.gatech.edu
OR (3-4:30)
cat output.txt Game.st | mail -s "lab 2 - YOUR NAME" jyan@cc.gatech.edu
OR (4:30-6)
cat output.txt Game.st | mail -s "lab 2 - YOUR NAME" smk@cc.gatech.edu
That's all for this lab. Please quit from VisualWorks before you logout.