Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Game Of Life, some questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Game Of Life, some questions" Watch "Game Of Life, some questions" New topic
Author

Game Of Life, some questions

Henrik Persson
Greenhorn

Joined: May 01, 2011
Posts: 5
Hello!

I'm new to Java, and this is my first "real" project that I'm actually satisfied with, so I thought I'd share it with you. Took a few days to write.
It's a version of the famous Game Of Life by John Conway, where the cells are represented by checkboxes (only because making some other graphic version would be too advanced for me).
It looks alright, you can choose three different sizes and four different speeds. The layout might not be correct for all systems (it's optimized for OSX), but just a simple resize of the window so the checkboxes form a square pattern will do the trick.

Some questions:

-The checkCell()-method seems extremely clunky and awkward, and I guess there must be a better way to do it, any tips? Algorithms?
-The reason I put an infinite loop in the startGame()-method is because I kept getting exceptions when trying to stop and start the game, something about how threads work. Is there a better way to do this (without making a complete rewrite of the entire program)?
-Is there any way to make the gui platform independent?

Have fun! Code below:

GameOfLifeCell-class:



GameOfLife-class:


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

I think you made things a lot harder for yourself by implementing the array of cells as a one-dimensional array.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I made this with javascript once, small project that was fun to do. You can take a look at it here it allows you to initialize the board and then press start, stop or reset. Just select view source in the browser to see the code. Since it is javascript that is similar to java syntax wise and since these kind of problem solving are language independent you might get some ideas from it.
Note: This is probably not the best or fastest way to implement this, it was just some pet project of mine I did for fun there is probably something that could be written better, also the code is not commented.
Henrik Persson
Greenhorn

Joined: May 01, 2011
Posts: 5
Paul Clapham wrote:I think you made things a lot harder for yourself by implementing the array of cells as a one-dimensional array.

I wrote this program while reading the Head First Java book, and it wasn't until I finished the program that I found out about multi-dimensional arrays in the appendix, and it crossed my mind that I could have used one of those instead, but I fail to see how that would improve my code. Can you be more specific? Keep in mind that I am a total newbie to programming!

Unnar Björnsson wrote:I made this with javascript once, small project that was fun to do. You can take a look at it here it allows you to initialize the board and then press start, stop or reset. Just select view source in the browser to see the code. Since it is javascript that is similar to java syntax wise and since these kind of problem solving are language independent you might get some ideas from it.
Note: This is probably not the best or fastest way to implement this, it was just some pet project of mine I did for fun there is probably something that could be written better, also the code is not commented.

Thanks a lot! Google Chrome freezes when I try to view your code however, I'll try it in a different browser.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Game Of Life, some questions