aboutsummaryrefslogtreecommitdiff
path: root/server/game.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'server/game.lisp')
-rw-r--r--server/game.lisp11
1 files changed, 10 insertions, 1 deletions
diff --git a/server/game.lisp b/server/game.lisp
index a43cd10..517db0d 100644
--- a/server/game.lisp
+++ b/server/game.lisp
@@ -36,6 +36,7 @@
(format t "~%Direction: ~a" dir)
(format t "~%Food: ~a" food)))
+;;; Note that there is no waranty that nb food are added (ex: if food position collide with snake position)
(defgeneric add-food (g nb)
(:documentation "Add food on the game grid."))
@@ -96,6 +97,14 @@
;; Check if we loose
)
+;;; Function to compare two list of two elements
+(defun equal-coord (c1 c2)
+ (let ((x1 (car c1))
+ (x2 (car c2))
+ (y1 (car (cdr c1)))
+ (y2 (car (cdr c2))))
+ (and (eql x1 x2) (eql y1 y2))))
+
(defmethod add-food ((g game) nb)
(with-slots (snake grid-size food) g
(let ((snake-list (coerce snake 'list)) ; To be able to use the function member later
@@ -106,5 +115,5 @@
(dotimes (i nb)
(let ((x (random size-x))
(y (random size-y)))
- (when (eq (member (list x y) snake-list) nil) ; Add if there is no conflict between snake and food position
+ (when (eq (member (list x y) snake-list :test #'equal-coord) nil) ; Add if there is no conflict between snake and food position
(setf food (make-array (1+ food-size) :initial-contents `(,@food-list ,(list x y))))))))))