Jump to content

[Java] Conway's Game of Life


WarFox
 Share

Recommended Posts

I've been working on a little implementation of Conway's Game of Life simulator when I've had some spare time. So here it is:

 

https://github.com/martintc/ConwayGameOfLife-Java

 

Right now it is only implemented as a Text console interface.

 

The game is fairly simple. Set up a board with pieces on it that represent a living organism. Based on conditions of it's environment, the living organism can either die or live. The conditions simulate over/under population and just right living conditions. Any cell with less than 2 neighbor, dies. Any cell with greater than 3 neighbors dies. Any cell surrounded by 2 or 3 neighbors, continues to live into the next generation. Some interesting conditions are configurations where a group of these simulated organisms "blink" or stay in a constant state so long as they are not influenced by anything else.

 

Pictures:

Spoiler

Screenshot_2020-05-01_22-27-33.png.f02a94944f501b4e85e322e493f67101.pnganotherpic.png.bc0693ac2c235f4c0e23d35c61ab6a59.png

Planned added functionality:

  • Add in graphical user interface with javaFX or Swing
  • Added error handling, right now initializing cells does not prevent you from entering anything out of bounds.
  • Step multiple generations with one action instead of one generation at a time
  • Preload configurations or save starting configurations

Heart of my algorithm:

The heart of the algorithm is really in the determining the number of neighbors. Applying the general rules if fairly straight forward as to when a cell should die or live. How I did this was by creating an integer array the same size as the game board. Did a iterative loop through every cell and incrementing the cell in the same position of the game board array as the cell being looked at for every alive neighbor. Returning this and then using this multi-dimensional array to apply the rules. Without getting too deep into specifics on positions and assuming every cell is surrounded by 8 cells, the number of operations is ~8(n*m) where n is the number of rows and m is the number of columns). Obviously for edge pieces, a cell does not have 8 neighbors, but if an arbitrarily large multi-dimensional array is used, the most time consuming is the inner part of the multi-dimensional array.

Edited by WarFox
Link to comment
Share on other sites

Very cool! I love the idea of skipping generations, and it makes me very curious. I'm sure it's easier to just do the calculation at every step, but since the game is deterministic, I'm sure there's a specific ruleset for for every "n" when you set a standard step to be "n" normal generations. I assume that at least one rule would be that you have to examine the squares "n" distance away. Since normally it's only 1 generation per step, you only care about adjacent squares, while for 2 generations per step, you'd have to look at every square within a distance of 2.

I bet there's a simple inductive proof to generalize the ruleset for any "n". I might look into that for fun and post an update once school is over.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...