I think my problem is mostly syntax but it might be my overall understanding of class hierarchy. Basically it's a Deck class with an array filled with Card objects, Card is a subclass of Deck, so Deck should be able to use Card's blocks and methods, right? If it is, I am messing up the syntax horribly trying to invoke it. I am using nested while loops to fill the array, but I want each instance of the Card object to have the suit and rank of that card instead of just printing "a Card". I left where I tried to make the Card object another array of size 2 to hold the Suit and Rank, but my gst compiler says it expected an "object" so obviously I'm doing something wrong. I pasted my code so you can see what I'm talking about. Other than the block invocations it is filling up an array of size 52 with blank card objects so the rest of the Deck class is basically working.
"The Deck object class is a deck of 52 Card objects. "
Object subclass: Deck [
| Content |
<comment: 'I represent of Deck of Cards'>
Deck class >> new [
<category: 'instance creation'>
| r |
r := super new .
Transcript show: 'start ' .
r init .
^r
]
init [
<category: 'initialization'>
|a b c|
Content := Array new: 52 .
a := 1 .
c := 1 .
[a <= 4] whileTrue:[
b := 1 .
[b <= 13] whileTrue:[
|card|
card := Card new .
Card := Array new: 2 . "This is where I'm trying to use the Card class blocks to make the Card objects have Rank and Suit"
Card at: 1 put: Card setRank: b| . "and here the rank"
Card at: 2 put: Card getSuit: a| . "and the suit"
Content at: c put: card .
b := b + 1 .
c := c + 1 .
].
a := a + 1 .
].
Content printNl .
]
] .
"The Card object has subclass Suit and a FaceValue array of Suit and Rank. "
Object subclass: Card [
| Suit Rank |
<comment: 'I represent a playing Card' >
init [
<category: 'initialization'>
Suit := Club .
Rank := 1 .
Transcript show: 'nCard ' .
^super init
]
getSuit: suitVal [
suitVal = 1 ifTrue: [Suit := Club] .
suitVal = 2 ifTrue: [Suit := Diamond] .
suitVal = 3 ifTrue: [Suit := Heart] .
suitVal = 4 ifTrue: [Suit := Spade] .
^Suit
] "getSuit"
setRank: rankVal [
Rank := rankVal .
^Rank
]
]
z := Deck new .