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))
(if grow body (butlast body)))))
and
(defn paint [graphics [x y] color]
(.setColor graphics color)
(.fillRect graphics x y *grid-size* *grid-size*))
As a bonus, it works with any number of dimensions :-)
Christophe
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---