aboutsummaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorLoic Guegan <manzerberdes@gmx.com>2019-05-08 18:59:45 +0200
committerLoic Guegan <manzerberdes@gmx.com>2019-05-08 18:59:45 +0200
commit3c42acfdd9f64e69420f1a0db4d4caf14f4ce6d4 (patch)
treeec05e4448388f5c5c6398b2ae8faf95d98d6da0e /server
parent614c7a627897da3250bcc9853ddea8fe4b523e9e (diff)
Change array to list: simple and faster
Diffstat (limited to 'server')
-rw-r--r--server/game.lisp35
1 files changed, 16 insertions, 19 deletions
diff --git a/server/game.lisp b/server/game.lisp
index 517db0d..5c0e368 100644
--- a/server/game.lisp
+++ b/server/game.lisp
@@ -16,23 +16,23 @@
:initarg :grid-size
:initform (list 30 30))
(food
- :initform (make-array 0))))
+ :initform (make-list 0))))
;;; Class constructor to initialize the snake
(defmethod initialize-instance :after ((g game) &key)
(with-slots (snake initial-size initial-position) g
- (setf snake (make-array initial-size :initial-element initial-position))))
+ (setf snake (make-list initial-size :initial-element initial-position))))
;;; Pretty-print a game
(defmethod print-object ((g game) stream)
(with-slots (snake dir food) g
(format t "Snake: ")
- (dotimes (i (first (array-dimensions snake)))
- (let ((elem (aref snake i)))
+ (dotimes (i (length snake))
+ (let ((elem (nth i snake)))
(unless (eql i 0)
(format t "<="))
(format t "(~a,~a)" (first elem) (second elem))))
- (format t "~%Size: ~a" (first (array-dimensions snake)))
+ (format t "~%Size: ~a" (length snake))
(format t "~%Direction: ~a" dir)
(format t "~%Food: ~a" food)))
@@ -54,11 +54,11 @@
;;; Grow snake of grow-size amount (snake is growing by the tail)
(defun grow-snake (snake grow-size)
- (let* ((old-size (first (array-dimensions snake)))
+ (let* ((old-size (length snake))
(new-size (+ old-size grow-size))
- (tail (aref snake (- old-size 1)))
- (new-tail (coerce (make-array grow-size :initial-element tail) 'list))
- (new-snake (make-array new-size :initial-contents `(,@(coerce snake 'list) ,@new-tail))))
+ (tail (nth (- old-size 1) snake))
+ (new-tail (make-list grow-size :initial-element tail))
+ (new-snake (coerce (make-array new-size :initial-contents `(,@snake ,@new-tail)) 'list)))
new-snake))
@@ -75,8 +75,8 @@
;; Then, move the snake
(with-slots (snake dir) g
(let ((last-old-x nil) (last-old-y nil))
- (dotimes (i (first (array-dimensions snake)))
- (let ((elem (aref snake i)))
+ (dotimes (i (length snake))
+ (let ((elem (nth i snake)))
(let ((x (first elem)) (y (second elem)))
;; Move snake
(if (eql i 0) ; Move head
@@ -92,7 +92,7 @@
(rotatef x last-old-x)
(rotatef y last-old-y)))
(format t "l:(~a,~a) n:(~a,~a) ~%" last-old-x last-old-y x y)
- (setf (aref snake i) (list x y))))))) ; Apply new snake location (update snake slot)
+ (setf (nth i snake) (list x y))))))) ; Apply new snake location (update snake slot)
;; Check if we loose
)
@@ -107,13 +107,10 @@
(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
- (food-list (coerce food 'list))
- (size-x (first grid-size))
- (size-y (second grid-size))
- (food-size (first (array-dimensions food))))
+ (let ((size-x (first grid-size))
+ (size-y (second grid-size)))
(dotimes (i nb)
(let ((x (random size-x))
(y (random size-y)))
- (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))))))))))
+ (when (eq (member (list x y) snake :test #'equal-coord) nil) ; Add if there is no conflict between snake and food position
+ (setf food (coerce (make-array (1+ (length food)) :initial-contents `(,@food ,(list x y))) 'list))))))))