Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // GameSize = 10x10
- int gameSize = 100;
- Cell[][] c = new Cell[gameSize][gameSize];
- int fr = 10;
- int frames = 0;
- boolean isDraving = true;
- void setup() {
- size(400, 400);
- frameRate(30);
- noStroke();
- // random cells are alive
- for (int x = 0; x < gameSize; x++) {
- for (int y = 0; y < gameSize; y++) {
- c[x][y] = new Cell();
- if (random(1)< 0.05) {
- c[x][y].alive = true;
- }
- }
- }
- }
- void draw() {
- frames++;
- if (!isDraving) {
- if (frames % fr == 0) {
- for (int x = 0; x < gameSize; x++) {
- for (int y = 0; y < gameSize; y++) {
- // CALCULATE LIVING CELLES
- int lc = 0;
- try { // left upper
- if (c[x-1][y-1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // upper
- if (c[x][y-1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // right upper
- if (c[x+1][y-1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // left
- if (c[x-1][y].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // right
- if (c[x+1][y].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // left down
- if (c[x-1][y+1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // down
- if (c[x][y-1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- try { // right down
- if (c[x+1][y+1].alive == true) lc++;
- }
- catch(Exception e) {
- }
- if (lc > 0) println(lc);
- c[x][y].calculate(lc);
- // DRAW CELLES
- if (c[x][y].alive) {
- fill(255);
- } else {
- fill(0);
- }
- rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
- }
- if (keyPressed) isDraving = true;
- }
- }
- } else {
- if (mousePressed) {
- int mx = floor( mouseX /(width/gameSize));
- int my = floor( mouseY /(width/gameSize));
- c[mx][my].alive = true;
- }
- if (keyPressed) isDraving = false;
- for (int x = 0; x < gameSize; x++) {
- for (int y = 0; y < gameSize; y++) {
- // DRAW CELLES
- if (c[x][y].alive) {
- fill(255);
- } else {
- fill(0);
- }
- rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
- }
- }
- }
- }
- class Cell {
- boolean alive;
- void calculate(int lN) {
- if (lN == 3 || lN == 2) {
- // NOTHING ;D
- alive = true;
- //println("ALIVE");
- }
- if (lN < 2) {
- alive = false;
- }
- if (lN > 3) {
- alive = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement