Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Improved code compatibility from clojure to clojurescript. Put view a…

…nd game together for the first time.
  • Loading branch information...
commit aa7b92e9777557f1ef71586985de57d701aedc99 1 parent 4c016bf
Benjamin Peter authored
1  .gitignore
@@ -5,3 +5,4 @@ game_of_life.js
5 5 cljs-out
6 6 .lein-deps-sum
7 7 lib
  8 +resources
11 Makefile
... ... @@ -1,10 +1,13 @@
1 1
2 2 CLJ_SRC = src/clj
3   -CLJS_SRC = src/cljs
4   -DEPS = $(shell find "${CLJS_SRC}" -name "*.cljs")
  3 +CLJS_SRC = src
  4 +CLJ_DEPS = $(shell find "${CLJ_SRC}" -name "*.clj")
  5 +CLJS_DEPS = $(shell find "${CLJS_SRC}" -name "*.cljs")
  6 +DEPS = ${CLJS_DEPS} ${CLJ_DEPS}
5 7
6 8 MAIN_JS = game_of_life.js
7 9 OUTPUT_DIR = cljs-out
  10 +NOTIFY_COMMAND = growlnotify -m compile-done cljs-watch
8 11 ADDITIONAL_CLASSPATH = ${PWD}/${CLJ_SRC}
9 12
10 13 # CLJS_ARGS = :optimizations :simple :pretty-print true
@@ -21,9 +24,9 @@ ${MAIN_JS}: ${DEPS}
21 24
22 25 .PHONY: watch
23 26 watch: ${DEPS}
24   - @export CLASSPATH=${CLASSPATH}:${ADDITIONAL_CLASSPATH}; cljs-watch -s "${CLJS_SRC}" -b '{${CLJS_ARGS}:output-dir "${OUTPUT_DIR}" :output-to "${MAIN_JS}"}'
  27 + @export CLASSPATH=${CLASSPATH}:${ADDITIONAL_CLASSPATH}; cljs-watch -s "${CLJS_SRC}" -c '${NOTIFY_COMMAND}' '{${CLJS_ARGS}:output-dir "${OUTPUT_DIR}" :output-to "${MAIN_JS}"}'
25 28
26   -.PHONY:clean
  29 +.PHONY: clean
