-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.clp
67 lines (60 loc) · 2.01 KB
/
sudoku.clp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(clear)
(deftemplate cell (slot row) (slot col) (slot value))
(deffacts init
(cell (row 1) (col 1) (value 1))
(cell (row 1) (col 2) (value 0))
(cell (row 1) (col 3) (value 0))
(cell (row 1) (col 4) (value 0))
(cell (row 2) (col 1) (value 0))
(cell (row 2) (col 2) (value 4))
(cell (row 2) (col 3) (value 0))
(cell (row 2) (col 4) (value 0))
(cell (row 3) (col 1) (value 2))
(cell (row 3) (col 2) (value 3))
(cell (row 3) (col 3) (value 1))
(cell (row 3) (col 4) (value 4))
(cell (row 4) (col 1) (value 0))
(cell (row 4) (col 2) (value 0))
(cell (row 4) (col 3) (value 0))
(cell (row 4) (col 4) (value 0))
)
(defrule put-1
?fact <- (cell (row ?r1) (col ?c1) (value 0))
(cell (row ?r2) (col ?c2) (value 2 &: (or (eq ?r2 ?r1) (eq ?c2 ?c1))))
(cell (row ?r3) (col ?c3) (value 3 &: (or (eq ?r3 ?r1) (eq ?c3 ?c1))))
(cell (row ?r4) (col ?c4) (value 4 &: (or (eq ?r4 ?r1) (eq ?c4 ?c1))))
=>
(assert (cell (row ?r1) (col ?c1) (value 1)))
(retract ?fact)
)
(defrule put-2
?fact <- (cell (row ?r1) (col ?c1) (value 0))
(cell (row ?r2) (col ?c2) (value 1 &: (or (eq ?r2 ?r1) (eq ?c2 ?c1))))
(cell (row ?r3) (col ?c3) (value 3 &: (or (eq ?r3 ?r1) (eq ?c3 ?c1))))
(cell (row ?r4) (col ?c4) (value 4 &: (or (eq ?r4 ?r1) (eq ?c4 ?c1))))
=>
(assert (cell (row ?r1) (col ?c1) (value 2)))
(retract ?fact)
)
(defrule put-3
?fact <- (cell (row ?r1) (col ?c1) (value 0))
(cell (row ?r2) (col ?c2) (value 1 &: (or (eq ?r2 ?r1) (eq ?c2 ?c1))))
(cell (row ?r3) (col ?c3) (value 2 &: (or (eq ?r3 ?r1) (eq ?c3 ?c1))))
(cell (row ?r4) (col ?c4) (value 4 &: (or (eq ?r4 ?r1) (eq ?c4 ?c1))))
=>
(assert (cell (row ?r1) (col ?c1) (value 3)))
(retract ?fact)
)
(defrule put-4
?fact <- (cell (row ?r1) (col ?c1) (value 0))
(cell (row ?r2) (col ?c2) (value 1 &: (or (eq ?r2 ?r1) (eq ?c2 ?c1))))
(cell (row ?r3) (col ?c3) (value 2 &: (or (eq ?r3 ?r1) (eq ?c3 ?c1))))
(cell (row ?r4) (col ?c4) (value 3 &: (or (eq ?r4 ?r1) (eq ?c4 ?c1))))
=>
(assert (cell (row ?r1) (col ?c1) (value 4)))
(retract ?fact)
)
(watch all)
(reset)
(run)
(facts)