Skip to content

Commit

Permalink
version 0.0.0.11 (pre-alpha)
Browse files Browse the repository at this point in the history
changelog:
added: speed controls, info about app
code cleanup, commentary added
  • Loading branch information
kadavr95 committed Dec 14, 2016
1 parent bc0d0d3 commit 60021f5
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 338 deletions.
198 changes: 72 additions & 126 deletions src/com/coursework/main/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,234 +2,180 @@

import javax.swing.*;
import java.awt.*;

/**
* Created by Yaskovich Dmitry on 17/11/2016.
*/
public class Field extends JPanel{
private int boardWidthInCells = 200;
/*
Life model is running on torus surface, where left side is connected with right and bottom side is connected with top one
*/
public class Field extends JPanel{ //field of cells class
private int boardWidthInCells = 200; //board size in number of cells
private int boardHeightInCells = 100;
FieldElement[][] FieldArray = new FieldElement[boardWidthInCells][boardHeightInCells];
FieldElement[][] FieldArrayNext = new FieldElement[boardWidthInCells][boardHeightInCells];
private int cellSize = 8;
public Field(){
for (int i=0;i<boardWidthInCells;i++)
private FieldElement[][] FieldArray = new FieldElement[boardWidthInCells][boardHeightInCells]; //array of cells
private FieldElement[][] FieldArrayNext = new FieldElement[boardWidthInCells][boardHeightInCells]; //array of cells for next step calculation
Field(){ //creation new instance of field
for (int i=0;i<boardWidthInCells;i++) //cycle through all array elements
{
for (int j=0;j<boardHeightInCells;j++)
{
FieldArray[i][j]=new FieldElement();
FieldArray[i][j]=new FieldElement(); //create instances of cell for both of arrays
FieldArrayNext[i][j]=new FieldElement();
// if ((i==0)|| (i==49)|| (i==99)|| (i==149) || (i==199) ||(j==0)|| (j==49)|| (j==99) || (j==24)|| (j==74)) {
//
// FieldArray[i][j].setAlive(true);
// FieldArray[i][j].setChannelRed(i);
// FieldArray[i][j].setChannelGreen(j);
// FieldArray[i][j].setChannelBlue(i / 2 + j);
//
// if (i==149)
// {
// FieldArray[i][j].setChannelRed(0);
// FieldArray[i][j].setChannelGreen(255);
// FieldArray[i][j].setChannelBlue(0);
// }
// if (i==99)
// {
// FieldArray[i][j].setChannelRed(255);
// FieldArray[i][j].setChannelGreen(0);
// FieldArray[i][j].setChannelBlue(0);
// }
// if (i==49)
// {
// FieldArray[i][j].setChannelRed(0);
// FieldArray[i][j].setChannelGreen(0);
// FieldArray[i][j].setChannelBlue(255);
// }
//
//
// }
}
//
}
}

public void stepCalculation()
void stepCalculation() //next life iteration calculation
{
for (int i=0;i<boardWidthInCells;i++) {
for (int j = 0; j < boardHeightInCells; j++) {
FieldArrayNext[i][j].setChannelRed(0);
for (int i=0;i<boardWidthInCells;i++) //cycle through all array elements
{
for (int j = 0; j < boardHeightInCells; j++)
{
FieldArrayNext[i][j].setChannelRed(0); //wipe all previous data from array for next step
FieldArrayNext[i][j].setChannelGreen(0);
FieldArrayNext[i][j].setChannelBlue(0);
FieldArrayNext[i][j].setAlive(false);
}
}

for (int i=0;i<boardWidthInCells;i++)
for (int i=0;i<boardWidthInCells;i++) //cycle through all array elements
{
for (int j=0;j<boardHeightInCells;j++)
{


cellRecalculation(i,j);
cellRecalculation(i,j); //calculate next cell state
}
//
}
FieldElement[][] t = FieldArray;
FieldElement[][] t = FieldArray; //swap next and current arrays using additional variable
FieldArray = FieldArrayNext;
FieldArrayNext = t;
//FieldArray=FieldArrayNext;
repaint();
// for (int i=0;i<boardWidthInCells;i++) {
// for (int j = 0; j < boardHeightInCells; j++) {
// FieldArrayNext[i][j].setChannelRed(255);
// FieldArrayNext[i][j].setChannelGreen(255);
// FieldArrayNext[i][j].setChannelBlue(255);
// }
// }

repaint(); //redraw field
}
private void cellRecalculation(int xIndex, int yIndex)
{
int red=0,green=0,blue=0,quantity=0,iIndex,jIndex;

for (int i=(xIndex-1);i<=(xIndex+1);i++)
private void cellRecalculation(int xIndex, int yIndex) //calculate next cell state
{
int red=0,green=0,blue=0,quantity=0,iIndex,jIndex; //local variable of cell colors, number of cell neighbours and cell indexes
for (int i=(xIndex-1);i<=(xIndex+1);i++) //cycle throught all neighbours of a cell
{
for (int j=(yIndex-1);j<=(yIndex+1);j++)
{
if ((i!=xIndex)||(j!=yIndex))
if ((i!=xIndex)||(j!=yIndex)) //if not cell itself
{
iIndex=i;
iIndex=i; //transfer cell indexes to temporary ones
jIndex=j;
if (i<0)
if (i<0) //if X coordinate is too low
{
iIndex=i+boardWidthInCells;
iIndex=i+boardWidthInCells; //use cell from another side of field
}
if (i>=boardWidthInCells)
if (i>=boardWidthInCells) //if X coordinate is too big
{
iIndex=i-boardWidthInCells;
iIndex=i-boardWidthInCells; //use cell from another side of field
}
if (j<0)
if (j<0) //if Y coordinate is too low
{
jIndex=j+boardHeightInCells;
jIndex=j+boardHeightInCells; //use cell from another side of field
}
if (j>=boardHeightInCells)
if (j>=boardHeightInCells) //if Y coordinate is too big
{
jIndex=j-boardHeightInCells;
jIndex=j-boardHeightInCells; //use cell from another side of field
}
if ( FieldArray[iIndex][jIndex].isAlive())
if ( FieldArray[iIndex][jIndex].isAlive()) //if neighbour is alive
{
quantity++;
if (FieldArray[iIndex][jIndex].getChannelRed()>red)
quantity++; //increase quantity of neighbours
if (FieldArray[iIndex][jIndex].getChannelRed()>red) //set initial cell red channel as maximum from red channel values of cell and her neighbours
red=FieldArray[iIndex][jIndex].getChannelRed();
if (FieldArray[iIndex][jIndex].getChannelBlue()>blue)
if (FieldArray[iIndex][jIndex].getChannelBlue()>blue) //set initial cell blue channel as maximum from blue channel values of cell and her neighbours
blue=FieldArray[iIndex][jIndex].getChannelBlue();
if (FieldArray[iIndex][jIndex].getChannelGreen()>green)
if (FieldArray[iIndex][jIndex].getChannelGreen()>green) //set initial cell green channel as maximum from green channel values of cell and her neighbours
green=FieldArray[iIndex][jIndex].getChannelGreen();
// red+=FieldArray[iIndex][jIndex].getChannelRed();
// green+=FieldArray[iIndex][jIndex].getChannelGreen();
// blue+=FieldArray[iIndex][jIndex].getChannelBlue();
}
}
}
//
}

if (FieldArray[xIndex][yIndex].isAlive())
if (FieldArray[xIndex][yIndex].isAlive()) //if cell is alive
{
if ((quantity<2)||(quantity>3))
if ((quantity<2)||(quantity>3)) //if less than 2 or more than 3 neighbours
{
FieldArrayNext[xIndex][yIndex].setAlive(false);
FieldArrayNext[xIndex][yIndex].setAlive(false); //kill cell in next step array
FieldArrayNext[xIndex][yIndex].setChannelRed(0);
FieldArrayNext[xIndex][yIndex].setChannelGreen(0);
FieldArrayNext[xIndex][yIndex].setChannelBlue(0);

}
else
else //if 2 or 3 neighbours
{
FieldArrayNext[xIndex][yIndex].setAlive(FieldArray[xIndex][yIndex].isAlive());
FieldArrayNext[xIndex][yIndex].setAlive(FieldArray[xIndex][yIndex].isAlive()); //transfer cell data to next step array
FieldArrayNext[xIndex][yIndex].setChannelRed(FieldArray[xIndex][yIndex].getChannelRed());
FieldArrayNext[xIndex][yIndex].setChannelGreen(FieldArray[xIndex][yIndex].getChannelGreen());
FieldArrayNext[xIndex][yIndex].setChannelBlue(FieldArray[xIndex][yIndex].getChannelBlue());
}
}
else
else //if cell is dead
{
if (quantity==3)
if (quantity==3) //if 3 neighbours
{
FieldArrayNext[xIndex][yIndex].setAlive(true);
red-=1;
FieldArrayNext[xIndex][yIndex].setAlive(true); //spawn cell
red-=1; //reduce all cell color channels by 1
blue-=1;
green-=1;
if (red<0)
if (red<0) //prevent color channels from negative values
red=0;
if (green<0)
green=0;
if (blue<0)
blue=0;
if (red>255)
red=255;
if (green>255)
green=255;
if (blue>255)
blue=255;
FieldArrayNext[xIndex][yIndex].setChannelRed(red);
FieldArrayNext[xIndex][yIndex].setChannelRed(red); //set cell channel colors
FieldArrayNext[xIndex][yIndex].setChannelGreen(green);
FieldArrayNext[xIndex][yIndex].setChannelBlue(blue);
}
else
else //if not 3 neighbours
{
FieldArrayNext[xIndex][yIndex].setAlive(FieldArray[xIndex][yIndex].isAlive());
FieldArrayNext[xIndex][yIndex].setAlive(FieldArray[xIndex][yIndex].isAlive()); //transfer cell data to next step array
FieldArrayNext[xIndex][yIndex].setChannelRed(FieldArray[xIndex][yIndex].getChannelRed());
FieldArrayNext[xIndex][yIndex].setChannelGreen(FieldArray[xIndex][yIndex].getChannelGreen());
FieldArrayNext[xIndex][yIndex].setChannelBlue(FieldArray[xIndex][yIndex].getChannelBlue());
}
}
}

public void paint(Graphics g)
public void paint(Graphics g) //field draw
{
int cellSize = 8;
super.paint(g);

for (int i=0;i<boardWidthInCells;i++)
for (int i=0;i<boardWidthInCells;i++) //cycle throught all cells
{
for (int j=0;j<boardHeightInCells;j++)
{
g.setColor(new Color(FieldArray[i][j].getChannelRed(),FieldArray[i][j].getChannelGreen(),FieldArray[i][j].getChannelBlue()));
g.fillRect(i*cellSize-1,j*cellSize-1,cellSize-1,cellSize-1);
g.setColor(new Color(FieldArray[i][j].getChannelRed(),FieldArray[i][j].getChannelGreen(),FieldArray[i][j].getChannelBlue())); //set brush color
g.fillRect(i*cellSize-1,j*cellSize-1,cellSize-1,cellSize-1); //draw cell (decreased values for stroke around cell)
}
//
}
// g.drawLine(0, 0, 1000, 1000);
// g.drawLine(800, 0, 0, 800);
}

public void setCell(boolean alive, int xIndex, int yIndex)
public void setCell(boolean alive, int xIndex, int yIndex) //default call to set new cell
{
FieldArray[xIndex][yIndex].setAlive(alive);
FieldArray[xIndex][yIndex].setChannelRed(255);
FieldArray[xIndex][yIndex].setAlive(alive); //set cell alive
FieldArray[xIndex][yIndex].setChannelRed(255); //set default cell colors
FieldArray[xIndex][yIndex].setChannelGreen(255);
FieldArray[xIndex][yIndex].setChannelBlue(255);
repaint();
repaint(); //redraw field
}

public void setCell(boolean alive, int xIndex, int yIndex, int channelRed, int channelGreen, int channelBlue)
public void setCell(boolean alive, int xIndex, int yIndex, int channelRed, int channelGreen, int channelBlue) //set new cell
{
FieldArray[xIndex][yIndex].setAlive(alive);
FieldArray[xIndex][yIndex].setChannelRed(channelRed);
FieldArray[xIndex][yIndex].setAlive(alive); //set cell alive
FieldArray[xIndex][yIndex].setChannelRed(channelRed); //set cell colors
FieldArray[xIndex][yIndex].setChannelGreen(channelGreen);
FieldArray[xIndex][yIndex].setChannelBlue(channelBlue);
repaint();
repaint(); //redraw field
}

public void clearField ()
public void clearField () //clear field
{
for (int i=0;i<boardWidthInCells;i++) {
for (int j = 0; j < boardHeightInCells; j++) {
FieldArray[i][j].setChannelRed(0);
for (int i=0;i<boardWidthInCells;i++) //cycle through all cells
{
for (int j = 0; j < boardHeightInCells; j++)
{
FieldArray[i][j].setChannelRed(0); //wipe all data about cells
FieldArray[i][j].setChannelGreen(0);
FieldArray[i][j].setChannelBlue(0);
FieldArray[i][j].setAlive(false);
}
}
repaint();
repaint(); //redraw field
}
}
39 changes: 17 additions & 22 deletions src/com/coursework/main/FieldElement.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,58 @@
package com.coursework.main;

/**
* Created by Yaskovich Dmitry on 16/11/2016.
*/
public class FieldElement {
private int channelRed, channelGreen, channelBlue;
private boolean alive;

public boolean isAlive() {
public class FieldElement { //cell class
private int channelRed, channelGreen, channelBlue; //cell color channels
private boolean alive; //cell alive value

boolean isAlive() { //get cell alive value
return alive;
}

public int getChannelBlue() {
int getChannelBlue() { //get cell blue channel value
return channelBlue;
}

public int getChannelGreen() {
int getChannelGreen() { //get cell green channel value
return channelGreen;
}

public int getChannelRed() {
int getChannelRed() { //get cell red channel value
return channelRed;
}

public void setAlive() {
void setAlive() { //default call to set cell alive value
this.alive = false;
}

public void setChannelBlue() {
void setChannelBlue() { //default call to set cell blue channel value
this.channelBlue = 0;
}

public void setChannelGreen() {
void setChannelGreen() { //default call to set cell green channel value
this.channelGreen = 0;
}

public void setChannelRed() {
void setChannelRed() { //default call to set cell red channel value
this.channelRed = 0;
}

public void setAlive(boolean alive) {
void setAlive(boolean alive) { //set cell alive value
this.alive = alive;
}

public void setChannelBlue(int channelBlue) {
void setChannelBlue(int channelBlue) { //set cell blue channel value
this.channelBlue = channelBlue;
}

public void setChannelGreen(int channelGreen) {
void setChannelGreen(int channelGreen) { //set cell green channel value
this.channelGreen = channelGreen;
}

public void setChannelRed(int channelRed) {
void setChannelRed(int channelRed) { //set cell red channel value
this.channelRed = channelRed;
}

public void changeAlive()
{
void changeAlive() { //change cell alive value
this.alive^=alive;
}

}
Loading

0 comments on commit 60021f5

Please sign in to comment.