Reputation: 107
I'm trying to write conways game of life in Java but it's not working as it should. By that I mean, still lifes work but blinkers and ships do not.
Here is the code for my algorithm, the seeding is handled by a seperate mouse listener (which is working as intended).
int[][] state1 = new int[80][80];
int[][] state2 = new int[80][80];
public void logic(){
state2=state1;
for(int i=0;i<80;i++){
for(int j=0;j<80;j++){
int sum=state1[(i-1+80)%80][j]+state1[(i-1+80)%80][(j-1+80)%80]+state1[i][(j-1+80)%80]+state1[(i+1)%80][(j-1+80)%80]+state1[(i+1)%80][j]+state1[(i+1)%80][(j+1)%80]+state1[i][(j+1)%80]+state1[(i-1+80)%80][(j+1)%80];
if(sum!=2 && sum!=3){
state2[i][j]=0;
}
else if(sum==3){
state2[i][j]=1;
}
}
}
state1=state2;
}
Upvotes: 1
Views: 605
Reputation: 178411
state2=state1;
is not doing what you think it does.
It only makes the two variables reference the same array.
so, you are actually changing the same matrix you are taking as "last step"
To solve it, you will need to copy state1
into state2
.
Upvotes: 5