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))) --Brian Will On Jan 2, 11:07 am, "Mark Volkmann" <r.mark.volkm...@gmail.com> wrote: > I've written a new version of the snake program that uses a more > literate style and therefore, to my eyes, calls for far fewer > comments. I think this code is very readable. Check it out > athttp://www.ociweb.com/mark/programming/ClojureLiterateSnake.html. > Feedback is welcomed! > > The most controversial thing about this code is probably my use of def > to change the state of the snake and the apple. It's not yet clear to > me that using atoms is needed here, but I need to think about that > more. > > This version has some features that weren't in the original such as: > - automatically turning the snake in the clockwise direction when aboardedge > is reached > - changing the color of the snake to black when it overlaps itself > - announcing a win when the length of the snake reaches 10 > - automatically restarting the game after an overlap or a win > > -- > 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 -~----------~----~----~----~------~----~------~--~---