Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The Life game
- * @author Noah Kissinger
- * @date 2012.2.13
- */
- import java.util.Random;
- public class Life {
- private static boolean[][] matrix;
- private static int bL, bH, lL, lH, r, c;
- private static long rSeed;
- public Life(long seed, int rows, int columns, int birthLow, int birthHigh,
- int liveLow, int liveHigh) {
- rSeed = seed;
- bL = birthLow;
- bH = birthHigh;
- lL = liveLow;
- lH = liveHigh;
- r = rows;
- c = columns;
- createMatrix();
- }
- public void update() {
- updateMatrix();
- }
- public boolean[][] world() {
- return matrix;
- }
- public static void createMatrix() {
- Random seedBool = new Random(rSeed);
- boolean[][] matrix = new boolean[r][c];
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[i].length; j++) {
- matrix[i][j] = false;
- }
- }
- for (int i = 1; i < matrix.length - 1; i++) {
- for (int j = 1; j < matrix[i].length - 1; j++) {
- matrix[i][j] = seedBool.nextBoolean();
- }
- }
- }
- public static void updateMatrix() {
- Random seedBool = new Random(rSeed);
- boolean[][] matrixCopy = matrix.clone();
- for (int i = 0; i < matrix.length; i++)
- matrixCopy[i] = matrix[i].clone();
- int count = 0;
- for (int i = 1; i < matrix.length - 1; i++) {
- for (int j = 1; j < matrix[i].length - 1; j++) {
- if (matrix[i][j] == false) {
- if (matrixCopy[i - 1][j - 1] == true)
- count++;
- if (matrixCopy[i - 1][j] == true)
- count++;
- if (matrixCopy[i - 1][j + 1] == true)
- count++;
- if (matrixCopy[i][j - 1] == true)
- count++;
- if (matrixCopy[i][j + 1] == true)
- count++;
- if (matrixCopy[i + 1][j - 1] == true)
- count++;
- if (matrixCopy[i + 1][j] == true)
- count++;
- if (matrixCopy[i + 1][j + 1] == true)
- count++;
- if (count >= bL && count <= bH) {
- matrix[i][j] = true;
- for (int i1 = 0; i1 < matrix.length; i1++) {
- for (int j1 = 0; j1 < matrix[i1].length; j1++) {
- matrix[i1][j1] = false;
- }
- }
- for (int i1 = 1; i1 < matrix.length - 1; i1++) {
- for (int j1 = 1; j1 < matrix[i1].length - 1; j1++) {
- matrix[i1][j1] = seedBool.nextBoolean();
- }
- }
- } else
- matrix[i][j] = false;
- count = 0;
- }
- else {
- if (matrixCopy[i - 1][j - 1] == true)
- count++;
- if (matrixCopy[i - 1][j] == true)
- count++;
- if (matrixCopy[i - 1][j + 1] == true)
- count++;
- if (matrixCopy[i][j - 1] == true)
- count++;
- if (matrixCopy[i][j + 1] == true)
- count++;
- if (matrixCopy[i + 1][j - 1] == true)
- count++;
- if (matrixCopy[i + 1][j] == true)
- count++;
- if (matrixCopy[i + 1][j + 1] == true)
- count++;
- if (count >= lL && count <= lH)
- matrix[i][j] = true;
- else
- matrix[i][j] = false;
- count = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement