Advertisement
noahjk

Life

Feb 13th, 2012
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. /**
  2.  * The Life game
  3.  * @author Noah Kissinger
  4.  * @date 2012.2.13
  5.  */
  6.  
  7. import java.util.Random;
  8.  
  9. public class Life {
  10.  
  11.     private static boolean[][] matrix;
  12.     private static int bL, bH, lL, lH, r, c;
  13.     private static long rSeed;
  14.    
  15.     public Life(long seed, int rows, int columns, int birthLow, int birthHigh,
  16.             int liveLow, int liveHigh) {
  17.        
  18.         rSeed = seed;
  19.         bL = birthLow;
  20.         bH = birthHigh;
  21.         lL = liveLow;
  22.         lH = liveHigh;
  23.         r = rows;
  24.         c = columns;
  25.  
  26.         createMatrix();
  27.  
  28.     }
  29.  
  30.     public void update() {
  31.         updateMatrix();
  32.     }
  33.  
  34.     public boolean[][] world() {
  35.         return matrix;
  36.     }
  37.  
  38.     public static void createMatrix() {
  39.  
  40.         Random seedBool = new Random(rSeed);
  41.  
  42.         boolean[][] matrix = new boolean[r][c];
  43.  
  44.         for (int i = 0; i < matrix.length; i++) {
  45.             for (int j = 0; j < matrix[i].length; j++) {
  46.                 matrix[i][j] = false;
  47.             }
  48.         }
  49.  
  50.         for (int i = 1; i < matrix.length - 1; i++) {
  51.             for (int j = 1; j < matrix[i].length - 1; j++) {
  52.                 matrix[i][j] = seedBool.nextBoolean();
  53.             }
  54.         }
  55.  
  56.     }
  57.  
  58.     public static void updateMatrix() {
  59.  
  60.         Random seedBool = new Random(rSeed);
  61.  
  62.         boolean[][] matrixCopy = matrix.clone();
  63.         for (int i = 0; i < matrix.length; i++)
  64.             matrixCopy[i] = matrix[i].clone();
  65.  
  66.         int count = 0;
  67.  
  68.         for (int i = 1; i < matrix.length - 1; i++) {
  69.             for (int j = 1; j < matrix[i].length - 1; j++) {
  70.  
  71.                 if (matrix[i][j] == false) {
  72.  
  73.                     if (matrixCopy[i - 1][j - 1] == true)
  74.                         count++;
  75.                     if (matrixCopy[i - 1][j] == true)
  76.                         count++;
  77.                     if (matrixCopy[i - 1][j + 1] == true)
  78.                         count++;
  79.                     if (matrixCopy[i][j - 1] == true)
  80.                         count++;
  81.                     if (matrixCopy[i][j + 1] == true)
  82.                         count++;
  83.                     if (matrixCopy[i + 1][j - 1] == true)
  84.                         count++;
  85.                     if (matrixCopy[i + 1][j] == true)
  86.                         count++;
  87.                     if (matrixCopy[i + 1][j + 1] == true)
  88.                         count++;
  89.  
  90.                     if (count >= bL && count <= bH) {
  91.                         matrix[i][j] = true;
  92.  
  93.                         for (int i1 = 0; i1 < matrix.length; i1++) {
  94.                             for (int j1 = 0; j1 < matrix[i1].length; j1++) {
  95.                                 matrix[i1][j1] = false;
  96.                             }
  97.                         }
  98.  
  99.                         for (int i1 = 1; i1 < matrix.length - 1; i1++) {
  100.                             for (int j1 = 1; j1 < matrix[i1].length - 1; j1++) {
  101.                                 matrix[i1][j1] = seedBool.nextBoolean();
  102.                             }
  103.                         }
  104.                     } else
  105.                         matrix[i][j] = false;
  106.                     count = 0;
  107.  
  108.                 }
  109.  
  110.                 else {
  111.  
  112.                     if (matrixCopy[i - 1][j - 1] == true)
  113.                         count++;
  114.                     if (matrixCopy[i - 1][j] == true)
  115.                         count++;
  116.                     if (matrixCopy[i - 1][j + 1] == true)
  117.                         count++;
  118.                     if (matrixCopy[i][j - 1] == true)
  119.                         count++;
  120.                     if (matrixCopy[i][j + 1] == true)
  121.                         count++;
  122.                     if (matrixCopy[i + 1][j - 1] == true)
  123.                         count++;
  124.                     if (matrixCopy[i + 1][j] == true)
  125.                         count++;
  126.                     if (matrixCopy[i + 1][j + 1] == true)
  127.                         count++;
  128.  
  129.                     if (count >= lL && count <= lH)
  130.                         matrix[i][j] = true;
  131.                     else
  132.                         matrix[i][j] = false;
  133.                     count = 0;
  134.  
  135.                 }
  136.             }
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement