• Post Reply Bookmark Topic Watch Topic
  • New Topic

John Conway's Game of Life Algorithm Help  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem:
So, I have been working on the Game of Life for a school project and I think I have gotten the algorithm for it wrong. Something like that.
I can't seem to figure out how to get things like gliders to work.

Whenever I make a glider, it just turns into a still life shape. (2x2 box). The same thing also happens for all of the oscillators.

Also, this is my first post so... If I'm doing this wrong, just let me know




array[o][i] is a 2D boolean array that stores if a cell is dead or alive and grid[o][i] is just the display in swing. (It's just an array of JPanels).
If you need any more code, just ask.

 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The rest of the code (I changed it a bit in attempt of fixing it xD):
 
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried to execute the code you have given, but there is an error on the method "revalidate(); ".... are you missing with some library import or a custom method signature?
Please take a look.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's too much code to examine in detail, but at a quick glance it looks like you're using just a single array to hold the cell values while you iterate over them. If so, that won't work. You need two arrays - one to read from, and one to write to. Once you're done with the iteration, you can copy the values of the second array to the first array for the next iteration.
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashish Dutt, I have always used 'revalidate();' in eclipse but it seems to have changed to 'invalidate();'. Thanks for the reply anyway.

Ulf Dittmer, I changed my code a bit and I spent a few hours looking around on the internet for someone who had the same problem as me. That was the exact problem that I had. One question, do I have to clear the one that I read from after I write it to the second?
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason you need two arrays:

You need to know the current state of everything while you compute the next state. remember, the entire field changes simultaneously on each clock cycle. My guess is that when you only had one array, you looked at the first element, and figured out if it should change. If so, you changed it. The problem is that when you then go to compute what happens to its neighbors, you are using the NEW state of the previous element.

so, you need to have a copy of the current state. you use it to compute the next state of each element, which you have to store in the second array.

Once that is 100% done, you need to set the "future state" array values as the "current state" - this is your next iteration.

There are several ways you could do this - copy the future to the current or swap the references are the two that come to mind. The first is probably simpler, but remember, you can't just do:

currentSateArray = futureStateArray;

because then both references will point to the same single array.
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help, I should be able to solve my problem now. Thanks
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhhhh, I've had another problem with my code, (t's to do with the copying and clearing of the arrays) and I'm not sure what I am doing wrong.



Thanks in advance for any help I get
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes you think there's a problem with that?

From what we can see, it might be anything starting with a compiler error because you haven't declared "arraycopy" and continuing through a whole range of other possibilities.
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't really a problem with the code as such, it's just that it's results aren't what I want and I don't know how to fix it. Here are some screenshots of my problems


Capture.PNG
[Thumbnail for Capture.PNG]
Oscillator
Capture1.PNG
[Thumbnail for Capture1.PNG]
Oscillator after next generation
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And then there is the glider:
Capture2.PNG
[Thumbnail for Capture2.PNG]
Glider
Capture3.PNG
[Thumbnail for Capture3.PNG]
Glider after next generation
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't worry about it. I made a stupid error which I have fixed now; I wasn't copying when nothing happens to the cell to the other array.

Thanks everyone for your help
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Fraser wrote:Don't worry about it. I made a stupid error which I have fixed now; I wasn't copying when nothing happens to the cell to the other array.
Thanks everyone for your help

Just in case you re-read this thread. I'd offer a few pieces of advice:

1. Your game logic is much too closely tied to your GUI. The entire game can be written using ONLY two boolean arrays - 'before' and 'after' or 'current' and 'future' - how you display those arrays (or use them to update the display) is a completely separate issue.

2. You have an awful lot of redundancy in your code. My advice: write methods for each "direction", or (possibly even better) define them in an enum. Or, rather than doing each direction individually, what about a getNeghbours() method that returns ALL the neighbours of a particular cell as an array?
You might even want to think about a Grid<T> class that encapsulates all this stuff and implements java.lang.Iterable, because you do do a lot of iteration of your grid.

3. You might also want to think about an enum for your ActionEvents: START_GEN, CLEAR_SCREEN etc. I think you'll find it makes your code a lot more readable. You can also use switch statements with enum constants (although these days you can use them with Strings too).

HIH

Winston
 
Jack Fraser
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code is nowhere near finished and I will clean it up eventually by writing it in methods and such. Also, I have never used Enums before so... Yeah...

As for what you mean with the arrays and GUI, I have no idea what you mean either. :/
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Fraser wrote:My code is nowhere near finished and I will clean it up eventually by writing it in methods and such. Also, I have never used Enums before so... Yeah...

As for what you mean with the arrays and GUI, I have no idea what you mean either. :/

The game of life is a piece of logic. A GUI is simply one way of displaying its output. You could also output it to a character display like a console or print the results on a printer - ie, the DISPLAY has nothing whatsoever to do with THE GAME - so you want to try and keep the two things as separate as you possibly can.

As for the rest, look at your code. You have two enormous 'if' stacks to check each "direction" from your 'current cell' in turn, all of which do exactly the same thing. Wouldn't it be easier to simply write a method that returns ALL "neighbours" at once? Then you just run through the results and use the same code to do your counting, viz:Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS: Unless you want Campbell Richie climbing all over you, don't write things like:
if (array[o - 1][i] == true) {

array[o - 1][i] is already a boolean, so the "== true" is redundant.

It can also go disastrously wrong if you forget one of those '=' because
if (array[o - 1][i] = true)
is perfectly legal, but does something completely different.

The correct form is:
if (array[o - 1][i]) {

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will hear much about "MVC" in programming. That is an acronym for "Model - View - Controller". The idea is that how you MODEL the data is completely separate from how you VIEW/DISPLAY the data and how you CONTROL the data.

A simple example would be something that tells you the time. There are many ways on how you could model/disply time. You could use digits. You could use hands that go around in a circle. I have a binary coded decimal clock on my desk that uses 20 LEDS that click on and off. You could use a graphic that shows the position of a Sun or Moon in the sky. You could use a picture of an hourglass with sand running down. There are probably thousands of ways you can SHOW what time it is.

And NONE of those care how you store the time, or keep track of time, as long as there is some way for the display to get the time. The display basically says "Hey - what time is it?". It gets the time and figures out how to show that to the world.

The MODEL is what keeps track of time. You may keep track of seconds elapsed since some arbitrary fixed date. You may look at the position of the sun in the sky. You may count the number of grains that fall through a hole. But the models only job is to know what time it IS, and be able to send it to someone who asks (generally, the VIEWer).

The CONTROL is the part that lets you...well..control things. My watch has a stem you pull, push, and twist to change the time. My clock-radio has four buttons. My digital clock has two buttons. My computer's clock lets me use a mouse and a keyboard to change things.

These components all need to be able to talk to each other, but each is really independent of the others.

That is what we are saying. Your Arrays that store the current state shouldn't be so intimately tied to your display.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!