# game of life

Page 1 of 1

## 6 Replies - 19107 Views - Last Post: 09 March 2010 - 09:16 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=160948&amp;s=1a92356797821d48f4df45ecb0dacd6c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• 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

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

## Re: game of life

Posted 09 March 2010 - 08:22 PM

[code] Paste Codes Between These Tags [/code]

### #3 no2pencil

• Professor Snuggly Pants

Reputation: 6839
• Posts: 31,513
• Joined: 10-May 07

## Re: game of life

Posted 09 March 2010 - 08:24 PM

Quote

```import java.util.Scanner;
```

... moving to Java.

### #4 Dogstopper

Reputation: 2972
• Posts: 11,223
• 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.

Reputation: 0
• 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?

### #6 pbl

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

Reputation: 8381
• 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

### #7 no2pencil

• Professor Snuggly Pants

Reputation: 6839
• Posts: 31,513
• Joined: 10-May 07

## Re: game of life

Posted 09 March 2010 - 09:16 PM

pbl, 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.