• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conway's Game of Life - problems with algorithm and swing  RSS feed

 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to all!

I'm currently trying to implement Conway's Game of Life in Java but the program doesn't work as I expected.

Here is the idea behind my basic algorithm:
-Have two separate two dimensional array. One for to calculate neighboring cells, and one for to add new cells.
-Calculate surrounding cells using the first array, in order to prevent having variance in neighbor numbers.
-When a cell with three surrounding cells is found in first array, new cell is added to second array. And when a living cell with 2 surrounding cells is found it is added to second array too.
-Completing the work above, first array's whole living cells are removed. Then, second array transfers its whole living cells to first array. After that, second array gets back to its initial state.
-repaint() method is invoked.

What i get from this algorithm is really annoying. After a given time, initial alive cells which are supposed to live up to second generation dies.



What is the problem i fail to see?

Thanks in advance.

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, and thanks for using code tags!  You got a cow (which is good!)

It looks like you snipped pieces of your code.  Could you post the entire program(s)?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
initial alive cells which are supposed to live up to second generation dies. 

How are you trying to debug the code to see why that is happening? Try printing out the values of the variables that are used to see what is happening.
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you trying to debug the code to see why that is happening? Try printing out the values of the variables that are used to see what is happening.


I did. I couldn't understand what's wrong with it. Because neighbor counting algorithm works as intended and other methods seem flawless to me. There is a code snippet which shows a strange behavior but I can't find it.

Rest of the code.


 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
methods seem flawless to me.

Yes, code you have written often seems like it should work.  However when you debug it and see what is is actually doing there may be an extra understanding reached about what needs to be fixed to make it do what you want.

If you want help with debugging, you need to post code that will compile, execute and show the problem.

One suggestion for debugging, make the grid as small as possible to reduce the debug output.  80x80 is way too  big.  Maybe 8x8?

The code has too many hardcoded magic numbers: 80 that will need to be changed to use the length of the array
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the debugging, put extra println's around the lines 48 and 52:

 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, it's me again. I changed a lot, though have yet to find underlying erroneous snippet. I believe code below is simple as it can be, adding further description and comments would be unnecessary.
Neighbor checking algorithm produces incorrect data inside process() method. I couldn't understand why because  getNeighborNumbers() method and process use two seperate arrays. What process method does it to add live cells to temp[][] array and then transferring it to cells[][] array.

If any of you observe a significant bug in my code, please let me know. I'd appreciate it. Thanks in advance.

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the test on line 6 for?  What is important about the value 5?

What is in the cells array when the process() method is called?  Do you print its contents so you can check it?
What is in the temp array at the end of the process() method?  Print its contents so you can check it.


A debugging suggestion: Add a println at the end of getNeighborNumbers that prints the value of i, j and neighbors when neighbors is > 0
That will allow you to check the algorithm.
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Initial alive cells, referring a still life:
It's a square, every cell has 3 neighbors.


I get the following from what you suggest. (i j : neighborNumbers)


Output suggests neighbor checking algorithm fulfills its design aspects and works as intended. Important thing is, cells[4][5] was called exactly 3 times, which means it was able to set temp[4][5] = true.



But i have no idea why. cells[4][5], according to this output is true, but it was not supposed to be. When  i print its initial value inside constructor, it prints false, but something magical happens, makes it true and ruins everything.
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whole program can be found here: http://codepaste.net/wpbv26

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was in cells before process and what was in cells afterwards?
Start the testing with the simple 3 in a horizontal row.  Is the expected result (3 vertical) obtained?
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found it. I unwillingly spent my hours and days to fix this god damn program.

And it appears that it was just a incomplete typing of a Boolean statement.  Yeah, instead of writing == , I've written =.

Anyways, I appreciate your help. Thank you!

Best regards.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of writing == , I've written =.

Yes that is something that isn't detected by the compiler.  That's why one should never compare a boolean variable to true (or false).  There is no need.  The value of the boolean itself is all that is needed:
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Burak

there is something I would like to see clarified. Can you run this code?



Questions:

1) can you explain the third outcome?
2) can you think of a simple remedy for this (for instance: in line 14)?
3) can you see at what I hinted in my first reply?
4) in your latest code, you still do: cells[][] = temp[][], however, it seems to work. Can you explain why it works there?
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running this?  I added a main:

Is that what you intended?

Also, this line:

...has a problem.  You are accessing the static method sleep() with an instance.  It is better to access it in a static way:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!