简体   繁体   English

如何在Smalltalk Visual Works中制作矩阵类?

[英]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. 我是Smalltalk的新手,正在尝试制作一个简单的TicTacToe游戏,我希望我的模型类成为矩阵,但我找不到在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. 我一直在关注本教程: http : //nerdysermons.blogspot.fr/2012/03/tictactoe-game-in-pharo-smalltalk.html ,它在Pharo上工作正常,但在使用Matrix类型时遇到问题以及simplebuttonmorph。 Can anyone please explain the syntax/packages/libraries between Pharo and VisualWorks? 谁能解释一下Pharo和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. 将一个类定义为Object的子类,并具有单元格,numberOfRows和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: 创建诸如at:at:和at:at:put:之类的方法,这些方法将按以下方式计算单元格数组的索引:
 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. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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