(in-package :remote-snake-server-game)

;;; Return true when doing a legal move (ex: snake can goto left when it is in the right direction)
(defun legal-move (active-dir dir)
  (or
   (eq active-dir dir) ; Goto same direction
   (and (or (eq dir :up) (eq dir :down)) ; Got up or down only if the snake is on the left or right direction
        (or (eq active-dir :left) (eq active-dir :right)))
   (and (or (eq dir :left) (eq dir :right)) ; Goto left or right only if the snake is on the up or down direction
        (or (eq active-dir :up) (eq active-dir :down)))))

;;; Grow snake of grow-size amount (snake is growing by the tail)
(defun grow-snake (snake grow-size)
  (let* ((old-size (length snake))
         (new-size (+ old-size grow-size))
         (tail (nth (- old-size 1) snake))
         (new-tail (make-list grow-size :initial-element tail)))
    (append snake new-tail)))

;;; 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))))

;; Remove a coord from a list of coord (usefull when snake is eating a food)
(defun remove-coord (l c)
  (if (eql l '()) l ; Terminal condition
    (let ((head (first l)))
      (if (equal-coord head c)
          (remove-coord (cdr l) c)
        (append `(,head) (remove-coord (cdr l) c))))))