Debug add-food

This commit is contained in:
Loic Guegan 2019-05-08 18:33:08 +02:00
parent f72400bf22
commit 614c7a6278

View file

@ -36,6 +36,7 @@
(format t "~%Direction: ~a" dir) (format t "~%Direction: ~a" dir)
(format t "~%Food: ~a" food))) (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) (defgeneric add-food (g nb)
(:documentation "Add food on the game grid.")) (:documentation "Add food on the game grid."))
@ -96,6 +97,14 @@
;; Check if we loose ;; 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) (defmethod add-food ((g game) nb)
(with-slots (snake grid-size food) g (with-slots (snake grid-size food) g
(let ((snake-list (coerce snake 'list)) ; To be able to use the function member later (let ((snake-list (coerce snake 'list)) ; To be able to use the function member later
@ -106,5 +115,5 @@
(dotimes (i nb) (dotimes (i nb)
(let ((x (random size-x)) (let ((x (random size-x))
(y (random size-y))) (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)))))))))) (setf food (make-array (1+ food-size) :initial-contents `(,@food-list ,(list x y))))))))))