Archived

This topic is now archived and is closed to further replies.

Starwind8748

I'm doing the game of life!

Recommended Posts

Starwind8748    122
I ran into a small problem though. I wrote a function to count the neighbors, and it worked, but it ended up being 3/4 of a page long. Does anyone know any incredibly short ways of doing this?

Share this post


Link to post
Share on other sites
Fruny    1658
Not terribly efficient, but short enough:

  
int indexwrap( int x, int xdim )
{
x %= xdim;
return (x<0)?x+xdim:x;
}

int count( int x, int y, int xdim, int ydim )
{
int i,j;
count = 0;
for( i = x-1; i < x+2; ++i )
for( j = y-1; j < y+2; ++j )
count += !((i==x) && (j==y)) && cell[indexwrap(i,xdim)][indexwrap(j,ydim)];
return count;
}

Share this post


Link to post
Share on other sites
Stoffel    250
It''s been a while since I played, but I don''t remember any neighbors. I remember getting babies in the back of the stationwagon, and the occasional car crash would dump them all out onto the playing board (oh the humanity!).

Share this post


Link to post
Share on other sites
wrenhal    122
quote:
Original post by Stoffel
It''s been a while since I played, but I don''t remember any neighbors. I remember getting babies in the back of the stationwagon, and the occasional car crash would dump them all out onto the playing board (oh the humanity!).


ROFL

Share this post


Link to post
Share on other sites