27 30 clean:
28 31 rm -f "${MAIN_JS}"
29 32 rm -fr "${OUTPUT_DIR}"
12 src/clj/name/benjaminpeter/util.clj
@@ -9,7 +9,15 @@
9 9 )
10 10 )
11 11
12   -(defmacro add_exception_handler [handler function]
13   - `(try ~function (catch js/Object ex# (~handler ex#)))
  12 +; Only usefull for clojurescript! try catch without class type.
  13 +(defmacro with_exception_handler [handler function]
  14 + `(try ~function (catch ex# (~handler ex#)))
14 15 )
15 16
  17 +;(defmacro case [value & statements]
  18 +; (for [[condition action] (partition 2 statements)]
  19 +; ;`(prn ~condition " ac " ~action))) ; test
  20 +; `(if-not action
  21 +; condition
  22 +; (if condition action ~(case value (rest (rest statements 2)))))))
  23 +
31 src/cljs/game_of_life/core.cljs
@@ -6,6 +6,7 @@
6 6 [game_of_life.world_builder :as builder]
7 7 [game_of_life.game :as game]
8 8 [game_of_life.view :as view]
  9 + [game_of_life.world :as world]
9 10 [game_of_life.view.outofrange :as outofrange]
10 11 [goog.graphics :as graphics])
11 12 (:require-macros [name.benjaminpeter.util :as util])
@@ -49,13 +50,15 @@
49 50 ,xxx"
50 51 )
51 52
52   -(defn next_gen [world]
53   - (str "Calculates and displays the next generation of the world. "
54   - "After each generation it waits for the user to press enter.")
55   - (println (printer/to_string world))
56   - (read-line)
57   - (recur (game/next_generation world))
58   -)
  53 +(defn call_after [ms callback]
  54 + (js/setTimeout callback ms))
  55 +
  56 +(defn next_gen [view world]
  57 + (.log js/console "paint")
  58 + (view/clear view)
  59 + (doseq [living (world/living_cells world)]
  60 + (view/draw_living view (+ 10 (:x living)) (+ 10 (:y living))))
  61 + (call_after 2000 #(next_gen view (game/next_generation world))))
59 62
60 63 ; Zwei zeichen Versionen
61 64 ; - locked view, statischer Ausschnitt
@@ -63,19 +66,13 @@
63 66 ; - skalieren vorsehen
64 67
65 68 (defn ^:export init []
66   -; (util/add_exception_handler view/exception_handler
67   - (try
68   - (-> (view/new_world (view/ViewOptions. 100 100 5 5 outofrange/exception) (domh/get-element "graphics"))
69   - (view/draw_living 0 0)
70   - (view/draw_living 10 10)
71   - (view/draw_living 20 20)
72   - (view/draw_living -30 30)
73   - )
74   - (catch js/Object ex (view/exception_handler ex))
  69 + (let [view (-> (view/new_world (view/ViewOptions. 100 100 5 5 outofrange/ignore) (domh/get-element "graphics")))]
  70 +; (util/with_exception_handler view/exception_handler
  71 + (next_gen view (builder/from_string oscyl cell/new_cell))
  72 +; )
75 73 )
76 74 )
77 75
78 76 (defn -main []
79   - (next_gen (builder/from_string term54 cell/new_cell))
80 77 )
81 78
14 src/cljs/game_of_life/game.cljs
... ... @@ -1,7 +1,7 @@
1 1 (ns game_of_life.game
2   - (:require [game_of_life.world :as world])
3   - (:require [game_of_life.cell :as cell])
4   -)
  2 + (:require
  3 + [game_of_life.world :as world]
  4 + [game_of_life.cell :as cell]))
5 5
6 6 (defn- next_generation_of_cell [new_world world unchecked_cells]
7 7 (str "Calculates the next generation of each cell. It has the "
@@ -13,13 +13,13 @@
13 13 "currently living cells and their neighbours.")
14 14 (if (seq unchecked_cells)
15 15 (let [examined_cell (first unchecked_cells)
16   - number_of_living_neighbours (number_of (living
  16 + number_of_living_neighbours (world/number_of (world/living
17 17 (world/neighbours_of world examined_cell)))]
18 18 (recur
19 19 (let [action (cell/next_cell_action examined_cell number_of_living_neighbours)]
20   - (case action
  20 + (condp = action
21 21 :kill (world/kill new_world examined_cell)
22   - :revive (world/invigorate new_world (revive examined_cell))
  22 + :revive (world/invigorate new_world (cell/revive examined_cell))
23 23 new_world
24 24 ))
25 25 world
@@ -39,7 +39,7 @@
39 39 (into
40 40 [(first living_cells)]
41 41 (into
42   - (vals (world/dead (world/all_neighbours_of world (first living_cells) new_cell)))
  42 + (vals (world/dead (world/all_neighbours_of world (first living_cells) cell/new_cell)))
43 43 (cells_to_check_step world (rest living_cells))
44 44 )
45 45 )
29 src/cljs/game_of_life/view.cljs
@@ -110,22 +110,17 @@
110 110 ))
111 111 )
112 112
113   -(defn exception_handler [exception]
114   - ;(js/alert (str "Exception: " exception))
115   - (doto (goog.ui.Dialog. nil true)
116   - (.setContent (str exception))
117   - (.setTitle "Exception")
118   - (.setButtonSet (.createOk goog.ui.Dialog.ButtonSet))
119   - (.setDraggable true)
120   - (.setModal false)
121   - (.setVisible true)
122   - )
123   - ;goog.events.listen(dialog2, goog.ui.Dialog.EventType.SELECT, function(e) {
124   - ; alert('You chose: ' + e.key);
125   - ;});
126   -
127   - ;goog.events.listen(window, 'unload', function() {
128   - ; goog.events.removeAll();
129   - ;});
  113 +(defn exception_handler
  114 + "Displays the exception using a google closure ui Dialog"
  115 + [exception]
  116 + (doto (goog.ui.Dialog. nil true)
  117 + (.setContent (str exception))
  118 + (.setTitle "Exception")
  119 + (.setButtonSet (.createOk goog.ui.Dialog.ButtonSet))
  120 + (.setDraggable true)
  121 + (.setModal false)
  122 + (.setVisible true)
  123 + )
  124 + nil
130 125 )
131 126
6 src/cljs/game_of_life/world.cljs
@@ -58,7 +58,6 @@
58 58 (get grid (WorldKey. x y))
59 59 )
60 60 (all_neighbours_of [this cell create_cell]
61   - ; bpeter todo macro the zipmap thing?
62 61 (zipmap
63 62 (neighbour_keywords)
64 63 (for [
@@ -66,8 +65,9 @@
66 65 :when (not (= 0 xOffset yOffset))
67 66 ]
68 67 (let [cur_x (+ (:x cell) xOffset)
69   - cur_y (+ (:y cell) yOffset)]
70   - (if-let [living_cell (retrieve this cur_x cur_y)]
  68 + cur_y (+ (:y cell) yOffset)
  69 + living_cell (retrieve this cur_x cur_y)]
  70 + (if living_cell
71 71 living_cell
72 72 (create_cell cur_x cur_y false)
73 73 )
3  src/cljs/game_of_life/world_builder.cljs
... ... @@ -1,5 +1,6 @@
1 1 (ns game_of_life.world_builder
2 2 (:require [game_of_life.world :as world])
  3 + (:require-macros [name.benjaminpeter.util :as util])
3 4 )
4 5
5 6 (defprotocol CellBuilder
@@ -24,7 +25,7 @@
24 25 "stopping when none are left.")
25 26 (if-let [desc (seq world_desc)]
26 27 (recur
27   - (case (first desc)
  28 + (condp = (first desc)
28 29 \X (living builder)
29 30 \x (living builder)
30 31 \. (dead builder)

0 comments on commit aa7b92e

Please sign in to comment.
Something went wrong with that request. Please try again.