Smalltalk Assignment: due ?.
For this assignment, you are going to implement that same thing as
you did for
your Java and Scheme projects, the clock patience program. To help you along, we're
giving you the Smalltalk code that takes the input card list and
builds a data structure to hold the results of the deal. You can use
this code as a starting point, or write it yourself if you like.
The code is:
" ***************************************************************
each card is represented as a list. For example,
the queen of spades is #(Q, S)
The hand itself is stored in 13 'stacks' - OrderedCollections, from the
Ace stack to King. To make this searchable, the
name of the stack is stored with the stack. For
example #(5 (K, D) (2 C)) means that the stack associated
with 5 has a K of diamonds on top of a two of clubs.
So, an entire hand might look something like:
#((A (J C) (2 H) (3 S) (4 D)) (2 (5 D) (Q D) (7 H) (2 C)) ... (K ...))
as an OrderedCollection
To run (from Squeak): (FileStream oldFileNamed: 'patience.st') fileIn.
The cards are stacked onto the piles from left to right,
the rightmost card is top
To play:
cards := OrderedCollection new.
cards sample.
cards patience.
Output is to the Transcript window.
*************************************************************** "
! Object methodsFor: 'algorithms' !
patience
"To play: (FileStream oldFileNamed: 'patience.st') fileIn.
cards := OrderedCollection new.
cards sample.
cards patience.
Output is to the Transcript window.
self is the sample deck of cards, an OrderedCollection.
Here the T of spades is hardcoded from pickTopCard #K
- you will need to replace this with whatever is on top of the king's
pile for a given deal. "
| hand |
hand := self deal: #K.
hand play: (hand pickTopCard: #K) move: 1.
!
play: card move: moveNum
"The heart of the program. Self is 'hand', holds the current card
distribution as stacks. 'card' is the current card we are operating on.
"
| nextcard nextrank |
nextrank := card first.
nextcard := self pickTopCard: nextrank.
nextcard size = 0 ifTrue:[
Transcript show: 'Game Over. Card=', card printString.
Transcript show: ', move=', moveNum printString.
Transcript cr.
Transcript show: 'Piles at end:'. Transcript cr.
Transcript show: self printString.
]
ifFalse:[
"nextrank := ?? . "
Transcript show: 'Move:', moveNum printString.
Transcript show: ', nextcard=', nextcard printString.
Transcript show: ', nextrank=', nextrank printString.
Transcript cr.
"recursive call to play goes here"].
!
sample
" Sets an OrderedCollection of cards.
Self is an empty OrderedCollection."
#((T S)(Q C)(8 S)(8 D)(Q H)(2 D)(3 H)(K H)(9 H)(2 H)(T H)(K S)(K C) (9 D)(J H)(7 H)(J D)(2 S)(Q S)(T D)(2 C)(4 H)(5 H)(A D)(4 D)(5 D)
(6 D)(4 S)(9 S)(5 S)(7 S)(J S)(8 H)(3 D)(8 C)(3 S)(4 C)(6 S)(9 C)
(A S)(7 C)(A H)(6 H)(K D)(J C)(7 D)(A C)(5 C)(T C)(Q D)(6 C)(3 C)) do:
[:each| self add: each asOrderedCollection].
!
deal: rank
"Self is an OrderedCollection deck of cards."
self size = 0 ifTrue:[^(OrderedCollection new) initialhand.]
ifFalse: [
^(self allButFirst deal: rank nextRank) addStack: self first rank:rank.
].
!
initialhand
#((A)(2)(3)(4)(5)(6)(7)(8)(9)(T)(J)(Q)(K)) do:[
:each|self add: each asOrderedCollection].
!
nextRank
"Self is a card rank. This method facilitates dealing "
(self = #K) ifTrue:[^#Q].
(self = #Q) ifTrue:[^#J].
(self = #J) ifTrue:[^#T].
(self = #T) ifTrue:[^9].
(self = 9) ifTrue:[^8].
(self = 8) ifTrue:[^7].
(self = 7) ifTrue:[^6].
(self = 6) ifTrue:[^5].
(self = 5) ifTrue:[^4].
(self = 4) ifTrue:[^3].
(self = 3) ifTrue:[^2].
(self = 2) ifTrue:[^#A].
(self = #A) ifTrue:[^#K].
!
addStack: card rank: rank
" find correct pile and then push.
Self is an OrderedCollection deck of cards. "
(self first first = rank) ifTrue: [
self first add: card.
]
ifFalse: [
self allButFirst addStack: card rank: rank.].
!
pushStack: card
" Self is a pile of cards such as #(2 (3 C) (A C)) as an OrderedCollection.
"
^(self add: (card)).
!