quil / quil-site

Source code of quil.info
http://quil.info
Eclipse Public License 1.0
7 stars 13 forks source link

Conway Game of Life #18

Closed mdgart closed 6 years ago

mdgart commented 6 years ago
(ns gameoflife.core
  (:require [quil.core :as q :include-macros true]
            [quil.middleware :as m]))

(def grid_size 100)  ;; change me
(def cell_size 3)  ;; change me

(def state {:matrix (vec 
                     (repeatedly (* grid_size grid_size) #(rand-int 2)))})

(defn setup []
  (q/frame-rate 10)
  (q/color-mode :hsb)
  (q/no-stroke)
  state)

(defn get-neighbors [idx vec]
  [
   (get vec (dec (- idx grid_size)))  
   (get vec (- idx grid_size))  
   (get vec (inc (- idx grid_size)))    

   (get vec (dec idx)) 
   (get vec (inc idx)) 

   (get vec (dec (+ grid_size idx)))  
   (get vec (+ grid_size idx))  
   (get vec (inc (+ grid_size idx))) 
  ])

(defn new-status [idx itm vec]
  (let [alive-n (get (frequencies (get-neighbors idx vec)) 1 0)]
    (if (= 1 itm)
      (if (or (> alive-n 3) (< alive-n 2)) 0 1)
      (if (= 3 alive-n) 1 0)
    )))

(defn update-state [state]
  (assoc state :matrix 
    (vec 
      (map-indexed 
       (fn [idx itm] (new-status idx itm (:matrix state))) 
       (:matrix state)))))

(defn draw-state [state]
  (q/background 240)

  (doseq [[i v] (map-indexed vector (:matrix state))] 
    (let [multiplier (int (/ i grid_size))
          x (* cell_size (- i (* multiplier grid_size)))
          y (* cell_size multiplier)]
      (q/fill 
       (if (= 1 v) 0 255))
      (q/rect x y cell_size cell_size))))

(q/defsketch gameoflife
  :host "host"
  :size [500 500]
  :setup setup
  :update update-state
  :draw draw-state
  :middleware [m/fun-mode])
nbeloglazov commented 6 years ago

Hi Mauro. Thanks for the sketch! I've run it locally and I see that simulation comes to a stable state pretty quickly. Maybe you could randomize initial state? It would be really cool to sit watch long simulation on quil.info.

nbeloglazov commented 6 years ago

Btw I have code ready to submit and upload to the site.

mdgart commented 6 years ago

Sure that it's an easy change, I'll keep the part with the matrix commented, just in case someone want to try particular initial configuration.

On Sep 29, 2017 11:57 PM, "Nikita Beloglazov" notifications@github.com wrote:

Hi Mauro. Thanks for the sketch! I've run it locally and I see that simulation comes to a stable state pretty quickly. Maybe you could randomize initial state? It would be really cool to sit watch long simulation on quil.info.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/quil/quil-site/issues/18#issuecomment-333286387, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGvCSJgyP6khKahz7q2Z5bPq1l3I_j0ks5sndhmgaJpZM4PnvmZ .

mdgart commented 6 years ago

Ok, it's random now, I also increased the size of the grid to 100*100

nbeloglazov commented 6 years ago

Thanks, Mauro! I added your sketch and it's live now: http://quil.info/?example=game%20of%20life ! I did a few changes though:

  1. Kept grid size to 20. I thought 100x100 is too big for a browser version and on quil.info size of sketch in pixels is small (200x200) so grid size 100x100 would mean very small size of each cell.

  2. Removed cell-size. It is automatically calculated from sketch width so that it looks good in small sketches on main page and if you open it in a separate window: http://quil.info/sketches/show/example_game-of-life

Feel free to add modifications. Just update this file and submit pull request.

Thanks again for the sketch!