|
27 | 27 | <article> |
28 | 28 | <h2>Clojure mode</h2> |
29 | 29 | <form><textarea id="code" name="code"> |
30 | | -; Conway's Game of Life, based on the work of: |
31 | | -;; Laurent Petit https://gist.github.com/1200343 |
32 | | -;; Christophe Grand http://clj-me.cgrand.net/2011/08/19/conways-game-of-life |
| 30 | +(ns game-of-life |
| 31 | + "Conway's Game of Life, based on the work of |
| 32 | + Christophe Grand (http://clj-me.cgrand.net/2011/08/19/conways-game-of-life) |
| 33 | + and Laurent Petit (https://gist.github.com/1200343).") |
33 | 34 |
|
34 | | -(ns ^{:doc "Conway's Game of Life."} |
35 | | - game-of-life) |
| 35 | +;;; Core game of life's algorithm functions |
36 | 36 |
|
37 | | -;; Core game of life's algorithm functions |
38 | | - |
39 | | -(defn neighbours |
40 | | - "Given a cell's coordinates, returns the coordinates of its neighbours." |
| 37 | +(defn neighbors |
| 38 | + "Given a cell's coordinates `[x y]`, returns the coordinates of its |
| 39 | + neighbors." |
41 | 40 | [[x y]] |
42 | | - (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])] |
| 41 | + (for [dx [-1 0 1] |
| 42 | + dy (if (zero? dx) |
| 43 | + [-1 1] |
| 44 | + [-1 0 1])] |
43 | 45 | [(+ dx x) (+ dy y)])) |
44 | 46 |
|
45 | 47 | (defn step |
46 | | - "Given a set of living cells, computes the new set of living cells." |
| 48 | + "Given a set of living `cells`, computes the new set of living cells." |
47 | 49 | [cells] |
48 | | - (set (for [[cell n] (frequencies (mapcat neighbours cells)) |
49 | | - :when (or (= n 3) (and (= n 2) (cells cell)))] |
| 50 | + (set (for [[cell n] (frequencies (mapcat neighbors cells)) |
| 51 | + :when (or (= n 3) |
| 52 | + (and (= n 2) |
| 53 | + (cells cell)))] |
50 | 54 | cell))) |
51 | 55 |
|
52 | | -;; Utility methods for displaying game on a text terminal |
53 | | - |
54 | | -(defn print-board |
55 | | - "Prints a board on *out*, representing a step in the game." |
56 | | - [board w h] |
57 | | - (doseq [x (range (inc w)) y (range (inc h))] |
58 | | - (if (= y 0) (print "\n")) |
59 | | - (print (if (board [x y]) "[X]" " . ")))) |
60 | | - |
61 | | -(defn display-grids |
62 | | - "Prints a squence of boards on *out*, representing several steps." |
| 56 | +;;; Utility methods for displaying game on a text terminal |
| 57 | + |
| 58 | +(defn print-grid |
| 59 | + "Prints a `grid` of `w` columns and `h` rows, on *out*, representing a |
| 60 | + step in the game." |
| 61 | + [grid w h] |
| 62 | + (doseq [x (range (inc w)) |
| 63 | + y (range (inc h))] |
| 64 | + (when (= y 0) (println)) |
| 65 | + (print (if (grid [x y]) |
| 66 | + "[X]" |
| 67 | + " . ")))) |
| 68 | + |
| 69 | +(defn print-grids |
| 70 | + "Prints a sequence of `grids` of `w` columns and `h` rows on *out*, |
| 71 | + representing several steps." |
63 | 72 | [grids w h] |
64 | | - (doseq [board grids] |
65 | | - (print-board board w h) |
66 | | - (print "\n"))) |
67 | | - |
68 | | -;; Launches an example board |
69 | | - |
70 | | -(def |
71 | | - ^{:doc "board represents the initial set of living cells"} |
72 | | - board #{[2 1] [2 2] [2 3]}) |
73 | | - |
74 | | -(display-grids (take 3 (iterate step board)) 5 5) |
| 73 | + (doseq [grid grids] |
| 74 | + (print-grid grid w h) |
| 75 | + (println))) |
75 | 76 |
|
76 | | -;; Let's play with characters |
77 | | -(println \1 \a \# \\ |
78 | | - \" \( \newline |
79 | | - \} \" \space |
80 | | - \tab \return \backspace |
81 | | - \u1000 \uAaAa \u9F9F) |
| 77 | +;;; Launches an example grid |
82 | 78 |
|
83 | | -;; Let's play with numbers |
84 | | -(+ 1 -1 1/2 -1/2 -0.5 0.5) |
| 79 | +(def grid |
| 80 | + "`grid` represents the initial set of living cells" |
| 81 | + #{[2 1] [2 2] [2 3]}) |
85 | 82 |
|
86 | | -</textarea></form> |
| 83 | +(print-grids (take 3 (iterate step grid)) 5 5)</textarea></form> |
87 | 84 | <script> |
88 | 85 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), { |
89 | 86 | autoCloseBrackets: true, |
|
0 commit comments