Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- boolean thisGen[][] = new boolean[100][100];
- boolean nextGen[][] = new boolean[100][100];
- int state;
- boolean underPop, overPop, alive, revive;
- void setup() {
- underPop = false;
- overPop = false;
- alive = true;
- revive = true;
- size(800, 800);
- strokeWeight(1);
- frameRate(10);
- state = 0;
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- nextGen[i][j] = false;
- int rand = int(random(4));
- if (rand == 0) {
- thisGen[i][j] = true;
- }
- }
- }
- }
- void draw() {
- fill(0);
- textAlign(CENTER);
- textSize(50);
- if (state == 0) {
- text("Rules", width/2, 100);
- text("Press Enter to start", width/2, height-100);
- textSize(25);
- text("Black = true", width-100, 75);
- text("White = false", width-100, 100);
- text("If population is less than 2", width/2, 200);
- if (underPop == true) fill(0);
- else fill(255);
- rect(width/2, 250, 25, 25);
- if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 250) && (mouseY < 250 + 25) && (mousePressed)) {
- if (underPop == false) underPop = true;
- else if (underPop == true) underPop = false;
- }
- fill(0);
- text("If population bigger than 3", width/2, 300);
- if (overPop == true) fill(0);
- else fill(255);
- rect(width/2, 350, 25, 25);
- if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 350) && (mouseY < 350 + 25) && (mousePressed)) {
- if (overPop == false) overPop = true;
- else if (overPop == true) overPop = false;
- }
- fill(0);
- text("If population is 2 or 3", width/2, 400);
- if (alive == true) fill(0);
- else fill(255);
- rect(width/2, 450, 25, 25);
- if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 450) && (mouseY < 450 + 25) && (mousePressed)) {
- if (alive == false) alive = true;
- else if (alive == true) alive = false;
- }
- fill(0);
- text("If it is dead and 3 neighbours are beside it", width/2, 500);
- if (revive == true) fill(0);
- else fill(255);
- rect(width/2, 550, 25, 25);
- if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 550) && (mouseY < 550 + 25) && (mousePressed)) {
- if (revive == false) revive = true;
- else if (revive == true) revive = false;
- }
- if (keyCode == ENTER) {
- state = 1;
- }
- }
- if (state == 1) {
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- if (thisGen[i][j] == true) {
- fill(0);
- } else fill(255, 190);
- rect(i*8, j*8, 8, 8);
- }
- }
- for (int i = 1; i < thisGen.length - 1; i++) {
- for (int j = 1; j < thisGen.length - 1; j++) {
- int count = 0;
- //The middle
- if (thisGen[i-1][j-1]) count++; // top left
- if (thisGen[i][j-1]) count++; //top middle
- if (thisGen[i+1][j-1]) count++; // top right
- if (thisGen[i+1][j]) count++; // middle right
- if (thisGen[i+1][j+1]) count++; //bottom right
- if (thisGen[i][j+1]) count++; //bottom middle
- if (thisGen[i-1][j+1]) count++; //bottom left
- if (thisGen[i-1][j]) count++; // left middle
- //Rules
- if (thisGen[i][j] && count < 2) nextGen[i][j] = underPop; //popuation less than 2 = die
- if (thisGen[i][j] && count > 3) nextGen[i][j] = overPop; //population bigger than 3 = die
- if (thisGen[i][j] && count == 2) nextGen[i][j] = alive; //population is 2 = alive
- if (thisGen[i][j] && count == 3) nextGen[i][j] = alive; //population is 3 = alive
- if (!thisGen[i][j] && count == 3) nextGen[i][j] = revive; //if it's dead and there is exatopLefty 3 beside it = alive
- }
- }
- //TOP
- for (int i = 1; i < thisGen.length - 1; i++) {
- int count = 0;
- if (thisGen[i-1][99]) count++;
- if (thisGen[i][99]) count++;
- if (thisGen[i+1][99]) count++;
- if (thisGen[i+1][0]) count++;
- if (thisGen[i+1][1]) count++;
- if (thisGen[i][1]) count++;
- if (thisGen[i-1][1]) count++;
- if (thisGen[i-1][0]) count++;
- if (thisGen[i][0]) {
- if (count < 2 || count > 3) nextGen[i][0] = false;
- else nextGen[i][0] = true;
- } else if (!thisGen[i][0] && count == 3) nextGen[i][0] = true;
- }
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- thisGen[i][j] = nextGen[i][j];
- }
- }
- //RIGHT
- for (int j = 1; j < thisGen.length - 1; j++) {
- int count = 0;
- if (thisGen[98][j-1]) count++;
- if (thisGen[99][j-1]) count++;
- if (thisGen[0][j-1]) count++;
- if (thisGen[0][j]) count++;
- if (thisGen[0][j+1]) count++;
- if (thisGen[99][j+1]) count++;
- if (thisGen[98][j+1]) count++;
- if (thisGen[98][j]) count++;
- if (thisGen[99][j]) {
- if (count < 2 ||count > 3) nextGen[99][j] = false;
- else nextGen[99][j] = true;
- } else if (!thisGen[99][j] && count == 3) nextGen[99][j] = true;
- }
- //BOTTOM
- for (int i = 1; i < thisGen.length - 1; i++) {
- int count = 0;
- if (thisGen[i-1][98]) count++;
- if (thisGen[i][98]) count++;
- if (thisGen[i+1][98]) count++;
- if (thisGen[i+1][99]) count++;
- if (thisGen[i+1][0]) count++;
- if (thisGen[i][0]) count++;
- if (thisGen[i-1][0]) count++;
- if (thisGen[i-1][99]) count++;
- if (thisGen[i][99]) {
- if (count < 2 || count > 3) nextGen[i][99] = false;
- else nextGen[i][99] = true;
- } else if (!thisGen[i][99] && count == 3) nextGen[i][99] = true;
- }
- //LEFT
- for (int j = 1; j < thisGen.length - 1; j++) {
- int count = 0;
- if (thisGen[99][j-1]) count++;
- if (thisGen[0][j-1]) count++;
- if (thisGen[1][j-1]) count++;
- if (thisGen[1][j]) count++;
- if (thisGen[1][j+1]) count++;
- if (thisGen[0][j+1]) count++;
- if (thisGen[99][j+1]) count++;
- if (thisGen[99][j]) count++;
- if (thisGen[0][j]) {
- if (count < 2 || count > 3) nextGen[0][j] = false;
- else nextGen[0][j] = true;
- } else if (!thisGen[0][j] && count == 3) nextGen[0][j] = true;
- }
- // Top left
- int topLeft = 0;
- if (thisGen[99][99]) topLeft++;
- if (thisGen[0][99]) topLeft++;
- if (thisGen[1][99]) topLeft++;
- if (thisGen[1][0]) topLeft++;
- if (thisGen[1][1]) topLeft++;
- if (thisGen[0][1]) topLeft++;
- if (thisGen[99][1]) topLeft++;
- if (thisGen[99][0]) topLeft++;
- if (thisGen[0][0]) {
- if (topLeft < 2 || topLeft > 3) nextGen[0][0] = false;
- else nextGen[0][0] = true;
- } else if (!thisGen[0][0] && topLeft == 3) nextGen[0][0] = true;
- // Top right
- int topRight = 0;
- if (thisGen[98][99]) topRight++;
- if (thisGen[99][99]) topRight++;
- if (thisGen[0][99]) topRight++;
- if (thisGen[0][0]) topRight++;
- if (thisGen[0][1]) topRight++;
- if (thisGen[99][1]) topRight++;
- if (thisGen[98][1]) topRight++;
- if (thisGen[98][0]) topRight++;
- if (thisGen[99][0]) {
- if (topRight < 2 || topRight > 3) nextGen[99][0] = false;
- else nextGen[99][0] = true;
- } else if (!thisGen[99][0] && topRight == 3) nextGen[99][0] = true;
- // Bottom left
- int bottomLeft = 0;
- if (thisGen[99][98]) bottomLeft++;
- if (thisGen[0][98]) bottomLeft++;
- if (thisGen[1][98]) bottomLeft++;
- if (thisGen[1][99]) bottomLeft++;
- if (thisGen[1][0]) bottomLeft++;
- if (thisGen[0][0]) bottomLeft++;
- if (thisGen[99][0]) bottomLeft++;
- if (thisGen[99][99]) bottomLeft++;
- if (thisGen[0][99]) {
- if (bottomLeft < 2 || bottomLeft > 3) nextGen[0][99] = false;
- else nextGen[0][99] = true;
- } else if (!thisGen[0][99] && bottomLeft == 3) nextGen[0][99] = true;
- // Bottom right
- int bottomRight = 0;
- if (thisGen[98][98]) bottomRight++;
- if (thisGen[99][98]) bottomRight++;
- if (thisGen[0][98]) bottomRight++;
- if (thisGen[0][99]) bottomRight++;
- if (thisGen[0][0]) bottomRight++;
- if (thisGen[99][0]) bottomRight++;
- if (thisGen[98][0]) bottomRight++;
- if (thisGen[98][99]) bottomRight++;
- if (thisGen[99][99]) {
- if (bottomRight < 2 || bottomRight > 3) nextGen[99][99] = false;
- else nextGen[99][99] = true;
- } else if (!thisGen[99][99] && bottomRight == 3) nextGen[99][99] = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement