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

You will write a Java program that implements Conway’s Game of Life, a simple ce

ID: 3764061 • Letter: Y

Question

You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton discussed in class. See for example: http://www.bitstorm.org/gameoflife/ Our version has a 10 x 10 grid, numbered like this: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 The grid is represented by a 10 x 10 2­dimensional integer array. If the grid point (i, j) is “populated”, the array element [i][j] contains 1; otherwise it contains 0. Elements along the edges, i == 0 or 9, or j == 0 or 9, are always unpopulated. When we display the grid, a populated cell is indicated by a ‘#’; an unpopulated cell is indicated by a space. What your program should do:

Prompt the user to enter a list of (i,j) pairs, both non­negative integers (stop when a negative integer is read for either i or j)

Prompt the user to enter the number of time steps Initialize the grid based on the (i,j) pairs entered by the user (set the corresponding array elements to 1) Display the initial state of the grid (call the displayGrid() method)

For each time step,

update the grid according to Conway’s rules (call the updateGrid() method)

display the grid (call the displayGrid() method) We follow Conway’s standard rules for updating the cells, as follows. A “neighbor” is a vertically, horizontally, or diagonally adjacent cell that is populated. A cell has anywhere from 0 to 8 neighbors.

For a cell that is “populated”, if the cell has <= 1 neighbors, or >= 4 neighbors, it dies (becomes 0). Otherwise, it survives (remains 1).

For a cell that is not populated, if the cell has exactly 3 neighbors, it becomes populated (becomes 1).

Cells on the edge always remain unpopulated (0). The displayGrid() method has prototype: public static void displayGrid(int mat[][]) It displays the borders of the grid (see sample runs below), and prints the 10 x 10 grid of cells. Populated cells are indicated with a ‘#’ sign, unpopulated cells with a space. The updateGrid() method has prototype: public static void updateGrid(int mat[][]) mat is the 2­dimensional array that contains the current state of the grid. The method counts the neighbors in each cell of the grid, and updates the cells in the grid according to Conway’s rules. You can obviously enter arbitrary initial conditions to test your program. Some fun examples adapted from the bitstorm.org site: Glider: initial populated cells at (3, 4) (4, 2) (4, 4) (5, 3) (5, 4) 5 cell row: initial populated cells at (6,4) (6,5) (6,6) (6,7) (6,8) Small explosion: initial populated cells at (4, 5) (4,6) (5,4) (5,5) (5, 7) (6, 5) (6,6) Note that these will not behave exactly the same as the bitstorm.org display when the populated cells run into the boundary conditions.

Explanation / Answer

import java.util.*;
import java.io.*;

public class ConwaysGameofLife
{
  
   int l=20,b=60;
   public static void main(String[] args)
   {

       ConwaysGameofLife now=new ConwaysGameofLife();
       now.setConways();
   }
   void setConways()
   {
       char[][] config=new char[l][b];
       startConways(config,l,b);
   }
   void startConways(char[][] mat,int l, int b)
   {
       Scanner s=new Scanner(System.in);
       String ch="";
       float per=0;
       while(!ch.equals("y"))
       {
           per=setConfig(mat);
             
           display2D(mat);
           System.out.println((per*100)+"% of grid filled.");
           System.out.println("Begin? y/n");
           ch=s.nextLine();
       }
       while(!ch.equals("x"))
       {
           mat=transform(mat,l,b);
           display2D(mat);

           System.out.println("Ctrl+Z to stop.");

           try
           {
               Thread.sleep(100);
           }
           catch(Exception e)
           {
               System.out.println("Something went horribly wrong.");
           }

             
       }
       s.close();
       System.out.println("Game Over");
   }

   char[][] transform(char[][] mat,int l, int b)
   {

       char[][] newmat=new char[l][b];
       for(int k=0;k<l;k++)
           for(int j=0;j<b;j++)
               newmat[i][j]=flip(mat,k,j);
       return newmat;
   }
   char flip(char[][] mat,int k, int j)
   {
       int count=around(mat,k,j);
       if(mat[k][j]=='*')
       {
           if(count<2||count>3)
               return '_';
           return '*';
       }
       else
       {
           if(count==3)
               return '*';
           return '_';
       }
   }
   int around(char[][] mat, int k, int j)
   {
       int count=0;
       for(int x=k-1;x<=k+1;x++)
           for(int y=j-1;y<=j+1;y++)
           {
               if(x==i&&y==j)
                   continue;
               count+=eval(mat,x,y);
           }
       return count;
   }
   int eval(char[][] mat, int k, int j)
   {
       if(k<0||j<0||k==l||j==b)
           return 0;
       if(mat[k][j]=='*')
           return 1;
       return 0;
   }

   float setCustomConfig(char[][] arr,String infile)
   {
       try
       {
           BufferedReader br=new BufferedReader(new FileReader(infile));
           String line;
           for(int k=0;k<arr.length;k++)
           {
               line=br.readLine();
               for(int j=0;j<arr[0].length;j++)
                   arr[k][j]=line.charAt(j);
           }
           br.close();
       }
       catch(Exception e)
       {
           System.out.println(e.getMessage());
       }
       return 0;
   }

   float setConfig(char[][] arr)
   {
       .
       float per=0.10f;
       for(int k=0;k<arr.length;k++)
           setConfig1D(arr[k],per);
       return per;
   }
   void setConfig1D(char[] arr,float per)
   {
       for(int k=0;k<arr.length;k++)
       {
           if(Math.random()<per)
               arr[k]='*';
           else
               arr[k]='_';
       }
   }
   void display2D(char[][] arr)
   {
       for(int k=0;k<arr.length;k++)
           display1D(arr[k]);
       System.out.println();
   }
   void display1D(char[] arr)
   {
       for(int k=0;k<arr.length;k++)
           System.out.print(arr[k]);
       System.out.println();
   }
}

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