;**************************************************************************
; File: fqueens.clp
; Content: Solving the 5-queens problem with only one rule
; Authors: Jean-Marc Labat and Michel Futtersack
; Version: 1.0 (September 1997)
; Contact: Jean-Marc.Labat,Michel.Futtersack@lip6.fr
;**************************************************************************


(deftemplate queen (slot row)(slot column))

(defrule init
=>
(loop-for-count (?i 1 5) do
(loop-for-count (?j 1 5) do
(assert (queen (row ?i)(column ?j))))))

(defrule 5-queens
(queen (row 1)(column ?y1))
(queen (row 2)(column ?y2&~?y1&:(and
(<> (+ 1 ?y1)(+ 2 ?y2)) (<> (- 1 ?y1)(- 2 ?y2)))))
(queen (row 3)(column ?y3&~?y1&~?y2&:(and
(<> (+ 1 ?y1)(+ 3 ?y3)) (<> (- 1 ?y1)(- 3 ?y3))
(<> (+ 2 ?y2)(+ 3 ?y3)) (<> (- 2 ?y2)(- 3 ?y3)))))
(queen (row 4)(column ?y4&~?y1&~?y2&~?y3&:(and
(<> (+ 1 ?y1)(+ 4 ?y4)) (<> (- 1 ?y1)(- 4 ?y4))
(<> (+ 2 ?y2)(+ 4 ?y4)) (<> (- 2 ?y2)(- 4 ?y4))
(<> (+ 3 ?y3)(+ 4 ?y4)) (<> (- 3 ?y3)(- 4 ?y4)))))
(queen (row 5)(column ?y5&~?y1&~?y2&~?y3&~?y4&:(and
(<> (+ 1 ?y1)(+ 5 ?y5)) (<> (- 1 ?y1)(- 5 ?y5))
(<> (+ 2 ?y2)(+ 5 ?y5)) (<> (- 2 ?y2)(- 5 ?y5))
(<> (+ 3 ?y3)(+ 5 ?y5)) (<> (- 3 ?y3)(- 5 ?y5))
(<> (+ 4 ?y4)(+ 5 ?y5)) (<> (- 4 ?y4)(- 5 ?y5)))))
=>
(loop-for-count (?i 1 (- ?y1 1)) do (printout t "* "))(printout t "X ")(loop-for-count (?i (+ ?y1 1) 5) do (printout t "* "))(printout t crlf)
(loop-for-count (?i 1 (- ?y2 1)) do (printout t "* "))(printout t "X ")(loop-for-count (?i (+ ?y2 1) 5) do (printout t "* "))(printout t crlf)
(loop-for-count (?i 1 (- ?y3 1)) do (printout t "* "))(printout t "X ")(loop-for-count (?i (+ ?y3 1) 5) do (printout t "* "))(printout t crlf)
(loop-for-count (?i 1 (- ?y4 1)) do (printout t "* "))(printout t "X ")(loop-for-count (?i (+ ?y4 1) 5) do (printout t "* "))(printout t crlf)
(loop-for-count (?i 1 (- ?y5 1)) do (printout t "* "))(printout t "X ")(loop-for-count (?i (+ ?y5 1) 5) do (printout t "* "))(printout t crlf)
(printout t " Type RETURN for the next solution : ")
(readline)
(printout t crlf crlf)
)