@nicolai, Thx for the cool tip. #eightNeighbors is new to me and I was curious to try it. So below is one way to have Life with it. For conciseness of this post, please excuse that I've roughly used booleans directly as grid elements rather a nicely encapsulated Cell object. Object subclass: #LifeGrid instanceVariableNames: 'size cellsAlive' LifeGrid >> initialize size := 4. cellsAlive := Dictionary new. 1 to: size do: [:x| 1 to: size do: [:y| cellsAlive at: (x@y) put: false ] ] LifeGrid >> turnAliveAt: location cellsAlive at: location put: true LifeGrid >> aliveAt: location ^ cellsAlive at: location ifPresent: [ :isAlive | isAlive ] ifAbsent: [ false ] LifeGrid >> countNeighboursAt: location ^ (location eightNeighbors select: [ :neighbour | self aliveAt: neighbour ]) size Testing from Playground... grid := LifeGrid new. grid countNeighboursAt: 2@2. "==>0" grid turnAliveAt: 1@1. grid countNeighboursAt: 2@2. "==>1" grid turnAliveAt: 3@3. grid countNeighboursAt: 2@2. "==>2" grid countNeighboursAt: 1@2. "==>1" cheers -ben On 16 January 2018 at 05:47, Nicolai Hess <nicolaihess@gmail.com> wrote:
2018-01-15 18:04 GMT+01:00 Photon <nico-braun@live.de>:
when I call the countNeighbours method i get an error: the block wants two arguments but I pass only one. This is probally because i want to pass x y but only really pass x wich ends up being a cell and not the index.
I still have trouble thinking it trough. The indicieDo: method seems to make the matrix bigger?! I don`t quite understand it.
It should not, and looking at the implementation, it just iterates with two loops 1 to number of rows times 1 to number of columns and calls your block argument with the pair of indexes.
There must be an easy way to to figure out if the suroundings are alive or not. I mean its all there what I need and in my mind its so easy to discribe it with normal words. But to tell the machine witth syntax is another thing really :/
About the surroundings, there is a nice method in Point Point>>eightNeighbors, that gives you the coordinates of the eight surrounding fields for example: (5@7) eightNeighbors "{(6@7). (6@8). (5@8). (4@8). (4@7). (4@6). (5@6). (6@6)}"
with this method and if you iterate through all pairs of indices of the matrix, you can do your computation for every point, but you have to take care about the points that don't have valid matrix indices for some of their neighbours (like 0@0, the left and top neighbours may actually "on the other side of the matrix"). But you can take a look at SequenceableCollection>>atWrap: how it deals with "wrapping around".
what if i selected only one element with do:[] and let its tell me its idices. I store them in a temp object and then I check outside the block the neighbours and add to the counter. Once this is done I repeat as many times as their elements in the matrix?