Question

I am new to smalltalk and trying to make a simple TicTacToe game, I want my model class to be a matrix but I can't find a way to do it on Visual Works. I've been following this tutorial : http://nerdysermons.blogspot.fr/2012/03/tictactoe-game-in-pharo-smalltalk.html , it works just fine with Pharo but I'm having trouble with the Matrix type and also the simplebuttonmorph. Can anyone please explain the syntax/packages/libraries between Pharo and VisualWorks? Thank you .

Was it helpful?

Solution

The following is a suggestion - there are many ways to implement matrices.

  1. Define a class as a subclass of Object with instance variables for cells, numberOfRows and numberOfColumns.
  2. Create a class method to initialize the matrix given the number of rows and number of columns - make the cells an array of size rows * columns
  3. Create methods like at:at: and at:at:put: which calculate an index into the cells array as follows:
cellNumberAt: row at: column
   ^(row - 1) * numberOfColumns + column

at: row at: column put: value
   cells at: (self cellNumberAt: row at: column) put: value

at: row at: column
   ^cells at: (self cellNumberAt: row at: column)

rowAt: rowNumber
   | row |
   row := OrderedCollection new.
   1 to: numberOfColumns do: [:columnNumber |
      row add: (self at: rowNumber at: columnNumber)].
   ^row

columnAt: columnNumber
   | column |
   column := OrderedCollection new.
   1 to: numberOfRows do: [:rowNumber |
      column add: (self at: rowNumber at: columnNumber)].
   ^column

I hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top