Skip to content

Instantly share code, notes, and snippets.

@fouric
Last active November 25, 2019 19:01
Show Gist options
  • Save fouric/f04784e4e571ca4d4a6c9f3d81b93db6 to your computer and use it in GitHub Desktop.
Save fouric/f04784e4e571ca4d4a6c9f3d81b93db6 to your computer and use it in GitHub Desktop.
(defclass square ()
((state :initarg :state :accessor state) ;; -1 if queen, 0, 1, ... N otherwise
(x :initarg :x :accessor x)
(y :initarg :y :accessor y)))
(defmethod update-square ((self square) new-state)
(if (!= (state self) -1)
(incf (state self) new-state) ;; it's not a queen - update
(setf (state self) 0))) ;; remove the queen
(defmethod get-state ((self square))
(if (= (state self) -1)
"Q"
(state-self)))
(defmethod get-coord ((self square))
(let ((coord (list (x self) (y self))))
coord))
(defclass board ()
((n :initarg :n :accessor n)
(queens :initarg :queens :accessor queens)
(squares :initarg :squares :accessor squares)))
(defun make-board (n)
(let ((squares (make-array (list n n))))
(dolist (row n)
(dolist (col n)
(setf (aref squares row col) (make-square 0 row col))))))
(defun print-board (self)
(let ((n (n self)))
(format t "--------------------------------------------------------~%")
(dotimes (row n)
;; not sure how numpy works so this is probably translated wrong
(let ((a-row (make-array (list n 0))))
(dotimes (col n)
;; this append is definitely wrong
(setf a-row (append a-row (coerce 'string (get-state (aref (squares self) row col))))))
;; also wrong because i'm not sure what *a_row does
(format t "~a~%" a-row)))
(format t "--------------------------------------------------------~%")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment