Archived

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

I'm doing the game of life!

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

• Forum Statistics

• Total Topics
628303
• Total Posts
2981923

• 10
• 11
• 11
• 10
• 10