Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
@@ -5,3 +5,4 @@ game_of_life.js
cljs-out
.lein-deps-sum
lib
+resources
11 Makefile
View
@@ -1,10 +1,13 @@
CLJ_SRC = src/clj
-CLJS_SRC = src/cljs
-DEPS = $(shell find "${CLJS_SRC}" -name "*.cljs")
+CLJS_SRC = src
+CLJ_DEPS = $(shell find "${CLJ_SRC}" -name "*.clj")
+CLJS_DEPS = $(shell find "${CLJS_SRC}" -name "*.cljs")
+DEPS = ${CLJS_DEPS} ${CLJ_DEPS}
MAIN_JS = game_of_life.js
OUTPUT_DIR = cljs-out
+NOTIFY_COMMAND = growlnotify -m compile-done cljs-watch
ADDITIONAL_CLASSPATH = ${PWD}/${CLJ_SRC}
# CLJS_ARGS = :optimizations :simple :pretty-print true
@@ -21,9 +24,9 @@ ${MAIN_JS}: ${DEPS}
.PHONY: watch
watch: ${DEPS}
- @export CLASSPATH=${CLASSPATH}:${ADDITIONAL_CLASSPATH}; cljs-watch -s "${CLJS_SRC}" -b '{${CLJS_ARGS}:output-dir "${OUTPUT_DIR}" :output-to "${MAIN_JS}"}'
+ @export CLASSPATH=${CLASSPATH}:${ADDITIONAL_CLASSPATH}; cljs-watch -s "${CLJS_SRC}" -c '${NOTIFY_COMMAND}' '{${CLJS_ARGS}:output-dir "${OUTPUT_DIR}" :output-to "${MAIN_JS}"}'
-.PHONY:clean
+.PHONY: clean
clean:
rm -f "${MAIN_JS}"
rm -fr "${OUTPUT_DIR}"
12 src/clj/name/benjaminpeter/util.clj
View
@@ -9,7 +9,15 @@
)
)
-(defmacro add_exception_handler [handler function]
- `(try ~function (catch js/Object ex# (~handler ex#)))
+; Only usefull for clojurescript! try catch without class type.
+(defmacro with_exception_handler [handler function]
+ `(try ~function (catch ex# (~handler ex#)))
)
+;(defmacro case [value & statements]
+; (for [[condition action] (partition 2 statements)]
+; ;`(prn ~condition " ac " ~action))) ; test
+; `(if-not action
+; condition
+; (if condition action ~(case value (rest (rest statements 2)))))))
+
31 src/cljs/game_of_life/core.cljs
View
@@ -6,6 +6,7 @@
[game_of_life.world_builder :as builder]
[game_of_life.game :as game]
[game_of_life.view :as view]
+ [game_of_life.world :as world]
[game_of_life.view.outofrange :as outofrange]
[goog.graphics :as graphics])
(:require-macros [name.benjaminpeter.util :as util])
@@ -49,13 +50,15 @@
,xxx"
)
-(defn next_gen [world]
- (str "Calculates and displays the next generation of the world. "
- "After each generation it waits for the user to press enter.")
- (println (printer/to_string world))
- (read-line)
- (recur (game/next_generation world))
-)
+(defn call_after [ms callback]
+ (js/setTimeout callback ms))
+
+(defn next_gen [view world]
+ (.log js/console "paint")
+ (view/clear view)
+ (doseq [living (world/living_cells world)]
+ (view/draw_living view (+ 10 (:x living)) (+ 10 (:y living))))
+ (call_after 2000 #(next_gen view (game/next_generation world))))
; Zwei zeichen Versionen
; - locked view, statischer Ausschnitt
@@ -63,19 +66,13 @@
; - skalieren vorsehen
(defn ^:export init []
-; (util/add_exception_handler view/exception_handler
- (try
- (-> (view/new_world (view/ViewOptions. 100 100 5 5 outofrange/exception) (domh/get-element "graphics"))
- (view/draw_living 0 0)
- (view/draw_living 10 10)
- (view/draw_living 20 20)
- (view/draw_living -30 30)
- )
- (catch js/Object ex (view/exception_handler ex))
+ (let [view (-> (view/new_world (view/ViewOptions. 100 100 5 5 outofrange/ignore) (domh/get-element "graphics")))]
+; (util/with_exception_handler view/exception_handler
+ (next_gen view (builder/from_string oscyl cell/new_cell))
+; )
)
)
(defn -main []
- (next_gen (builder/from_string term54 cell/new_cell))
)
14 src/cljs/game_of_life/game.cljs
View
@@ -1,7 +1,7 @@
(ns game_of_life.game
- (:require [game_of_life.world :as world])
- (:require [game_of_life.cell :as cell])
-)
+ (:require
+ [game_of_life.world :as world]
+ [game_of_life.cell :as cell]))
(defn- next_generation_of_cell [new_world world unchecked_cells]
(str "Calculates the next generation of each cell. It has the "
@@ -13,13 +13,13 @@
"currently living cells and their neighbours.")
(if (seq unchecked_cells)
(let [examined_cell (first unchecked_cells)
- number_of_living_neighbours (number_of (living
+ number_of_living_neighbours (world/number_of (world/living
(world/neighbours_of world examined_cell)))]
(recur
(let [action (cell/next_cell_action examined_cell number_of_living_neighbours)]
- (case action
+ (condp = action
:kill (world/kill new_world examined_cell)
- :revive (world/invigorate new_world (revive examined_cell))
+ :revive (world/invigorate new_world (cell/revive examined_cell))
new_world
))
world
@@ -39,7 +39,7 @@
(into
[(first living_cells)]
(into
- (vals (world/dead (world/all_neighbours_of world (first living_cells) new_cell)))
+ (vals (world/dead (world/all_neighbours_of world (first living_cells) cell/new_cell)))
(cells_to_check_step world (rest living_cells))
)
)
29 src/cljs/game_of_life/view.cljs
View
@@ -110,22 +110,17 @@
))
)
-(defn exception_handler [exception]
- ;(js/alert (str "Exception: " exception))
- (doto (goog.ui.Dialog. nil true)
- (.setContent (str exception))
- (.setTitle "Exception")
- (.setButtonSet (.createOk goog.ui.Dialog.ButtonSet))
- (.setDraggable true)
- (.setModal false)
- (.setVisible true)
- )
- ;goog.events.listen(dialog2, goog.ui.Dialog.EventType.SELECT, function(e) {
- ; alert('You chose: ' + e.key);
- ;});
-
- ;goog.events.listen(window, 'unload', function() {
- ; goog.events.removeAll();
- ;});
+(defn exception_handler
+ "Displays the exception using a google closure ui Dialog"
+ [exception]
+ (doto (goog.ui.Dialog. nil true)
+ (.setContent (str exception))
+ (.setTitle "Exception")
+ (.setButtonSet (.createOk goog.ui.Dialog.ButtonSet))
+ (.setDraggable true)
+ (.setModal false)
+ (.setVisible true)
+ )
+ nil
)
6 src/cljs/game_of_life/world.cljs
View
@@ -58,7 +58,6 @@
(get grid (WorldKey. x y))
)
(all_neighbours_of [this cell create_cell]
- ; bpeter todo macro the zipmap thing?
(zipmap
(neighbour_keywords)
(for [
@@ -66,8 +65,9 @@
:when (not (= 0 xOffset yOffset))
]
(let [cur_x (+ (:x cell) xOffset)
- cur_y (+ (:y cell) yOffset)]
- (if-let [living_cell (retrieve this cur_x cur_y)]
+ cur_y (+ (:y cell) yOffset)
+ living_cell (retrieve this cur_x cur_y)]
+ (if living_cell
living_cell
(create_cell cur_x cur_y false)
)
3  src/cljs/game_of_life/world_builder.cljs
View
@@ -1,5 +1,6 @@
(ns game_of_life.world_builder
(:require [game_of_life.world :as world])
+ (:require-macros [name.benjaminpeter.util :as util])
)
(defprotocol CellBuilder
@@ -24,7 +25,7 @@
"stopping when none are left.")
(if-let [desc (seq world_desc)]
(recur
- (case (first desc)
+ (condp = (first desc)
\X (living builder)
\x (living builder)
\. (dead builder)
Please sign in to comment.
Something went wrong with that request. Please try again.