I tidied up a couple of things: Changed function name 'verify-direction' to 'new-direction' (it is more than a simple verification) Passed 'direction' and 'snake-head' to 'new-direction' to avoid accessing global state from inside the function Used destructuring to simplify let statement in new-head (just to see what it looked like) Extracted dy,dx calculation in 'move-snake' into a 'delta' function
I notice everything seems to happen in the assignment vector in the 'let' in move-snake? Is that good style? I'm not sure. What would be interesting would be to have a second randomly moving snake to avoid, that would force out some issues with global state etc. The new code at the end: * ; Only run the application automatically if run as a script, ; not if loaded in a REPL with load-file. (println "*command-line-args* =" *command-line-args*) (if *command-line-args* (main))* Doesn't do what it says for me, it printlns and stops from clj, any idea what I am doing wrong? Cheers Tom 2009/1/3 Brian Will <brian.thomas.w...@gmail.com> > > My way was a little verbose, but you could do: > > (defn verify-direction > "Gets the current snake direction or > a new one if a board edge was reached." > [] > (let [direction (@snake :direction) > head (snake-head) > x (head :x) > y (head :y) > at-left (= x 0) > at-right (= x (- *board-width* 1)) > at-top (= y 0) > at-bottom (= y (- *board-height* 1)) > left-half (< x (/ *board-width* 2)) > top-half (< y (/ *board-height* 2))] > (cond > (and (= direction :right) at-right) (if top-half :down :up) > (and (= direction :down) at-bottom) (if left-half :right :left) > (and (= direction :left) at-left) (if top-half :down :up) > (and (= direction :up) at-top) (if left-half :right :left) > true direction))) > > On Jan 3, 9:28 am, "Mark Volkmann" <r.mark.volkm...@gmail.com> wrote: > > On Sat, Jan 3, 2009 at 3:03 AM, Brian Will <brian.thomas.w...@gmail.com> > wrote: > > > > > The problem with the snake running off the edge simply is a flaw in > > > your logic here: > > > > > (defn verify-direction > > > "Gets the current snake direction or > > > a new one if a board edge was reached." > > > [] > > > (let [direction (@snake :direction) > > > head (snake-head) > > > x (head :x) > > > y (head :y)] > > > (cond > > > (and (= direction :right) (= x (- *board-width* 1))) :down > > > (and (= direction :down) (= y (- *board-height* 1))) :left > > > (and (= direction :left) (= x 0)) :up > > > (and (= direction :up) (= y 0)) :right > > > true direction))) > > > > > Each case of the cond here is flawed. For example, when the snake is > > > heading right and already on the bottom, he's going to turn down, > > > which is off the board. > > > > > I suggest splitting each direction case into two cases such that the > > > snake goes in the direction where there's more room to travel: > > > > > (defn verify-direction > > > "Gets the current snake direction or > > > a new one if a board edge was reached." > > > [] > > > (let [direction (@snake :direction) > > > head (snake-head) > > > x (head :x) > > > y (head :y) > > > left (= x 0) > > > right (= x (- *board-width* 1)) > > > top (= y 0) > > > bottom (= y (- *board-height* 1)) > > > left-half (< x (/ *board-width* 2)) > > > right-half (>= x (/ *board-width* 2)) > > > top-half (< y (/ *board-height* 2)) > > > bottom-half (>= y (/ *board-height* 2))] > > > (cond > > > (and (= direction :right) right top-half) :down > > > (and (= direction :right) right bottom-half) :up > > > (and (= direction :down) bottom left-half) :right > > > (and (= direction :down) bottom right-half) :left > > > (and (= direction :left) left top-half) :down > > > (and (= direction :left) left bottom-half) :up > > > (and (= direction :up) top left-half) :right > > > (and (= direction :up) top right-half) :left > > > true direction))) > > > > Thanks Brian! I ended up doing something similar that required a > > little less code. Here's what I did. > > > > (let [direction (@snake :direction) > > head (snake-head) > > x (head :x) > > y (head :y) > > at-left (= x 0) > > at-right (= x (- *board-width* 1)) > > at-top (= y 0) > > at-bottom (= y (- *board-height* 1))] > > (cond > > (and (= direction :up) at-top) (if at-right :left :right) > > (and (= direction :right) at-right) (if at-bottom :up :down) > > (and (= direction :down) at-bottom) (if at-left :right :left) > > (and (= direction :left) at-left) (if at-top :down :up) > > true direction))) > > > > -- > > R. Mark Volkmann > > Object Computing, Inc. > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---
snake.clj
Description: Binary data