Pong!

52 views
Skip to first unread message

jng27

unread,
Sep 12, 2009, 7:43:44 PM9/12/09
to Clojure

Sean Devlin

unread,
Sep 12, 2009, 9:21:25 PM9/12/09
to Clojure

Jarkko Oranen

unread,
Sep 13, 2009, 8:04:48 AM9/13/09
to Clojure
Neat. I have some suggestions though.

1) I think you use too many refs. the sx, sy, etc. could all be
replaced with a simple
(defstruct position :x :y)
(def whatever (ref (struct position nil nil))
It's less granular, but much easier

Also, do the *w and *h refs don't seem to change after the game
starts, so refs are overkill for them. You could at least make them
atoms, though if possible using just regular defs and (binding [...])
or maybe even alter-var-root! would be cleaner (no need to explicitly
deref vars).

2) write specialised functions for updating the positions, and use
alter instead of ref-set. eg. instead of
(ref-set bx (+ @bx @sx)) (ref-set by (+ @by @sy)) you could have
something like
(alter (partial merge-with +) b-pos @s-pos)

3) Split up the actionPerformed for the pong-frame. it's way too big.
The best way to split it would be to try to move as much of the actual
logic into pure functions and just altering your refs using those
functions. Separating the dosync logic (dealing with refs) from the
actual game logic (calculating the new values from the old ones) makes
the code much more understandable.

4) Prefer (.foo bar) and (Foo/bar) to (. foo bar) (this is my
personal recommendation)

--
Jarkko

Chris

unread,
Sep 13, 2009, 10:51:25 AM9/13/09
to Clojure
Got some attention here on Hacker News:

http://news.ycombinator.com/item?id=819630
Reply all
Reply to author
Forward
0 new messages