Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The game of life, invented by J. Conway, is intended to model life in a society

ID: 3543355 • Letter: T

Question

The game of life, invented by J. Conway, is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is viewed as extending indefinitely in both directions, then each cell has eight neighbors, the eight cells surrounding it. In each generation, births and deaths occur according to the following rules:

Write a program to play the game of life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; other change shape and size and may move across the array; and others may produce gliders that detach themselves from the society and sail off into space!

Max dimensions of the array

Initial layout of the organisms

#of generations to be generated

The shape of the organisms in each generation.

The shape of the organisms at the end.

Explanation / Answer

please rate - thanks


any question ask


import java.util.*;
public class gameOfLife
{public static void main(String []args)
{Scanner in=new Scanner(System.in);
int i;
System.out.print("Enter rows in your world: ");
int rows=in.nextInt();
System.out.print("Enter columns in your world: ");
int cols=in.nextInt();
System.out.print("Enter number of generations in your world: ");
int gens=in.nextInt();

char[][] world=new char[rows][cols];
initialize(world,rows,cols,in);
for(i=0;i<gens;i++)
   {
    display(world,rows,cols,i);
   System.out.println("Press enter for next generation: ");
   in.nextLine();
   generation(world,rows,cols);
   }
display(world,rows,cols,i);
}
public static int check(int i,int j,char world[][])
{if(world[i][j]==' ')
     return 0;
return 1;
}
public static void generation(char world[][ ],int rows,int cols)
{int i,j,count;
for(i=0;i<rows;i++)
    for(j=0;j<cols;j++)
        {count=0;
         if(i!=0)
             {if(j!=0)
                  count+=check(i-1,j-1,world);
              count+=check(i-1,j,world);
              if(j!=cols-1)
                  count+=check(i-1,j+1,world);
              }
         if(j!=0)   
              count+=check(i,j-1,world);
         if(j!=cols-1&&i!=0)  
              count+=check(i-1,j+1,world);
         if(i!=rows-1 )
              {if(j!=0)
                 count+=check(i+1,j-1,world);
              count+=check(i+1,j,world);
              if(j!=cols-1)
                  count+=check(i+1,j+1,world);
              }
         if(world[i][j]=='*'&&(count<2||count>3))
              world[i][j]=' ';
         if(world[i][j]==' '&&count==3)
                  world[i][j]='*';
          }
        }

public static void display(char world[][],int rows,int cols,int gen)
{int i,j;
System.out.println("After generation "+gen);
for(i=0;i<rows;i++)
    {for(j=0;j<cols;j++)
       System.out.print(world[i][j]);
     System.out.println();
     }
}
public static void initialize(char world[][],int rows,int cols,Scanner in)
{int i,j;
for(i=0;i<rows;i++)
    for(j=0;j<cols;j++)
         world[i][j]=' ';
System.out.println("Enter row (starting at 0) and column(starting at 0)");
System.out.println("for populated cells-enter a negative number for the row");
System.out.println("when all cells are populated");
System.out.print("row: ");
i=in.nextInt();
while(i>=0)
   {System.out.print("col: ");
    j=in.nextInt();
    if(i>=0&&i<rows&&j>=0&&j<cols)
        world[i][j]='*';
    else
       System.out.println("cell["+i+"]["+j+"] is invalid");
     System.out.print("row: ");
    i=in.nextInt();
    }
in.nextLine();
}     
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote