Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1.  
  2. public class Life
  3. {
  4.     public static void main(string[] args)
  5.     {
  6.         int row = 6;
  7.         int column = 8;
  8.         int seed = 7;
  9.         int generations = 6;
  10.         boolean[][] matrix = createMatrix(row, column);
  11.         for (int i = 0; i <=  generations ; i++)
  12.         {
  13.             // if we're in our first generation, just create the matrix
  14.             if (i == 0)
  15.             {
  16.                 matrix = fillMatrix(matrix, seed);
  17.             }
  18.             // otherwise, get the next generation
  19.             else
  20.             {
  21.                 matrix = getNextGeneration(matrix);
  22.             }
  23.             printMatrix(matrix);
  24.         }
  25.        
  26.     }
  27.  
  28.     public static boolean[][] createMatrix(int length, int width)
  29.     {
  30.         return new boolean[length][width]();
  31.     }
  32.  
  33.     public static boolean[][] fillMatrix(boolean[][] matrix, int seed)
  34.     {
  35.         Random rnd = new Random(seed);
  36.         // iterate through all rows except the first and last
  37.         for (int i = 1 ; i <= matrix.length - 1; i++)
  38.         {
  39.             // iterate through all columns except the first and last
  40.             for (int j = 1 ; j <= matrix[i].length - 1; j++)
  41.             {
  42.                 matrix[i][j] = rnd.nextBoolean();
  43.             }
  44.         }
  45.         return matrix;
  46.     }
  47.  
  48.     public static boolean[][] getNextGeneration(boolean[][] currentGeneration)
  49.     {
  50.         int neighbors = 0;
  51.         int length = currentGeneration.length;
  52.         int width = currentGeneration[0].length;
  53.         boolean[][] currentGeneration = new boolean[length][width]();
  54.  
  55.         for (int i = 1 ; i <= currentGeneration.length - 1; i++)
  56.         {
  57.             // iterate through all columns except the first and last
  58.             for (int j = 1 ; j <= currentGeneration[i].length - 1; j++)
  59.             {
  60.                 // check each of the cell's neighbors, and increment the neighbors variable above
  61.  
  62.                 // change the number in this if statement to correspond to what the lab says makes a cell die
  63.                 if (neighbors > 3)
  64.                 {
  65.                     // cell dies
  66.                     currentGeneration[i][j] = false;
  67.                 }
  68.                 else
  69.                 {
  70.                     // cell stays alive
  71.                     currentGeneration[i][j] = true;
  72.                 }
  73.             }
  74.             neighbors = 0;
  75.         }
  76.         return currentGeneration;
  77.     }
  78.  
  79.     public static void printMatrix(boolean[][] matrix)
  80.     {
  81.         for (int i = 0 ; i != matrix.length ; i++)
  82.         {
  83.             for (int j = 0 ; j != matrix[i].length ; j++)
  84.             {
  85.                 String output = "- ";
  86.                 if (matrix[i][j])
  87.                 {
  88.                     output = "# ";
  89.                 }
  90.                 System.out.print(output);
  91.             }
  92.             // print this after each row so that it prints the next row on a new line
  93.             System.out.println("");
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement