Following a Memory Game tutorial
The tutorial is free so I am documenting every step I take so that I can capture thoughts along the build process. Any code that has been modified by me will be highlighted, otherwise it comes from the tutorial.
A card is an object holding a symbol to be displayed, a state representing whether it is flipped or not, and an announcer to emit state changes.
Object subclass: #MgdCardModel instanceVariableNames: 'symbol flipped announcer' classVariableNames: '' package: 'Bloc-MemoryGame-Demo-Model'
MgdCardModel super initialize. flipped := false
symbol: aCharacter symbol := aCharactor
In the code above, we create a new instance side method on the MgdCardModel class. The method name is a keyword message 'symbol:' which accepts an argument that we have given the variable name 'aCharacter'. Inside the method, we assign the aCharacter argument to our symbol instance variable.
symbol ^ symbol
We then create an accessor method to return the value contained in our 'symbol' variable, implemented in the code above.
isFlipped ^ flipped
As we have already initialised our 'flipped' variable, we don't need to create a setter for it (i'm not sure if we'll need one down the line). So we just created an accessor method for it, shown above.
announcer ^ announcer ifNil: [ announcer := Announcer new ]
We created another method above with the name 'announcer'. The method assigns a new instance of an Announcer object if there is nothing in the 'announcer' instance variable.