From 2a1c03e3d2a2b5765761d0ac63f3b074fbf5147f Mon Sep 17 00:00:00 2001 From: Brad Svercl Date: Wed, 29 May 2019 20:14:46 -0500 Subject: [PATCH] Better HIT-ITSELF implementation --- snake.lisp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/snake.lisp b/snake.lisp index 30415ef..1ac1bb9 100644 --- a/snake.lisp +++ b/snake.lisp @@ -41,6 +41,10 @@ (defmethod (setf snake-position) (pos snake) (setf (first (segments-of snake)) pos)) +(defun snake-tail (snake) + "The rest of the SNAKE." + (rest (segments-of snake))) + ;; TODO(bsvercl): Don't allow Left<->Right Up<->Down (defun change-direction (snake new-direction) "Modify DIRECTION of SNAKE with NEW-DIRECTION." @@ -60,17 +64,10 @@ +segments-across-height+)))))) (defun hit-itself (snake) - ;; TODO(bsvercl): How do you make this elegant in LISP? - ;; I want to do something like - ;; for (segment = 0; segment < segments.len; segment++) { - ;; if segment position is equal to head position then return true - ;; // implicit continue - ;; } - (let ((segments (segments-of snake))) - (some #'(lambda (x) (eq x t)) - (loop for this = (first segments) - for that in (rest segments) - collect (vec2= this that))))) + (let ((head-position (snake-position snake))) + (loop for segment in (snake-tail snake) + when (vec2= segment head-position) + do (return-from hit-itself t)))) (gamekit:defgame snake-game () ((player :reader player-of)