We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Re: Game of life
Page Index Toggle Pages: 1
Re: Game of life (Read 959 times)
Re: Game of life
Apr 5th, 2009, 3:12pm
 
didt you take a look at some existing game of life code ?

there are a lot of them out there
http://is.gd/qTXO
Re: Game of life
Reply #1 - Apr 6th, 2009, 4:01am
 
i don't think that's the question. the supplied code is for the original Life, but the question concerns a different version where the next generation isn't based on a count of the neighbours but on their value when read as a byte.

basically you need a 256 (8 bits = 256) array at the top which you then seed with your rules (probably random 0s and 1s)

the value of the neighbours is then
128 * i[x - 1][y - 1] + 64 * i[x][y - 1] + 32 * i[x + 1][y - 1] +
16 * i[x - 1][y] + 8 * i[x + 1][y] +
4 * i[x - 1][y + 1] + 2 * i[x][y + 1] + 1 * i[x + 1][y + 1];

(that's the row above, the current row and then the row below. i may have that upside down though so you might have to swap the y - 1 and y + 1)

use this value (0-255) as an index into your rules array.
Re: Game of life
Reply #2 - Apr 6th, 2009, 1:30pm
 
NoviceMan wrote on Apr 6th, 2009, 8:04am:
if you could explain what you mean when you say seed my 256 array at the top with my rules.


well, you pretty much have it here:
Quote:
Im trying to create a random rule with the array
int rule[] = new int[256];
and assign the elements of the rule to be 1 or 0.  Im trying to make a random assignment that will set 10% of the values to 1 using the random() command.


which is simply a case of
int rule = new int[256]; // int is way too big, 1 bit will do

then you can either set 26 (ie 10% of 256) random items to 1
for (i = 0 ; i < 26 ; i++) {
 rule[random(255)] = 1; // index might need to be an integer
}
or go through all the rules and set them randomly with 10% chance
for (i = 0 ; i < 256 ; i++) {
// 1 chance in 10
 if (random(10) < 1) {
   rule[i] = 1;
 } else {
   rule[i] = 0;
}

(might be an idea to have a set list of these initially - easier to debug than something that keeps changing randomly)

now for each iteration new[x][y] = rule[v]; where v is the value of the neighbours.

you don't need your n == 1 || n == 2... condition (which won't work anyway!)

um, there's no equivalent in this scheme for the
"   Each cell with two or three neighbors survives.
For a space that is 'empty' or 'unpopulated'
  Each cell with three neighbors becomes populated. "
rules - the new cell appears to depend solely on the value of its neighbours and its current state doesn't seem to matter.
Re: Game of life
Reply #3 - Apr 6th, 2009, 1:32pm
 
btw this bit in your neighbour calculations

for (int i= x-1; i <=x+1; i++){
  for (int j=y-1; j <= y+1; j++){
    if (i == x && j ==y) continue;
    if (cur[(i+width)%width][(j+width)%width] == 1) n++:
  }
}

which is counting the neighbours

is made obsolete by the 128 * ... 64 * ... line above it.
Re: Game of life
Reply #4 - Apr 7th, 2009, 10:22am
 
just do it at the same time as you set the value of next[x][y] (i see you were drawing the same time as you are setting, which is probably faster than looping over the array again)

you can either test next[x][y] and set according to that
if (next[x][y] == 0) {
 set(x,y,color(0));
} else {
 set(x,y,color(255));
}
which is flexible or just
set(x,y,color(next[x][y] * 255));
which is faster, probably.

NB this and the next[i][j] have to go in the inner loop in draw(). also, you're using i, j where i was using x, y so you'll need to change that.
Page Index Toggle Pages: 1