Guest User

Board.java

a guest
Jan 2nd, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package com.jnumerical.gameoflife;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.util.Random;
  6.  
  7. public class Board
  8. {
  9.     private int cellPix, gridWidth, gridHeight, gap, shift, chance, boardLifetime, colorLifetime, steps;
  10.     private int[][] grid = new int[gridWidth][gridHeight];
  11.     private int[][] gridNext = new int[gridWidth][gridHeight];
  12.     private Color colorBG, colorA, colorB, colorC, colorD;
  13.     private Random rand = new Random();
  14.    
  15.     public Board(int c, int cPix, int g, int bLife, int cLife) {
  16.         steps = 0;
  17.         chance = c;
  18.         cellPix = cPix;
  19.         boardLifetime = bLife;
  20.         colorLifetime = cLife;
  21.         gridWidth = Main.width / cellPix;
  22.         gridHeight = Main.height / cellPix;
  23.         grid = new int[gridWidth][gridHeight];
  24.         gridNext = new int[gridWidth][gridHeight];
  25.         gap = g;
  26.         shift = gap / 2;
  27.     }
  28.    
  29.     public void update(){
  30.         calculateNext();
  31.         setNext();
  32.         chooseColors();
  33.        
  34.         if(steps % colorLifetime == 0) createColors();
  35.         if(steps % boardLifetime == 0) randomizeGrid();
  36.         steps++;
  37.     }
  38.    
  39.     public void createColors(){
  40.         colorBG = new Color(0, 0, 0);
  41.        
  42.         int r = 0, g = 0, b = 0;
  43.         int rand6 = rand.nextInt(6);
  44.         int rand256 = rand.nextInt(256);
  45.         if(rand6 == 0){
  46.             r = 0;
  47.             g = rand256;
  48.             b = 255;
  49.         }
  50.         else if(rand6 == 1){
  51.             r = 255;
  52.             g = rand256;
  53.             b = 0;
  54.         }
  55.         else if(rand6 == 2){
  56.             r = rand256;
  57.             g = 255;
  58.             b = 0;
  59.         }
  60.         else if(rand6 == 3){
  61.             r = rand256;
  62.             g = 0;
  63.             b = 255;
  64.         }
  65.         else if(rand6 == 4){
  66.             r = 255;
  67.             g = 0;
  68.             b = rand256;
  69.         }
  70.         else if(rand6 == 5){
  71.             r = 0;
  72.             g = 255;
  73.             b = rand256;
  74.         }
  75.        
  76.         colorA = new Color((r+g)/2, (g+b)/2, (b+r)/2);
  77.         colorB = new Color(r, g, b);        
  78.         colorC = new Color((r+b)/2, (g+r)/2, (b+g)/2);
  79.         colorD = new Color((r+b)/4, (g+r)/4, (b+g)/4);
  80.     }
  81.    
  82.     public void calculateNext(){
  83.         int neighbors;
  84.         for(int i=0; i<gridWidth; i++)
  85.             for(int j=0; j<gridHeight; j++){
  86.                 neighbors = getNeighbors(i,j);
  87.                 if(neighbors==3 || (neighbors==2 && grid[i][j]>0)) gridNext[i][j] = 1;
  88.                 else gridNext[i][j] = 0;
  89.             }
  90.     }
  91.    
  92.     public int getNeighbors(int i, int j){
  93.         int num = 0;
  94.         i += gridWidth;
  95.         j += gridHeight;
  96.         if(grid[(i+1) % gridWidth][j     % gridHeight] > 0) num++;
  97.         if(grid[(i-1) % gridWidth][j     % gridHeight] > 0) num++;
  98.         if(grid[i     % gridWidth][(j+1) % gridHeight] > 0) num++;
  99.         if(grid[i     % gridWidth][(j-1) % gridHeight] > 0) num++;
  100.         if(grid[(i+1) % gridWidth][(j+1) % gridHeight] > 0) num++;
  101.         if(grid[(i-1) % gridWidth][(j-1) % gridHeight] > 0) num++;
  102.         if(grid[(i+1) % gridWidth][(j-1) % gridHeight] > 0) num++;
  103.         if(grid[(i-1) % gridWidth][(j+1) % gridHeight] > 0) num++;
  104.         return num;
  105.     }
  106.    
  107.     public void setNext(){
  108.         for(int i=0; i<gridWidth; i++)
  109.             for(int j=0; j<gridHeight; j++){
  110.                 grid[i][j] = gridNext[i][j];
  111.             }
  112.     }
  113.    
  114.     public void render(Graphics g){
  115.         g.setColor(colorBG);
  116.         g.fillRect(0, 0, Main.width, Main.height);
  117.         for(int i=0; i<gridWidth; i++){
  118.             for(int j=0; j<gridHeight; j++){
  119.                 if(grid[i][j] != 0){
  120.                     if(grid[i][j] == 1) g.setColor(colorD);
  121.                     else if(grid[i][j] == 2) g.setColor(colorC);
  122.                     else if(grid[i][j] == 3) g.setColor(colorB);
  123.                     else g.setColor(colorA);
  124.                     g.fillRect(i*cellPix +shift, j*cellPix +shift, cellPix-gap, cellPix-gap);
  125.                 }
  126.             }
  127.         }
  128.     }
  129.    
  130.     private void randomizeGrid(){
  131.         for(int i=0; i<gridWidth; i++)
  132.             for(int j=0; j<gridHeight; j++){
  133.                 if(rand.nextInt(100) < chance) grid[i][j] = 1;
  134.                 else grid[i][j] = 0;
  135.             }
  136.     }
  137.    
  138.     public void chooseColors(){
  139.         for(int i=0; i<gridWidth; i++)
  140.             for(int j=0; j<gridHeight; j++){
  141.                 if(grid[i][j] > 0) grid[i][j] = getNeighbors(i,j);
  142.             }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment