So I'm trying to print the first six generations of the Game Of Life in a 7x7 array. I only have the code for one generation, but I can fix that later. I made the array types integers. What I need help with is getting the correct code for all of the neighbors that each cell has and also the code for each generation to correctly mutate. If you can help, PLEASE do!

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class blank {

public static void main(String[] args) throws FileNotFoundException {
Scanner fileReader = new Scanner(new File("project3.txt"));
int [][] neighbor = new int [7][7];
int [][] board = new int [7][7];
for(int i = 0; i<7; i++){
for(int j = 0; j<7; j++){
board[i][j] = fileReader.nextInt();


if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i-1][j]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i+1][j]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i][j-1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i][j+1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i][j-1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i-1][j-1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i-1][j+1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i+1][j+1]==2)
neighbor[i][j]++;}

if(i >= 1 && i <= 5 && j >=1 && j <= 5){
if(board[i+1][j-1]==2)
neighbor[i][j]++;}


if (i==0 && j>=1 && j<=5){
if(board[i+1][j]==2)
neighbor[i][j]++;}

if (i==0 && j>=1 && j<6){
if(board[i][j+1]==2)
neighbor[i][j]++;}

if (i==0 && j>0 && j<=5){
if(board[i][j-1]==2)
neighbor[i][j]++;}

if (i==0 && j>=1 && j<6){
if(board[i+1][j+1]==2)
neighbor[i][j]++;}

if (i==0 && j>0 && j<=5){
if(board[i+1][j-1]==2)
neighbor[i][j]++;}


if(j==0 && i>=0 && i<=6){
if(board[i][j+1]==2)
neighbor[i][j]++;}

if(j==0 && i>=0 && i<6){
if(board[i+1][j]==2)
neighbor[i][j]++;}

if(j==0 && i>0 && i<=6){
if(board[i-1][j]==2)
neighbor[i][j]++;}

if(j==0 && i>=0 && i<6){
if(board[i+1][j+1]==2)
neighbor[i][j]++;}

if(j==0 && i>0 && i<=6){
if(board[i-1][j+1]==2)
neighbor[i][j]++;}


if(i==6 && j>=1 && j<=5){
if(board[i-1][j]==2)
neighbor[i][j]++;}

if(i==6 && j>=1 && j<6){
if(board[i][j+1]==2)
neighbor[i][j]++;}

if(i==6 && j>0 && j<=5){
if(board[i][j-1]==2)
neighbor[i][j]++;}

if(i==6 && j>=1 && j<6){
if(board[i-1][j+1]==2)
neighbor[i][j]++;}

if(i==6 && j>0 && j<=5){
if(board[i-1][j-1]==2)
neighbor[i][j]++;}


if(j==6 && i>=0 && i<=6){
if(board[i][j-1]==2)
neighbor[i][j]++;}

if(j==6 && i>=0 && i<6){
if(board[i+1][j]==2)
neighbor[i][j]++;}

if(j==6 && i>0 && i<=6){
if(board[i-1][j]==2)
neighbor[i][j]++;}

if(j==6 && i>=0 && i<6){
if(board[i+1][j-1]==2)
neighbor[i][j]++;}

if(j==6 && i>0 && i<=6){
if(board[i-1][j-1]==2)
neighbor[i][j]++;}


}printMe(board); 
System.out.println();
printIt(neighbor);
mutate(board, neighbor);
}
}
private static void printMe(int [][]board){
int gen = 0;
System.out.println("Generation " + gen + ": ");
for (int i = 0; i < 7; i++){
for (int j = 0; j < 7; j++){
System.out.print(board[i][j] + "\t");
}//end for j
System.out.println();
System.out.println();
}//end for i
gen += 1;
}//end printMe


private static void printIt(int [][]neighbor){
for (int i = 0; i < 7; i++){
for (int j = 0; j < 7; j++){
System.out.print(neighbor[i][j] + "\t");
}//end for j
System.out.println();
System.out.println();
}//end for i
}//end printMe

private static void mutate(int [][]board, int [][]neighbor){
for(int i = 0; i < 7; i++){
for(int j = 0; i < 7; j++){
neighbor[i][j] == ?????;
board[i][j] == ?????;
if(board[i][j] == 1 && neighbor[i][j] == 3)
board[i][j]=2;
if(board[i][j] == 2 && neighbor[i][j] >= 4)
board[i][j]=1;
if(board[i][j] == 2 && neighbor[i][j] <= 1)
board[i][j]=1;
}
}
}

}

Recommended Answers

All 2 Replies

Please edit your post and properly indent the code to show the nesting levels with pairs of {}

Also remove the } from the end of a statement and put it on its own line. This code hides the ending } and makes the code very hard to read.

help with is getting the correct code for all of the neighbors

One technique is to have an array of the index changes for a point to get to its neighbor. The neighbor tester would apply those changes and check for boundary before testing the contents of the array.
For example: For the row above the square being tested (changes to the x,y values): {-1, -1}, {0, -1}, (1, -1)

What I need help with is getting the correct code for all of the neighbors that each cell has and also the code for each generation to correctly mutate.

All of those if statements are making my head spin. Are you familiar with the % operator?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.