简体   繁体   中英

How to make a Matrix Class in smalltalk Visual Works?

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM