Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(ns problems.life
(:require [clojure.string]));;should use "only" here! (use '[clojure.data.json :only (read-json json-str)])
; GOL is a function, which takes
; a function as input - the function takes an index(x,y) and returns a value
; for example, 1 or 0.
(defn play1 [f]
(print (f '(0 0))))
;this is where the problem is :(
(defn play [f]
(let
;live is a function which returns true only if a cell has 1 neighbor.
[live #(= 1
(+ (f [%1 (dec %2)])
(f [%1 (inc %2)])
(f [(inc %1) %2])
(f [(dec %1) %2])
(f [(dec %1) (dec %2)])
(f [(inc %1) (inc %2)])
(f [(dec %1) (inc %2)])
(f [(inc %1) (dec %2)])
))]
(for [x [0 1] y [0 1]]
(println (live x y)))))
(defn board [cell]
(println cell)
(let [ret (cond
(= cell [0 0]) 1
(= cell [0 1]) 0
(= cell [1 0]) 0
(= cell [1 1]) 1
:else 0)]
ret))
;We create an initial function which defines the game of life
;landscape. Note that this function's syntax is roughly equivalent
;to that of a 2D array in how we define it, but it is flexible: we can
;easily swap out a cond statement to, for example, make a database call.
;better yet: the board can be infintely large.
(defn main1 []
(print "\n------------\n")
(play board)
;Anonymous function which takes a 2-tuple and returns corresponding
;live/dead status for a given cell.
(print "\n------------\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.