Skip to content
This repository has been archived by the owner on May 20, 2021. It is now read-only.

Commit

Permalink
Better HIT-ITSELF implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
svercl committed May 30, 2019
1 parent 03d3fae commit 2a1c03e
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions snake.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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)
Expand Down

0 comments on commit 2a1c03e

Please sign in to comment.