Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. // GameSize = 10x10
  2.   int gameSize = 100;
  3.   Cell[][] c = new Cell[gameSize][gameSize];
  4.   int fr = 10;
  5.   int frames = 0;
  6.  
  7.   boolean isDraving = true;
  8.  
  9.   void setup() {
  10.     size(400, 400);
  11.     frameRate(30);
  12.     noStroke();
  13.  
  14.     // random cells are alive
  15.     for (int x = 0; x < gameSize; x++) {
  16.       for (int y = 0; y < gameSize; y++) {
  17.         c[x][y] = new Cell();
  18.         if (random(1)< 0.05) {
  19.           c[x][y].alive = true;
  20.         }
  21.       }
  22.     }
  23.   }
  24.  
  25.   void draw() {
  26.     frames++;
  27.     if (!isDraving) {
  28.       if (frames % fr == 0) {
  29.         for (int x = 0; x < gameSize; x++) {
  30.           for (int y = 0; y < gameSize; y++) {
  31.  
  32.             // CALCULATE LIVING CELLES
  33.             int lc = 0;
  34.             try { // left upper
  35.               if (c[x-1][y-1].alive == true) lc++;
  36.             }
  37.             catch(Exception e) {
  38.             }
  39.  
  40.             try { //  upper
  41.               if (c[x][y-1].alive == true) lc++;
  42.             }
  43.             catch(Exception e) {
  44.             }
  45.  
  46.             try { // right upper
  47.               if (c[x+1][y-1].alive == true) lc++;
  48.             }
  49.             catch(Exception e) {
  50.             }
  51.  
  52.             try { // left
  53.               if (c[x-1][y].alive == true) lc++;
  54.             }
  55.             catch(Exception e) {
  56.             }
  57.  
  58.             try { // right
  59.               if (c[x+1][y].alive == true) lc++;
  60.             }
  61.             catch(Exception e) {
  62.             }
  63.  
  64.             try { // left down
  65.               if (c[x-1][y+1].alive == true) lc++;
  66.             }
  67.             catch(Exception e) {
  68.             }
  69.  
  70.             try { // down
  71.               if (c[x][y-1].alive == true) lc++;
  72.             }
  73.             catch(Exception e) {
  74.             }
  75.  
  76.             try { // right down
  77.               if (c[x+1][y+1].alive == true) lc++;
  78.             }
  79.             catch(Exception e) {
  80.             }
  81.  
  82.             if (lc > 0) println(lc);
  83.             c[x][y].calculate(lc);
  84.  
  85.             // DRAW CELLES
  86.             if (c[x][y].alive) {
  87.               fill(255);
  88.             } else {
  89.               fill(0);
  90.             }
  91.             rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
  92.           }
  93.           if (keyPressed) isDraving = true;
  94.         }
  95.       }
  96.     } else {
  97.  
  98.       if (mousePressed) {
  99.         int mx = floor( mouseX /(width/gameSize));
  100.         int my = floor( mouseY /(width/gameSize));
  101.  
  102.         c[mx][my].alive = true;
  103.       }
  104.  
  105.       if (keyPressed) isDraving = false;
  106.  
  107.       for (int x = 0; x < gameSize; x++) {
  108.         for (int y = 0; y < gameSize; y++) {
  109.           // DRAW CELLES
  110.           if (c[x][y].alive) {
  111.             fill(255);
  112.           } else {
  113.             fill(0);
  114.           }
  115.           rect(width/gameSize*x, height/gameSize*y, width/gameSize, height/gameSize);
  116.         }
  117.       }
  118.     }
  119.   }
  120.  
  121.  
  122.   class Cell {
  123.     boolean alive;
  124.  
  125.     void calculate(int lN) {
  126.       if (lN == 3 || lN == 2) {
  127.         // NOTHING ;D
  128.         alive = true;
  129.         //println("ALIVE");
  130.       }
  131.       if (lN < 2) {
  132.         alive = false;
  133.       }
  134.       if (lN > 3) {
  135.         alive = false;
  136.       }
  137.     }
  138.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement