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];
- void setup() {
- size(800, 800);
- strokeWeight(1);
- frameRate(100);
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- nextGen[i][j] = false;
- int rand = int(random(5));
- if (rand == 0) {
- thisGen[i][j] = true;
- }
- }
- }
- //glider
- //thisGen[5][5] = true;
- //thisGen[6][5] = true;
- //thisGen[6][3] = true;
- //thisGen[7][4] = true;
- //thisGen[7][5] = true;
- }
- void draw() {
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- if (thisGen[i][j]) {
- fill(0);
- } else fill(255);
- 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;
- 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
- if (thisGen[i][j] && count < 2) nextGen[i][j] = false;
- if (thisGen[i][j] && count > 3) nextGen[i][j] = false;
- if (thisGen[i][j] && count == 2) nextGen[i][j] = true;
- if (thisGen[i][j] && count == 3) nextGen[i][j] = true;
- if (!thisGen[i][j] && count == 3) nextGen[i][j] = true;
- }
- }
- for (int i = 0; i < thisGen.length; i++) {
- for (int j = 0; j < thisGen.length; j++) {
- thisGen[i][j] = nextGen[i][j];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement