;;;;Copyright (c) 2007 Jeremy English <jhe@jeremyenglish.org>
;;;;
;;;;Permission to use, copy, modify, distribute, and sell this software and its
;;;;documentation for any purpose is hereby granted without fee, provided that
;;;;the above copyright notice appear in all copies and that both that
;;;;copyright notice and this permission notice appear in supporting
;;;;documentation.  No representations are made about the suitability of this
;;;;software for any purpose.  It is provided "as is" without express or
;;;;implied warranty.
;;;;
;;;;Created: 20-June-2007
;;;;
;;;;

(use-package :chess-trace)

(defun diff (x y)
  (abs (- x y)))

(defun safe (column queens row &optional (queen-row 1))
  (if (null queens)
      t
      (cond
        ((= column (first queens)) nil)
        ((= (diff column (first queens)) (diff row queen-row)) nil)
        (t (safe column (rest queens) row (1+ queen-row))))))

(defun eight-queens (chess-board width height &key (row 1) (queens '()) (num-queens 8))
  (if (>= (length queens) num-queens)
      queens
      (dotimes (x width)
        (let ((column (1+ x)))
        (when (safe column (reverse queens) row)
          (add-frame chess-board (reverse queens))
            (let ((q8 (eight-queens chess-board width height
                                    :row (1+ row)
                                    :queens (cons column queens)
                                    :num-queens num-queens)))
              (unless (null q8) (return q8))))))))

(defun gif-eight-queens (filename)
  (let ((cb (make-chess-board)))
    (add-frame cb (reverse (eight-queens cb (blocks cb) (blocks cb))))
    (add-frame-delay 500 cb)
    (output-board cb filename)))

(defun add-frame (cb queens)
  (start-frame cb)
  (let ((y 0))
    (dolist (q queens)
      (let* ((x (1- q)))
        (put-piece-xy cb :name :queen :x x :y y :color :white))
      (incf y)))
  (end-frame cb :delay-time 30))






