game of life

john conway's game of life

Page 1 of 1

6 Replies - 16896 Views - Last Post: 09 March 2010 - 09:16 PM Rate Topic: -----

#1 ask00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 10

game of life

Posted 09 March 2010 - 08:17 PM

The Game of Life is not your typical computer game. It is a 'cellular automaton', and was invented by Cambridge mathematician John Conway. This game became widely known when it was mentioned in an article published by Scientific American in 1970. It consists of a collection of cells which, based on a few mathematical rules, can live, die or multiply. Depending on the initial conditions, the cells form various patterns throughout the course of the game.

The Rules

For a space that is 'populated':

Each cell with one or no neighbors dies, as if by loneliness.
Each cell with four or more neighbors dies, as if by overpopulation.
Each cell with two or three neighbors survives.

For a space that is 'empty' or 'unpopulated'

Each cell with three neighbors becomes populated.



My code(not fully complete):

import java.util.Scanner;
public class studyhard7 {
    
    public static void main(String[] args) {
    	
    	Scanner input=new Scanner(System.in);
    	
    	System.out.println("Please enter either 1 or 0:");
    	
    	int array[][]=new int[5][5];
    	for(int row=0;row<array.length;row++){
    		for(int column=0;column<array[row].length;column++){
    			int a=input.nextInt();
    			if(a==1||a==0){
    				array[row][column]=a;
    			}
    			else{
    				System.out.println("Invalid number");
    				System.exit(0);
    			}
    			
    		}
    	}
    	
    	genMethod(array);
    	
    }
    
    
    public static void genMethod(int matrix[][]){
    	
    	
    	for(int i=1;i<=10;i++){
    		
    		System.out.println("Generation "+i);
    		
    		
    		for(int row=0;row<matrix.length;row++){
    			for(int column=0;column<matrix[row].length;column++){
    				System.out.print(matrix[row][column]+" ");
    			}
    			System.out.println(" ");
    		}
    	}
    	
    	
    	
    }
    
}





Anyone help me to complete this program, i got stuck to make the rule of the game....please help me....

This post has been edited by no2pencil: 09 March 2010 - 08:24 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: game of life

#2 xtr3mnico  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 89
  • Joined: 26-February 10

Re: game of life

Posted 09 March 2010 - 08:22 PM

[code] Paste Codes Between These Tags [/code]
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • Posts: 30,868
  • Joined: 10-May 07

Re: game of life

Posted 09 March 2010 - 08:24 PM

Quote

import java.util.Scanner;

... moving to Java.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: game of life

Posted 09 March 2010 - 08:31 PM

pbl wrote a code snippet on this about 6 months ago.
http://www.dreaminco...snippet4501.htm

If you have a specific question, it would be easier for us if you just asked it.
Was This Post Helpful? 1
  • +
  • -

#5 ask00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 10

Re: game of life

Posted 09 March 2010 - 09:02 PM

I have checked the pbl code, but i'm not understand his code, i'm just a beginner. I want to make a simple code that generate 10 generations of cells for a given pattern of input. my code as above just generate 10 same arrays. this because i'm not enter the rule the game into my code.

rules like below:
For a space that is 'populated':

Each cell with one or no neighbors dies, as if by loneliness.
Each cell with four or more neighbors dies, as if by overpopulation.
Each cell with two or three neighbors survives.

For a space that is 'empty' or 'unpopulated'

Each cell with three neighbors becomes populated.

what should i use?if statement or create another method?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: game of life

Posted 09 March 2010 - 09:15 PM

It is not easy... this is why the code is kind of complicated
You need 2 arrays to compute the next generation
you cannot just update an array because the rule applies to if cell still there if cell is not there

If you are a beginner it is not the type of assigment you can do that easily
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • Posts: 30,868
  • Joined: 10-May 07

Re: game of life

Posted 09 March 2010 - 09:16 PM

View Postpbl, on 09 March 2010 - 10:15 PM, said:

If you are a beginner it is not the type of assigment you can do that easily

Which is why it's a common assignment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1