;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/domains/crossword.lisp

;;;; Crossword Puzzles as N-ary CSPs

;;; Two formulations, both solved by AC-SOLVER and AC-SEARCH-SOLVER of
;;; search/algorithms/csp-nary.lisp: one variable per word slot, with
;;; binary constraints where the slots cross (MAKE-WORD-CROSSWORD-CSP), and
;;; one variable per cell, with an n-ary constraint on each run of cells
;;; (MAKE-CROSSWORD-CSP).

(defun make-word-crossword-csp ()
  "A small crossword with a variable for each word slot."
  (let ((three '("ant" "big" "bus" "car" "has")))
    (make-nary-csp
     :domains `((one-across ,@three)
		(one-down "book" "buys" "hold" "lane" "year")
		(two-down "ginger" "search" "symbol" "syntax")
		(three-across "book" "buys" "hold" "land" "year")
		(four-across ,@three))
     :constraints
     (list (make-nary-constraint :scope '(one-across one-down)
				 :condition (meet-at-constraint 0 0))
	   (make-nary-constraint :scope '(one-across two-down)
				 :condition (meet-at-constraint 2 0))
	   (make-nary-constraint :scope '(three-across two-down)
				 :condition (meet-at-constraint 2 2))
	   (make-nary-constraint :scope '(three-across one-down)
				 :condition (meet-at-constraint 0 2))
	   (make-nary-constraint :scope '(four-across two-down)
				 :condition (meet-at-constraint 0 4))))))

(defparameter *crossword1*
  '("___**"
    "_*_**"
    "____*"
    "_*_**"
    "**___"
    "**_**")
  "A crossword grid: _ is a cell to fill, * a blocked cell.")

(defparameter *crossword-words1*
  '("ant" "big" "bus" "car" "has" "book" "buys" "hold" "lane" "year"
    "ginger" "search" "symbol" "syntax"))

(defun crossword-cell (row col)
  "The name of the variable of a cell."
  (format nil "p~D_~D" col row))

(defun make-crossword-csp (puzzle words)
  "A CSP with a variable (a lowercase letter) for each cell of PUZZLE, a
  list of strings, and a constraint that each run of two or more cells,
  across or down, spells one of WORDS."
  (let ((rows (length puzzle))
	(cols (length (first puzzle)))
	(letters (coerce "abcdefghijklmnopqrstuvwxyz" 'list))
	(domains nil)
	(constraints nil))
    (flet ((cell? (row col) (char= (char (nth row puzzle) col) #\_))
	   (add-run (scope)
	     (when (> (length scope) 1)
	       (push (make-nary-constraint :scope (reverse scope)
					   :condition (is-word-constraint words))
		     constraints))))
      (dotimes (row rows)
	(dotimes (col cols)
	  (when (cell? row col)
	    (push (cons (crossword-cell row col) (copy-list letters))
		  domains))))
      ;; Runs across, then runs down
      (dotimes (row rows)
	(let ((scope nil))
	  (dotimes (col cols)
	    (if (cell? row col)
		(push (crossword-cell row col) scope)
	      (progn (add-run scope) (setf scope nil))))
	  (add-run scope)))
      (dotimes (col cols)
	(let ((scope nil))
	  (dotimes (row rows)
	    (if (cell? row col)
		(push (crossword-cell row col) scope)
	      (progn (add-run scope) (setf scope nil))))
	  (add-run scope))))
    (make-nary-csp :domains (nreverse domains)
		   :constraints (nreverse constraints))))

(defun display-crossword (puzzle assignment &optional (stream t))
  "Print the grid with the letters of ASSIGNMENT, an alist."
  (dotimes (row (length puzzle))
    (dotimes (col (length (nth row puzzle)))
      (if (char= (char (nth row puzzle) col) #\*)
	  (format stream "[*] ")
	(let ((binding (assoc (crossword-cell row col) assignment
			      :test #'equal)))
	  (format stream "[~A] "
		  (if binding (char-upcase (cdr binding)) "_")))))
    (format stream "~%")))
