On Tue, Dec 30, 2008 at 4:21 AM, Christophe Grand <christo...@cgrand.net> wrote:
>
> Mark Volkmann a écrit :
>> For example, here's some code that I don't
>> know how to rewrite in a way that I find self-explanatory:
>>
>> (every?
>>     #(<= (- (apple %) +grid-size+) (head %) (+ (apple %) +grid-size+))
>>     [+x-index+ +y-index+]))
>>
>> And here's another one:
>>
>> (assoc snake :body
>>     (cons
>>       (vec (map #(+ (dir %) ((first body) %)) [+x-index+ +y-index+]))
>>       (if grow body (butlast body)))))
>>
>> Perhaps using your suggestion to go back and use a map with :x and :y
>> keys instead of a two-element vector to represent x/y coordinates
>> would help a little, but I'm still not sure the code would be
>> immediately obvious.
>>
> I think it would be clearer if we get rid of indexing:
>
> (every? #(<= (- *grid-size*) % *grid-size*)
>  (map - apple head)))
>
> and
>
> (assoc snake :body
>    (cons
>      (map + dir (first body))

The previous line needs to be (vec (map + dir (first body))).

>      (if grow body (butlast body)))))
>
> and
>
> (defn paint [graphics [x y] color]
>  (.setColor graphics color)
>  (.fillRect graphics x y *grid-size* *grid-size*))

Excellent improvements! Thank you very much!
I've updated http://www.ociweb.com/mark/programming/ClojureSnake.html
to include these changes.

> As a bonus, it works with any number of dimensions :-)

Except for the fact that Java's Graphics object doesn't know how to
paint in 3D. ;-)

-- 
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to