A Live Developer Journal

Building A Memory Game In Pharo Using Bloc

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.

Card model

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'

Initialising method (card not flipped and accessors)

Initialise method with a default 'flipped' value of false

    super initialize.
    flipped := false

symbol variable accessor

  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

We then create an accessor method to return the value contained in our 'symbol' variable, implemented in the code above.

Accessor method for flipped variable

    ^ 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.

Method for creating an announcement

    ^ 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.