xxxxxxxxxx
// Lists
import java.util.List;
import java.util.ArrayList;
//import java.util.Random;
void setup() { // Called at the start of the program
size(1000, 1000, P3D); // Setting the main frame size
// setupFrame(); // Setting up the second frame
setRandom(); // Sets the cells to random
applyRules("6", "5 6 7"); // Default rules
// if (args != null) if (args.length != 0) readFile(args[0]); // Opens a state of the game with the executable if there is any
// autogeneration.run(); // Sets up autogeneration
}
List birth = new ArrayList(); // The birth rules
List survive = new ArrayList(); // The survive rules
int _size = 32; // The _size of the 'habitat'
boolean[][][] cells = new boolean[_size][_size][_size]; // The current state of the game
int[][][] neighbours = new int[_size][_size][_size];
// Camera movement variables
float cY, cX;
int cameraSpeed = 3; // The speed of the camera rotation
//Random rnd = new Random();
int frequency = 10; // The frequency of alive cells in Set Random
void setRandom() { // Makes all the tiles random.
int actives = 0;
for(int x = 0; x < _size; x++)
for(int y = 0; y < _size; y++)
for(int z = 0; z < _size; z++) {
setCell(x, y, z, int(random(frequency)) == 0);
actives += cells[x][y][z] ? 1 : 0;
}
}
void reset() { // Resets all the tiles
for(int x = 0; x < _size; x++)
for(int y = 0; y < _size; y++)
for(int z = 0; z < _size; z++) {
setCell(x,y,z, false);
}
}
void setSize(int i) { // Sets the size of the 'habitat'
_size = i;
nSize.setValue(i);
}
void applyRules(String b, String s) { // Applies the rules of the game from the B and S textfields.
birth.clear(); survive.clear();
if (b != "") {
String[] bs = b.split(" ");
for (final String i : bs) birth.add(parseInt(i));
} if (s != "") {
String[] ss = s.split(" ");
for (final String i : ss) survive.add(parseInt(i));
}
}
boolean getCell(int x, int y, int z) { return cells[x][y][z]; } // Gets the value of a specific cell
void setCell(int x, int y, int z, boolean state) { cells[x][y][z] = state; } // Sets a value to a specific cell
void generate() { // Generates
int actives = 0;
for(int x = 0; x < _size; x++)
for(int y = 0; y < _size; y++)
for(int z = 0; z < _size; z++) neighbours[x][y][z] = neighboursCount(x,y,z);
for(int x = 0; x < _size; x++)
for(int y = 0; y < _size; y++)
for(int z = 0; z < _size; z++) {
setCell(x,y,z, rules(getCell(x,y,z), neighbours[x][y][z]));
actives += getCell(x,y,z) ? 1 : 0;
}
}
boolean rules(boolean currentValue, int countNeighbours) { // Returns the future state of a cell
return currentValue ? survive.contains(countNeighbours) : birth.contains(countNeighbours);
}
int neighboursCount(int x, int y, int z) { // The number of neighbours that a cell has.
int c = 0;
for(int fx = -1; fx < 2; fx++)
for(int fy = -1; fy < 2; fy++)
for(int fz = -1; fz < 2; fz++) {
if (fx == 0 && fy == 0 && fz == 0) continue;
c += cells[position(x + fx)][position(y + fy)][position(z + fz)] ? 1 : 0;
}
return c;
}
int position(int i) { // Goes back to the start or to the end if a value is bigger than the array or negative.
return i == -1 ? _size-1 : i == _size ? 0 : i;
}
void draw() {
background(190);
fill(255);
stroke(0);
// automatic camera movement
//cX+=gAutoX.isSelected()?cameraSpeed/100f:0;
//cY+=gAutoY.isSelected()?cameraSpeed/100f:0;
// mouse camera movement
translate(500, 500, 0);
if (mousePressed) {
cX += (mouseX - pmouseX) * cameraSpeed / 1000f;
cY += -(mouseY - pmouseY) * cameraSpeed / 1000f;
} rotateX(cY); rotateY(cX);
translate(-15*_size/2, -15*_size/2, -15*_size/2);
// draws the cubes that are live
for (int x = 0; x < _size; x++) {
for(int y = 0; y < _size; y++) {
for(int z = 0; z < _size; z++) {
if (getCell(x,y,z)) box(15);
translate(0, 0, 15);
} translate(0, 15, -15*_size);
} translate(15, -15*_size, 0);
}
}
void keyPressed() {
if (key == ' ')
generate();
}