package com.jnumerical.gameoflife; import java.awt.Color; import java.awt.Graphics; import java.util.Random; public class Board { private int cellPix, gridWidth, gridHeight, gap, shift, chance, boardLifetime, colorLifetime, steps; private int[][] grid = new int[gridWidth][gridHeight]; private int[][] gridNext = new int[gridWidth][gridHeight]; private Color colorBG, colorA, colorB, colorC, colorD; private Random rand = new Random(); public Board(int c, int cPix, int g, int bLife, int cLife) { steps = 0; chance = c; cellPix = cPix; boardLifetime = bLife; colorLifetime = cLife; gridWidth = Main.width / cellPix; gridHeight = Main.height / cellPix; grid = new int[gridWidth][gridHeight]; gridNext = new int[gridWidth][gridHeight]; gap = g; shift = gap / 2; } public void update(){ calculateNext(); setNext(); chooseColors(); if(steps % colorLifetime == 0) createColors(); if(steps % boardLifetime == 0) randomizeGrid(); steps++; } public void createColors(){ colorBG = new Color(0, 0, 0); int r = 0, g = 0, b = 0; int rand6 = rand.nextInt(6); int rand256 = rand.nextInt(256); if(rand6 == 0){ r = 0; g = rand256; b = 255; } else if(rand6 == 1){ r = 255; g = rand256; b = 0; } else if(rand6 == 2){ r = rand256; g = 255; b = 0; } else if(rand6 == 3){ r = rand256; g = 0; b = 255; } else if(rand6 == 4){ r = 255; g = 0; b = rand256; } else if(rand6 == 5){ r = 0; g = 255; b = rand256; } colorA = new Color((r+g)/2, (g+b)/2, (b+r)/2); colorB = new Color(r, g, b); colorC = new Color((r+b)/2, (g+r)/2, (b+g)/2); colorD = new Color((r+b)/4, (g+r)/4, (b+g)/4); } public void calculateNext(){ int neighbors; for(int i=0; i0)) gridNext[i][j] = 1; else gridNext[i][j] = 0; } } public int getNeighbors(int i, int j){ int num = 0; i += gridWidth; j += gridHeight; if(grid[(i+1) % gridWidth][j % gridHeight] > 0) num++; if(grid[(i-1) % gridWidth][j % gridHeight] > 0) num++; if(grid[i % gridWidth][(j+1) % gridHeight] > 0) num++; if(grid[i % gridWidth][(j-1) % gridHeight] > 0) num++; if(grid[(i+1) % gridWidth][(j+1) % gridHeight] > 0) num++; if(grid[(i-1) % gridWidth][(j-1) % gridHeight] > 0) num++; if(grid[(i+1) % gridWidth][(j-1) % gridHeight] > 0) num++; if(grid[(i-1) % gridWidth][(j+1) % gridHeight] > 0) num++; return num; } public void setNext(){ for(int i=0; i 0) grid[i][j] = getNeighbors(i,j); } } }