Archived

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

I'm doing the game of life!

This topic is 5844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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
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
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