Skip to content

Commit 18f6900

Browse files
committed
tweaks to make game runnable via lein
1 parent 9414a03 commit 18f6900

3 files changed

Lines changed: 34 additions & 15 deletions

File tree

ch04-concurrency/project.clj

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
(defproject concurrency "1.0.0-SNAPSHOT"
2-
:description "FIXME: write description"
3-
:dependencies [[org.clojure/clojure "1.3.0"]
4-
[enlive "1.0.0"]])
2+
:description "An array-backed set implementation, analogous to
3+
Clojure's array-map, explored in chapter 3 of 'Clojure Programming' by
4+
Emerick, Carper, and Grand."
5+
:url "http://github.com/clojurebook/ClojureProgramming"
6+
:dependencies [[org.clojure/clojure "1.3.0"]]
7+
:profiles {:1.4 {:dependencies [[org.clojure/clojure "1.4.0-beta6"]]}}
8+
:run-aliases {:loot com.clojurebook.concurrency.game/-loot-demo
9+
:battle com.clojurebook.concurrency.game/-battle-demo
10+
:logged-battle com.clojurebook.concurrency.game-validators/-main})

ch04-concurrency/src/com/clojurebook/concurrency/game.clj

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,40 @@
6767
(while (and (alive? @character)
6868
(alive? @other)
6969
(action character other))
70-
(Thread/sleep (rand-int 50))))
70+
(Thread/sleep (rand-int 100))))
7171

72-
(comment
73-
;epic battle!
72+
(defn -battle-demo []
7473
(def smaug (character "Smaug" :health 500 :strength 400))
7574
(def bilbo (character "Bilbo" :health 100 :strength 100))
7675
(def gandalf (character "Gandalf" :health 75 :mana 1000))
77-
76+
7877
(wait-futures 1
7978
(play bilbo attack smaug)
8079
(play smaug attack bilbo)
8180
(play gandalf heal bilbo))
8281

83-
(pprint (map (comp #(select-keys % [:name :health :mana]) deref) [smaug bilbo gandalf])))
82+
(clojure.pprint/pprint
83+
(map
84+
(comp #(select-keys % [:name :health :mana]) deref)
85+
[smaug bilbo gandalf]))
86+
87+
(shutdown-agents))
8488

8589

86-
(comment
90+
(defn -loot-demo []
8791
(def smaug (character "Smaug" :health 500 :strength 400 :items (set (range 50))))
8892
(def bilbo (character "Bilbo" :health 100 :strength 100))
8993
(def gandalf (character "Gandalf" :health 75 :mana 1000))
9094
(wait-futures 1
9195
(while (loot smaug bilbo))
9296
(while (loot smaug gandalf)))
93-
94-
(println (map (comp count :items deref) [bilbo gandalf]))
95-
(println (filter (:items @bilbo) (:items @gandalf))))
97+
98+
(println "Bilbo's and Gandalf's item counts (should always == 50):"
99+
(map (comp count :items deref) [bilbo gandalf]))
100+
(println "Overlap in Bilbo's and Gandalf's items (should always be empty):"
101+
(filter (:items @bilbo) (:items @gandalf)))
102+
103+
(shutdown-agents))
96104

97105

98106

ch04-concurrency/src/com/clojurebook/concurrency/game_validators.clj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,25 @@
7979
(action character other))
8080
(Thread/sleep (rand-int 100))))
8181

82-
(comment
82+
(defn -main []
8383
(def smaug (character "Smaug" :health 500 :strength 400))
8484
(def bilbo (character "Bilbo" :health 100 :strength 100))
8585
(def gandalf (character "Gandalf" :health 75 :mana 1000))
8686

8787
(log-reference bilbo console character-log)
8888
(log-reference smaug console character-log)
89-
89+
9090
(wait-futures 1
9191
(play bilbo attack smaug)
9292
(play smaug attack bilbo)
9393
(play gandalf heal bilbo))
9494

95-
(clojure.pprint/pprint (map (comp #(select-keys % [:name :health :mana]) deref) [smaug bilbo gandalf])))
95+
(clojure.pprint/pprint
96+
(map
97+
(comp #(select-keys % [:name :health :mana]) deref)
98+
[smaug bilbo gandalf]))
99+
100+
(shutdown-agents))
96101

97102

98103

0 commit comments

Comments
 (0)