Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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; i<gridWidth; i++)
- for(int j=0; j<gridHeight; j++){
- neighbors = getNeighbors(i,j);
- if(neighbors==3 || (neighbors==2 && grid[i][j]>0)) 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<gridWidth; i++)
- for(int j=0; j<gridHeight; j++){
- grid[i][j] = gridNext[i][j];
- }
- }
- public void render(Graphics g){
- g.setColor(colorBG);
- g.fillRect(0, 0, Main.width, Main.height);
- for(int i=0; i<gridWidth; i++){
- for(int j=0; j<gridHeight; j++){
- if(grid[i][j] != 0){
- if(grid[i][j] == 1) g.setColor(colorD);
- else if(grid[i][j] == 2) g.setColor(colorC);
- else if(grid[i][j] == 3) g.setColor(colorB);
- else g.setColor(colorA);
- g.fillRect(i*cellPix +shift, j*cellPix +shift, cellPix-gap, cellPix-gap);
- }
- }
- }
- }
- private void randomizeGrid(){
- for(int i=0; i<gridWidth; i++)
- for(int j=0; j<gridHeight; j++){
- if(rand.nextInt(100) < chance) grid[i][j] = 1;
- else grid[i][j] = 0;
- }
- }
- public void chooseColors(){
- for(int i=0; i<gridWidth; i++)
- for(int j=0; j<gridHeight; j++){
- if(grid[i][j] > 0) grid[i][j] = getNeighbors(i,j);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment