<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
      <title>Tagged with #conway - Processing 2.x and 3.x Forum</title>
      <link>https://forum.processing.org/two/discussions/tagged/feed.rss?Tag=%23conway</link>
      <pubDate>Sun, 08 Aug 2021 20:18:04 +0000</pubDate>
         <description>Tagged with #conway - Processing 2.x and 3.x Forum</description>
   <language>en-CA</language>
   <atom:link href="/two/discussions/tagged%23conway/feed.rss" rel="self" type="application/rss+xml" />
   <item>
      <title>Conway's game of life - suggest code improvements</title>
      <link>https://forum.processing.org/two/discussion/26146/conway-s-game-of-life-suggest-code-improvements</link>
      <pubDate>Sun, 28 Jan 2018 00:40:37 +0000</pubDate>
      <dc:creator>Tsskyx</dc:creator>
      <guid isPermaLink="false">26146@/two/discussions</guid>
      <description><![CDATA[<p>After like half a year of not programming with Processing at all, I got back into it to see if I'm still good at it.</p>

<p>I tried to make Conway's game of life. Look at the code and suggest improvements. I'm really not happy with a few things in it, for example the code that checks the state of neighboring cells for each one:</p>

<pre><code>class Cell {
  PVector dim, pos;
  boolean state, change;
  int[] id = new int[2];

  Cell(int x, int y) {
    id[0] = x;
    id[1] = y;
    dim = new PVector(width/float(cols), height/float(rows));
    pos = new PVector(x*dim.x, y*dim.y);
  }

  boolean mouseInside() {
    return mouseX &gt; pos.x &amp;&amp; mouseX &lt; pos.x+dim.x &amp;&amp; mouseY &gt; pos.y &amp;&amp; mouseY &lt; pos.y+dim.y;
  }

  void iterate() {
    int count = 0;
    if (id[0]-1 &gt; -1 &amp;&amp; id[1]-1 &gt; -1 &amp;&amp; grid[id[0]-1][id[1]-1].state) {
      count++;
    }
    if (id[1]-1 &gt; -1 &amp;&amp; grid[id[0]][id[1]-1].state) {
      count++;
    }
    if (id[0]+1 &lt; rows &amp;&amp; id[1]-1 &gt; -1 &amp;&amp; grid[id[0]+1][id[1]-1].state) {
      count++;
    }
    if (id[0]-1 &gt; -1 &amp;&amp; grid[id[0]-1][id[1]].state) {
      count++;
    }
    if (id[0]+1 &lt; rows &amp;&amp; grid[id[0]+1][id[1]].state) {
      count++;
    }
    if (id[0]-1 &gt; -1 &amp;&amp; id[1]+1 &lt; cols &amp;&amp; grid[id[0]-1][id[1]+1].state) {
      count++;
    }
    if (id[1]+1 &lt; cols &amp;&amp; grid[id[0]][id[1]+1].state) {
      count++;
    }
    if (id[0]+1 &lt; rows &amp;&amp; id[1]+1 &lt; cols &amp;&amp; grid[id[0]+1][id[1]+1].state) {
      count++;
    }
    if (state &amp;&amp; (count &lt; 2 || count &gt; 3) || !state &amp;&amp; count == 3) {
      change = true;
    }
  }

  void display() {
    if (change) {
      state = !state;
      change = false;
    }
    fill(state? color(255, 255, 0) : 200);
    stroke(175);
    rect(pos.x, pos.y, dim.x, dim.y);
  }
}

int rows = 40;
int cols = 40;
boolean drawMode;

Cell[][] grid = new Cell[cols][rows];

void setup() {
  size(800, 800);
  for (int i = 0; i &lt; cols; i++) {
    for (int j = 0; j &lt; rows; j++) {
      grid[i][j] = new Cell(i, j);
    }
  }
}

void draw() {
  background(0);
  for (int i = 0; i &lt; cols; i++) {
    for (int j = 0; j &lt; rows; j++) {
      if (mousePressed &amp;&amp; mouseButton != CENTER &amp;&amp; grid[i][j].mouseInside()) {
        grid[i][j].state = drawMode;
      }
      grid[i][j].display();
    }
  }
}

void mousePressed() {
  switch (mouseButton) {
  case LEFT:
    drawMode = true;
    break;
  case RIGHT:
    drawMode = false;
    break;
  case CENTER:
    for (int i = 0; i &lt; cols; i++) {
      for (int j = 0; j &lt; rows; j++) {
        grid[i][j].state = false;
      }
    }
  }
}

void keyPressed() {
  for (int i = 0; i &lt; cols; i++) {
    for (int j = 0; j &lt; rows; j++) {
      if (key == ' ') {
        grid[i][j].iterate();
      }
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Game of Life - Can't find the error</title>
      <link>https://forum.processing.org/two/discussion/24696/game-of-life-can-t-find-the-error</link>
      <pubDate>Sun, 22 Oct 2017 23:23:10 +0000</pubDate>
      <dc:creator>DerBlaue</dc:creator>
      <guid isPermaLink="false">24696@/two/discussions</guid>
      <description><![CDATA[<p>As the title says I'm trying to program "Conways Game Of Life" but the cells don't behave as they should. I can't find my mistake. I hope one of you can find it :)</p>

<p>Seems like the code is too long so I made a pdf: <a rel="nofollow" href="https://drive.google.com/file/d/0B9pKdSOwgbtydjN1aU03TGQtSUk/view?usp=sharing">https://drive.google.com/file/d/0B9pKdSOwgbtydjN1aU03TGQtSUk/view?usp=sharing</a></p>
]]></description>
   </item>
   <item>
      <title>Cannot read property '0' of undefined</title>
      <link>https://forum.processing.org/two/discussion/23165/cannot-read-property-0-of-undefined</link>
      <pubDate>Thu, 22 Jun 2017 08:23:20 +0000</pubDate>
      <dc:creator>cameronnichols</dc:creator>
      <guid isPermaLink="false">23165@/two/discussions</guid>
      <description><![CDATA[<p>I don't understand why I'm getting this error. I am attempting to make Conway's Game Of Life, and this requires a grid. So in the process of drawing the grid, it makes it to the last column, and 2nd row (index 1) and produces the error in the title. I don't understand why, I even put 'grid' in the console log and none of the elements were undefined.</p>

<pre><code>function Cell(i,j){
  this.r = j;
  this.c = i;
  this.x = this.c*res;
  this.y = this.r*res;
  this.state = floor(random(0,1)+.5);
  this.next;
  this.getNeighborhood = () =&gt; {
    let temp = [];
    for (let k = -1; k &lt;=1; k++){
      for (let l = -1; l &lt;= 1; l++){
        if (inRange(k,0,cols) &amp;&amp; inRange(l,0,rows)){
          temp.push(grid[this.c+k][this.r+l]);
        }
      }
    }
    return temp;
  }
  this.show = () =&gt; {
    stroke(0);
    fill(this.state*255);
    rect(this.x,this.y,res,res);
  }
}

function inRange(val,min,max){
  return (val&gt;=min&amp;&amp;val&lt;max)
}
</code></pre>

<p>Those two functions are in a separate file from the main sketch.</p>

<p>Here's the main sketch:</p>

<pre><code>var res = 10;
var cols;
var rows;
var grid;

function setup(){
  createCanvas(600,600);
  cols = floor(width/res);
  rows = floor(height/res);
  grid = new Array(cols);
  for (let i = 0; i &lt; grid.length; i++){
    grid[i] = new Array(rows);
    for (let j = 0; j &lt; grid[i].length; j++){
      grid[i][j] = new Cell(i,j);
    }
  }
}

function draw(){
  background(255);
  for (let i = 0; i &lt; grid.length; i++){
    for (let j = 0; j &lt; grid[i].length; j++){
      let cell = grid[i][j];
      cell.show();
      cell.next = computeState(cell);
      cell.state = cell.next;
      cell.show();
    }
  }
}
</code></pre>
]]></description>
   </item>
   <item>
      <title>Optimize my Conway Game of Life in Quil in Clojure</title>
      <link>https://forum.processing.org/two/discussion/22937/optimize-my-conway-game-of-life-in-quil-in-clojure</link>
      <pubDate>Mon, 05 Jun 2017 11:47:13 +0000</pubDate>
      <dc:creator>Aljones15</dc:creator>
      <guid isPermaLink="false">22937@/two/discussions</guid>
      <description><![CDATA[<p>Ok so in clojure REPL (time (conway-state cells)) returns about 0.43 milliseconds more than fast enough for 15 FPS. However the actual draw loop seems to be really lagging on drawing my rects. code below:``</p>

<pre><code>  1(ns hello-quil.core                                                                                                  
  2                                                                                                                     
  3 (:require [quil.core :as q]                                                                                         
  4            [quil.middleware :as m]                                                                                  
  5            [clojure.core.reducers :as r]))                                                                          
  6                                                                                                                     
  7(def scaler 10.0)                                                                                                    
  8(defstruct cell :index :alive :x :y)                                                                                 
  9(defn scale-w [] (Math/floor (/ 640 scaler) ))                                                                       
 10(defn scale-h [] (Math/floor (/ 360 scaler) ))                                                                       
 11                                                                                                                     
 12                                                                                                                     
 13(defn scaledCoord [coord] (if (= coord 0) coord (* scaler coord)))                                                   
 14                                                                                                                     
 15(def initState                                                                                                       
 16  ; the initial state                                                                                                
 17  (map-indexed                                                                                                       
 18    (fn [index num]                                                                                                  
 19      (struct cell index (rand-int 2) (scaledCoord (mod index (scale-w)))                                            
 20        (scaledCoord(Math/floor (/ index (scale-w))))))                                                              
 21    (range 0 (* (scale-w) (scale-h))))                                                                               
 22)                                                                                                                    
 23                                                                                                                     
 24(defn setup []                                                                                                       
 25  ; Set frame rate to 30 frames per second.                                                                          
 26  (q/frame-rate 15)                                                                                                  
 27  ; Set color mode to HSB (HSV) instead of default RGB.                                                              
 28  (q/color-mode :hsb)                                                                                                
 29  ; setup function returns initial state. It contains                                                                
 30  {:cells (into [] initState)}                                                                                       
 31)                                                                                                                    
 32                                                                                                                     
 33(defn getNeighbor [x y cells]                                                                                        
 34  ; gets the neigbor or return a random alive if no neighbor                                                         
 35  (let [result (into [] (r/filter (fn [c] (and (= x (:x c)) (= y (:y c)))) cells))]                                  
 36  (if (empty? result) (rand-int 2) (:alive (first result))))                                                         
 37)                                                                                                                    
 38                                                                                                                     
 39                                                                                                                     
 40(defn getNeighbors [c cells]                                                                                         
 41  ; get neighbors in 8 directions                                                                                    
 42  (let [x (:x c) y (:y c) left (- x scaler) right (+ x scaler) up (+ y scaler) down (- y scaler)]                    
 43    (map                                                                                                             
 44      (fn [[newX newY]](getNeighbor newX newY cells))                                                                
 45      [[right y] [left y] [right up] [left up] [x up] [x down] [right down] [left down]]))                           
 46)                                                                                                                    
 47                                                                                                                     
 48                                                                                                                     
 49(defmulti cell-state                                                                                                 
 50  ; takes a vector of neighbors and sums them                                                                        
 51  (fn [c cells] (reduce + cells))                                                                                    
 52)                                                                                                                    
 53; if there are 2 the cell stays the same                                                                             
 54(defmethod cell-state 2 [c cells] c)                                                                                 
 55; if there are 3 neighbors we reproduce 1                                                                            
 56(defmethod cell-state 3 [c _] (assoc c :alive 1))                                                                    
 57; everything else it dies                                                                                            
 58(defmethod cell-state :default [c _] (assoc c :alive 0))                                                             
 59                                                                                                                      60(defn update-cell [cells]                                                                                            
 61  ; curried func that takes the state, then return a mappable func                                                   
 62  (fn [c] (let [neighbors (getNeighbors c cells)]                                                                    
 63    (cell-state c neighbors)))                                                                                       
 64)                                                                                                                    
 65                                                                                                                     
 66(defn conway-state [cells]                                                                                           
 67  ; update conway's game of life                                                                                     
 68  (map (update-cell cells) cells)                                                                                    
 69)                                                                                                                    
 70                                                                                                                     
 71(defn update-state [state]                                                                                           
 72  ; Update sketch state by changing circle color and position.                                                       
 73  {:cells (conway-state (:cells state))}                                                                             
 74)                                                                                                                    
 75                                                                                                                     
 76(defn getFill [alive]                                                                                                
 77  (if (&gt; alive 0) 255 0)                                                                                             
 78)                                                                                                                    
 79                                                                                                                     
 80(defn draw-cell [c]                                                                                                  
 81  (q/with-fill [(getFill (:alive c))] (q/rect (:x c) (:y c) scaler scaler))                                          
 82)                                                                                                                    
 83                                                                                                                     
 84(defn draw-state [state]                                                                                             
 85  ; Clear the sketch by filling it with light-grey color.                                                            
 86  (q/background 240)                                                                                                 
 87  ; Calculate x and y coordinates of the cell and the color.                                                         
 88  (doseq [cell (:cells state) ]                                                                                      
 89    (draw-cell cell))                                                                                                
 90)            
91                                                                                                                     
 92(q/defsketch hello-quil                                                                                              
 93  :title "Basic Conway Game of Life"                                                                                 
 94  :size [640 360]                                                                                                    
 95  ; setup function called only once, during sketch initialization.                                                   
 96  :setup setup                                                                                                       
 97  ; update-state is called on each iteration before draw-state.                                                      
 98  :update update-state                                                                                               
 99  :draw draw-state                                                                                                   
100  :features [:keep-on-top]                                                                                           
101  ; This sketch uses functional-mode middleware.                                                                     
102  ; Check quil wiki for more info about middlewares and particularly                                                 
103  ; fun-mode.                                                                                                        
104  :middleware [m/fun-mode])                 
</code></pre>
]]></description>
   </item>
   <item>
      <title>Game of Lyfe &amp; Pictures</title>
      <link>https://forum.processing.org/two/discussion/20855/game-of-lyfe-pictures</link>
      <pubDate>Fri, 17 Feb 2017 17:14:50 +0000</pubDate>
      <dc:creator>semiprocoder</dc:creator>
      <guid isPermaLink="false">20855@/two/discussions</guid>
      <description><![CDATA[<p>So I recently stumbled upon game of life, and I can't use an already made program for that! So I decided to make my own version of conway's game of life, but I decided to play around with the settings. I can change the amount of living cells next to a dead one that are necessary to make it come alive, and I can change the "tolerance" of living cells, where they stay alive with a different amount of living cells. Then I added color by taking the amount of living cells adjacent in the previous generation. I also added walls that cannot support life no matter what. This is why I renamed my game to LYFE. I also plan to add a two player mode, but I have not done that yet.</p>

<p>I programmed this is eclipse, so I cannot really port it to open processing or anything(well I could, but I don't know javascript). Anyways, the github link has a zip of the source with some screenshots I was playing around with and it has a zip with a compiled jar of my code(Note: the settings are in, well, the settings folder, and should be mostly self explanatory, but if you have any questions, just ask!).
<a rel="nofollow" href="https://github.com/awesommee333/LYFE">https://github.com/awesommee333/LYFE</a></p>

<p>And here is a screenshot of something I made with this program:
<img src="https://forum.processing.org/two/uploads/imageupload/837/T4G7K1CIKRG6.png" alt="capture-000234SCALED" title="capture-000234SCALED" /></p>
]]></description>
   </item>
   </channel>
</rss>