• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Conways game of life

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone please hep me get started with this code ?

Do you know what Conway's Game of Life is? No? Well then, prepare to be amazed!* Conway's Game of Life is probably the best-known cellular automaton experiment. You have an mxn grid of cells, each of which is either on or off. Based on the current state of the grid, you can generate a new grid, wherein some cells may flip their states, and others may stay the same. Each cell's new state will be based solely on its current state, and the current state of its eight immediate neighbours.
In specific, Conway's Game of Life has four rules for determining the new state of a cell:
1. A dead cell (off/0) will activate if it has precisely three live neighbours.
2. A live cell with exactly two or three live neighbours will stay the same (still live).
3. A live cell with only one or zero neighbours will die of loneliness.
4. A live cell with four or more live neighbours will die of irritation.
For this assignment, assume an additional rule:
 A boundary cell (a cell at a corner, or along one of the edges) has neighbours that wrap. ◦ e.g. if a grid is 5x5, cell[2][0]'s neighbours are not just cell[1][0], cell[1][1], cell[2][1],
cell[3][1], and cell[3][0], but also cell[1][4], cell[2][4], and cell[3][4].
You start off with some initial grid state, and apply the rules above to the entire grid for each iteration. Obviously, different initial grid states will yield different grid states after X iterations.
For your implementation, you need to handle the following:
 Write a program that can simulate the Game of Life (I'm guessing you saw this part coming).
 The program will use the console for input/output (this may affect your choice of IDE).
 The initial grid state will be in a text file (refer to the assignment folder for examples).
 The user will have to tell the program the name of that file.
 The program will prompt the user for a # of iterations to process
◦ If the user enters 0 (or less), the program should end and display the final grid.
◦ If the user enters X>0, it should perform X iterations, show the new current grid (after X
iterations), and return to the iteration prompt (to repeat this process)
Remember, the new grid needs to be constructed in a separate array from the original, because otherwise that would change the neighbours of other cells before they were handled. Consider how you can handle this intelligently (tip: even though it's a completely different problem, look up how double buffering works, or simply attend tutorial and I can give you some tips for this).
 * Amazement not guaranteed.
Consider a somewhat modular approach:
 A loadState method that, when given a filename, loads the text file and creates/initializes
the grid (separating this from the main program logic makes it more readable)
 A getCell function that tells you the value at a given coordinate (this can make it easier to
implement the wrapping)
 A countNeighbours function that counts the neighbours around a coordinate (making use
of getCell)
 A method to display the current state of the grid, since you'll be doing it often
 An iterate method that iterates through all of the cells, creates the new grid state, and
handles any array management
Obviously, you'll need to handle some IO for this assignment. You need to be able to write to the screen, and to read from both the keyboard and a file. Consider looking up the Scanner class on the Java API specifications page ( http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html ). Additionally, take a look at the sample LifeGame.java skeleton provided in the assignment folder.
Posts: 2911
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to code ranch Simmi,

Before we start, I would suggest you look at this: Not A Code Mill
and this: Quote Your Sources
salvin francis
Posts: 2911
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Additionally, since Conways game of life is a topic that we have touched in the past, I would also suggest you to look at this: Search First
Posts: 1868
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's also helpful to know what Java development is at.
For instance what do you know about or what is your level of confidence with:
  • Java Classes
  • Interfaces
  • naming/style conventions
  • the keywords Public, Private, Static, Final
    this is supposed to be a surprise, but it smells like a tiny ad:
    a bit of art, as a gift, that will fit in a stocking
      Bookmark Topic Watch Topic
    • New Topic