Results 1 to 5 of 5
  1. #1
    manefist is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,261
    Rep Power
    25

    Default Re: The game of Life - Conway

    reason that I cannot really understand
    Have you tried debugging your code to see what the code is doing wrong?
    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.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,261
    Rep Power
    25

    Default 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?

  4. #4
    manefist is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default 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..

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,261
    Rep Power
    25

    Default Re: The game of Life - Conway

    This is for testing only. Having everything in one file makes it easier for everyone to test with the same stuff.

Similar Threads

  1. game of life, using 2d array
    By aramiky818 in forum New To Java
    Replies: 1
    Last Post: 09-09-2011, 05:47 AM
  2. game of life
    By bigskers76 in forum New To Java
    Replies: 10
    Last Post: 12-09-2009, 05:21 AM
  3. 2d array help- Game of Life
    By n3philim in forum New To Java
    Replies: 2
    Last Post: 07-23-2009, 12:01 AM
  4. A more efficient Game of Life
    By unreal4evr in forum New To Java
    Replies: 3
    Last Post: 03-27-2009, 03:08 AM
  5. Game of Life assignment
    By javan00b in forum New To Java
    Replies: 4
    Last Post: 04-28-2008, 05:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •