Skip to content

Commit 40a94a9

Browse files
REPL launch scripts
more snakiness
1 parent eb2a659 commit 40a94a9

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

bin/repl.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -cp .;lib/jline-0.9.94.jar;lib/clojure.jar;lib/clojure-contrib.jar;lib/compojure.jar;lib/hsqldb.jar;lib/jetty-6.1.14.jar;lib/jetty-util-6.1.14.jar;lib/servlet-api-2.5-6.1.14.jar jline.ConsoleRunner clojure.lang.Repl

bin/repl.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -cp .:lib/jline-0.9.94.jar:lib/clojure.jar:lib/clojure-contrib.jar:lib/compojure.jar:lib/hsqldb.jar:lib/jetty-6.1.14.jar:lib/jetty-util-6.1.14.jar:lib/servlet-api-2.5-6.1.14.jar jline.ConsoleRunner clojure.lang.Repl

examples/server/step_3.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
(html
88
[:head
99
[:title title]
10-
(include-js "/public/javascripts/code-highlighter.js" "/public/javascripts/clojure.js")
10+
(include-js "/public/javascripts/code-highlighter.js"
11+
"/public/javascripts/clojure.js")
1112
(include-css "/public/stylesheets/code-highlighter.css")]
1213
[:body
1314
[:h2 title]
@@ -21,7 +22,7 @@
2122
(text-area {:rows 20 :cols 73} "body")
2223
[:br]
2324
(submit-button "Save"))))
24-
; END: snippet
25+
; END: new-snippet
2526

2627
(defn create-snippet [body]
2728
(if-let [id (insert-snippet body)]

examples/snake.clj

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
:dir [1 0]
5050
:type :snake
5151
:color (Color. 15 160 70)})
52-
; START: snake
52+
; END: snake
5353

5454
; START: move
5555
(defn move [{:keys [body dir] :as snake} & grow]
@@ -62,89 +62,107 @@
6262
(if newdir (assoc snake :dir newdir) snake))
6363
; END: turn
6464

65-
; START: win
65+
; START: win?
6666
(defn win? [{body :body}]
6767
(>= (count body) win-length))
68-
; END: win
68+
; END: win?
6969

70-
; START: lose
70+
; START: lose?
7171
(defn head-overlaps-body? [{[head & body] :body}]
7272
; have proposed to SS that argument order be reversed:
7373
(includes? head body))
7474

7575
(def lose? head-overlaps-body?)
76-
; END: lose
76+
; END: lose?
7777

78-
; START: eats
78+
; START: eats?
7979
(defn eats? [{[snake-head] :body} {apple :location}]
8080
(= snake-head apple))
81-
; END: eats
81+
; END: eats?
8282

8383
; ----------------------------------------------------------
8484
; mutable model
8585
; ----------------------------------------------------------
86+
; START: update-positions
8687
(defn update-positions [snake apple]
8788
(dosync
8889
(if (eats? @snake @apple)
8990
(do (ref-set apple (create-apple))
9091
(alter snake move :grow))
91-
(alter snake move))))
92+
(alter snake move)))
93+
nil)
94+
; END: update-positions
9295

96+
; START: update-direction
9397
(defn update-direction [snake newdir]
9498
(dosync (alter snake turn newdir)))
99+
; END: update-direction
95100

101+
; START: reset-game
96102
(defn reset-game [snake apple]
97103
(dosync (ref-set apple (create-apple))
98-
(ref-set snake (create-snake))))
104+
(ref-set snake (create-snake)))
105+
nil)
106+
; END: reset-game
99107

100108
; ----------------------------------------------------------
101109
; gui
102110
; ----------------------------------------------------------
111+
; START: fill-point
103112
(defn fill-point [g pt color]
104113
(let [[x y width height] (point-to-screen-rect pt)]
105114
(.setColor g color)
106115
(.fillRect g x y width height)))
116+
; END: fill-point
107117

118+
; START: paint
108119
(defmulti paint (fn [g object & _] (:type object)))
109120

110-
(defmethod paint :snake [g {:keys [body color]}]
111-
(doseq [point body]
112-
(fill-point g point color)))
113-
114-
(defmethod paint :apple [g {:keys [location color]}]
121+
(defmethod paint :apple [g {:keys [location color]}] ; <label id="code.paint.apple"/>
115122
(fill-point g location color))
116123

124+
(defmethod paint :snake [g {:keys [body color]}] ; <label id="code.paint.snake"/>
125+
(doseq [point body]
126+
(fill-point g point color)))
127+
; END: paint
128+
129+
; START: game-panel
130+
(defn game-panel [frame snake apple]
131+
(proxy [JPanel ActionListener KeyListener] []
132+
(paintComponent [g] ; <label id="code.game-panel.paintComponent"/>
133+
(proxy-super paintComponent g)
134+
(paint g @snake)
135+
(paint g @apple))
136+
(actionPerformed [e] ; <label id="code.game-panel.actionPerformed"/>
137+
(update-positions snake apple)
138+
(when (lose? @snake)
139+
(reset-game snake apple)
140+
(JOptionPane/showMessageDialog frame "You lose!"))
141+
(when (win? @snake)
142+
(reset-game snake apple)
143+
(JOptionPane/showMessageDialog frame "You win!"))
144+
(.repaint this))
145+
(keyPressed [e] ; <label id="code.game-panel.keyPressed"/>
146+
(update-direction snake (dirs (.getKeyCode e))))
147+
(keyReleased [e])
148+
(keyTyped [e])))
149+
; END: game-panel
150+
151+
; START: game
117152
(defn game []
118-
(let [snake (ref (create-snake))
153+
(let [snake (ref (create-snake)) ; <label id="code.game.let"/>
119154
apple (ref (create-apple))
120155
frame (JFrame. "Snake")
121-
panel (proxy [JPanel ActionListener KeyListener] []
122-
(paintComponent [g]
123-
(proxy-super paintComponent g)
124-
(paint g @snake)
125-
(paint g @apple))
126-
(actionPerformed [e]
127-
(update-positions snake apple)
128-
(when (lose? @snake)
129-
(reset-game snake apple)
130-
(JOptionPane/showMessageDialog frame "You lose!"))
131-
(when (win? @snake)
132-
(reset-game snake apple)
133-
(JOptionPane/showMessageDialog frame "You win!"))
134-
(.repaint this))
135-
(keyPressed [e]
136-
(update-direction snake (dirs (.getKeyCode e))))
137-
(keyReleased [e])
138-
(keyTyped [e]))
156+
panel (game-panel frame snake apple)
139157
timer (Timer. turn-millis panel)]
140-
(doto panel
158+
(doto panel ; <label id="code.game.panel"/>
141159
(.setFocusable true)
142160
(.addKeyListener panel))
143-
(doto frame
161+
(doto frame ; <label id="code.game.frame"/>
144162
(.add panel)
145163
(.setSize (* width point-size) (* height point-size))
146164
(.setVisible true))
147-
(.start timer)
148-
[snake, apple, timer]))
149-
165+
(.start timer) ; <label id="code.game.timer"/>
166+
[snake, apple, timer])) ; <label id="code.game.return"/>
167+
; END: game
150168

0 commit comments

Comments
 (0)