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 start Squeak: inisqueak
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 " Deals out 13 piles."
cards patience. "Plays 1 move, prints out piles to Transcript."
To test:
hand := cards deal: $K.
hand. "Use PrintIt from middle mouse button"
hand pickTopCard: $K
*************************************************************** "
! Object methodsFor: 'algorithms' !
patience
"To play: (FileStream oldFileNamed: 'patience.st') fileIn.
cards := OrderedCollection new.
cards sample.
cards patience.
Output is to the Transcript window."
| hand |
hand := self deal: $K.
hand play: (hand pickTopCard: $K) move: 1.
!
play: card move: moveNum
| 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; cr.
Transcript show: 'Piles at end:'. Transcript cr.
Transcript show: self printString. Transcript cr.
]
ifFalse:[
Transcript show: 'Piles: ', self printString; cr.
^self].
!
sample
" Sets an OrderedCollection of cards."
#(($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].
!
initialhand
#(($A)(2)(3)(4)(5)(6)(7)(8)(9)($T)($J)($Q)($K)) do:
[:each| self add: each asOrderedCollection].
!
deal: rank
self size = 0 ifTrue:[^(OrderedCollection new) initialhand]
ifFalse: [
^(((self allButFirst) deal: (rank nextRank)) addStack: (self first) rank: rank)].
!
addStack: card rank: rank
(self first first = rank) ifTrue: [
self first add: card.
]
ifFalse: [
(self allButFirst) addStack: card rank: rank.].
!
pickTopCard: rank
| st |
st := self select: [:each| each first = rank].
(st first allButFirst) size = 0 ifTrue: [
^(#() asOrderedCollection)].
^st first removeLast asOrderedCollection.
!
nextRank
(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].
!