Results 1 to 5 of 5
Thread: The game of Life - Conway
- 12-07-2011, 03:58 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
The game of Life - Conway
Hello everyone, I am trying to write a simple program which emulate the game of life following four different rules which decide in the next generation the status (dead/alive) of a cell.
These are the rule:
F = filled cell
E = empty cell
1. if F < 2 OR F > 3 the cell in the next generation die
2. if F = 2 OR F = 3 the cell in the next generation live
3. if E = 3 the cell in the next generation live
Now, I want to try with this file:
* * *
The expected output should be:
*
*
*
However my result is:
*
*
*
My output is wrong for some reason that I cannot really understand, I am stuck to this point for days, I will really appreciate if someone has an useful suggestion to share.
Here my code:
Java Code:import java.util.Scanner; import java.io.*; class LifeGrid { private int[][] grid, newGrid; private int generation = 0; public LifeGrid(int x, int y, String filename) throws FileNotFoundException { grid = new int[x][y]; newGrid = new int[x][y]; int j = 0; Scanner scanner = new Scanner(new File(filename)); while(scanner.hasNextLine() && j < x) { String line = scanner.nextLine(); for(int i=0; i<line.length() && i<y; i++) { if(line.charAt(i) == '*') grid[j][i] = 1; else grid[j][i] = 0; } j++; } scanner.close(); } public void show() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { if(grid[i][j] == 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } System.out.println("Generation:" + generation); } //Getter methods public int getWidth() { return grid[0].length; } public int getHeight() { return grid.length; } public int getGeneration() { return this.generation; } public int getCell(int x, int y) { return grid[x][y]; } public static void main(String[] args)throws FileNotFoundException { LifeGrid life = new LifeGrid(6, 10, args[0]); life.run(); } //Check neighbours public int neighbours(int x, int y) { int neighbours = 0; int top = (y > 0 ? y-1: getWidth()-1); int btm = (y < getWidth()-1 ? y+1: 0); int left = (x > 0 ? x-1: getHeight()-1); int right = (x < getHeight()-1 ? x+1: 0); if(grid[x][top] == 1) {neighbours++;} if(grid[x][btm] == 1) {neighbours++;} if(grid[left][top]== 1) {neighbours++;} if(grid[left][btm] == 1) {neighbours++;} if(grid[left][y] == 1) {neighbours++;} if(grid[right][y] == 1) {neighbours++;} if(grid[right][top] == 1) {neighbours++;} if(grid[right][btm] == 1) {neighbours++;} return neighbours; } public void run() { int n; for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { n = neighbours(i,j); if(grid[i][j] == 1) { if(n < 2 || n > 3) {generation = 0;} if(n == 2 || n == 3) {generation = 1;} } else { if(n == 3) {generation = 1;} else {generation = 0;} } newGrid[i][j] = generation; } } grid = newGrid.clone(); show(); } }Last edited by Norm; 12-07-2011 at 04:43 PM. Reason: added code tags
- 12-07-2011, 04:45 PM #2
Re: The game of Life - Conway
Have you tried debugging your code to see what the code is doing wrong?reason that I cannot really understand
Add some printlns to show the values of the variables as they change. You should know what you expect the values of the variables to be as the code executes. When you look at what is printed out, you will be able to find the problem.
- 12-07-2011, 04:50 PM #3
Re: The game of Life - Conway
Can you hard code values for the grid array for testing so the program will execute without the need for any external file?
- 12-07-2011, 04:56 PM #4
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Re: The game of Life - Conway
But my purpose is to make the program independent, therefor I want the need of an external file.
However I tried to display any value but still I cannot find the error..
- 12-07-2011, 04:58 PM #5
Similar Threads
-
game of life, using 2d array
By aramiky818 in forum New To JavaReplies: 1Last Post: 09-09-2011, 05:47 AM -
game of life
By bigskers76 in forum New To JavaReplies: 10Last Post: 12-09-2009, 05:21 AM -
2d array help- Game of Life
By n3philim in forum New To JavaReplies: 2Last Post: 07-23-2009, 12:01 AM -
A more efficient Game of Life
By unreal4evr in forum New To JavaReplies: 3Last Post: 03-27-2009, 03:08 AM -
Game of Life assignment
By javan00b in forum New To JavaReplies: 4Last Post: 04-28-2008, 05:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks