Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conway Game of Life #18

Closed
mdgart opened this issue Sep 28, 2017 · 5 comments
Closed

Conway Game of Life #18

mdgart opened this issue Sep 28, 2017 · 5 comments

Comments

@mdgart
Copy link

mdgart commented Sep 28, 2017

(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
Copy link
Member

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
Copy link
Member

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

@mdgart
Copy link
Author

mdgart commented Sep 30, 2017 via email

@mdgart
Copy link
Author

mdgart commented Sep 30, 2017

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

@nbeloglazov
Copy link
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants