Conway's game of life

FG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 2 03:23:14 PST 2015


Bloody Thunderbird has sent a reply to the OP and not to the NG.

On 2015-02-02 at 11:45, gedaiu wrote:
> I don't think that the line of code is wrong. If use && the function will check for neighbours only on diagonals. Having || allows the search on the vertical and horizontal axis and diagonals.

In short: Yes, && alone would check only diagonals, but I forgot to tell you to also change the ==.

(diff1 == 1 || diff2 == 1) -- bad, accepts whole neighbouring rows and columns
(diff1 == 1 && diff2 == 1) -- bad, accepts only neighbouring diagonals
(diff1 <= 1 && diff2 <= 1) -- correct, I think :)

This unittest should show the difference:

unittest {
     CellList world = [ Cell(0,0), Cell(0,1), Cell(0,2), Cell(0,3) ];
     assertEqual(Cell(1,1).neighbours(world), 3);
}

Cell(0,3) is not a neighbour bit fits the (diff1 == 1 || diff2 == 1) criterion.


More information about the Digitalmars-d-learn mailing list