Conway's game of life

gedaiu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 2 02:45:00 PST 2015


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.

There are some tests that check the function:

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

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

I don't see a glitch.

Thanks,
Bogdan



On Sunday, 1 February 2015 at 22:51:42 UTC, FG wrote:
> On 2015-02-01 at 22:00, gedaiu wrote:
>> I implemented Conway's game of life in D.
>
> I think you are playing a different game here.
>
>     /// Count cell neighbours
>     long neighbours(Cell myCell, CellList list) {
>         long cnt;
>         foreach(cell; list) {
>             auto diff1 = abs(myCell.x - cell.x);
>             auto diff2 = abs(myCell.y - cell.y);
>             if(diff1 == 1 || diff2 == 1) cnt++;   // Why || 
> instead of && ???
>         }
>         return cnt;
>     }



More information about the Digitalmars-d-learn mailing list