Manually revert to the Release 7.8.04 tag.
[org-mode.git] / contrib / lisp / org-sudoku.el
blob6977f1f51d55270f54ef78f72a7751a81f306890
1 ;;; org-sudoku.el --- Greate and solve SUDOKU games in Org tables
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp, games
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
9 ;;
10 ;; This file is not yet part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This is a quick hack to create and solve SUDOKU games in org tables.
32 ;; Commands:
34 ;; org-sudoku-create Create a new SUDOKU game
35 ;; org-sudoku-solve-field Solve the field at point in a SUDOKU game
36 ;; (this is for cheeting when you are stuck)
37 ;; org-sudoku-solve Solve the entire game
40 ;;; Code
42 (require 'org)
43 (require 'org-table)
45 ;;; Customization
47 (defvar org-sudoku-size 9
48 "The size of the sudoku game, 9 for a 9x9 game and 4 for a 4x4 game.
49 Larger games do not seem to work because of limited resources - even though
50 the algorithm is general.")
52 (defvar org-sudoku-timeout 2.0
53 "Timeout for finding a solution when creating a new game.
54 After this timeout, the program starts over from scratch to create
55 a game.")
57 ;;; Interactive commands
59 (defun org-sudoku-create (nfilled)
60 "Create a sudoku game."
61 (interactive "nNumber of pre-filled fields: ")
62 (let ((sizesq org-sudoku-size)
63 game)
64 (loop for i from 1 to org-sudoku-size do
65 (loop for j from 1 to org-sudoku-size do
66 (push (list (cons i j) 0) game)))
67 (setq game (nreverse game))
68 (random t)
69 (setq game (org-sudoku-build-allowed game))
70 (setq game (org-sudoku-set-field game (cons 1 1)
71 (1+ (random org-sudoku-size))))
72 (catch 'solved
73 (let ((cnt 0))
74 (while t
75 (catch 'abort
76 (message "Attempt %d to create a game" (setq cnt (1+ cnt)))
77 (setq game1 (org-sudoku-deep-copy game))
78 (setq game1 (org-sudoku-solve-game
79 game1 'random (+ (float-time) org-sudoku-timeout)))
80 (when game1
81 (setq game game1)
82 (throw 'solved t))))))
83 (let ((sqrtsize (floor (sqrt org-sudoku-size))))
84 (loop for i from 1 to org-sudoku-size do
85 (insert "| |\n")
86 (if (and (= (mod i sqrtsize) 0) (< i org-sudoku-size))
87 (insert "|-\n")))
88 (backward-char 5)
89 (org-table-align))
90 (while (> (length game) nfilled)
91 (setq game (delete (nth (1+ (random (length game))) game) game)))
92 (mapc (lambda (e)
93 (org-table-put (caar e) (cdar e) (int-to-string (nth 1 e))))
94 game)
95 (org-table-align)
96 (org-table-goto-line 1)
97 (org-table-goto-column 1)
98 (message "Enjoy!")))
100 (defun org-sudoku-solve ()
101 "Solve the sudoku game in the table at point."
102 (interactive)
103 (unless (org-at-table-p)
104 (error "not at a table"))
105 (let (game)
106 (setq game (org-sudoku-get-game))
107 (setq game (org-sudoku-build-allowed game))
108 (setq game (org-sudoku-solve-game game))
109 ;; Insert the values
110 (mapc (lambda (e)
111 (org-table-put (caar e) (cdar e) (int-to-string (nth 1 e))))
112 game)
113 (org-table-align)))
115 (defun org-sudoku-solve-field ()
116 "Just solve the field at point.
117 This works by solving the whole game, then inserting only the single field."
118 (interactive)
119 (unless (org-at-table-p)
120 (error "Not at a table"))
121 (org-table-check-inside-data-field)
122 (let ((i (org-table-current-dline))
123 (j (org-table-current-column))
124 game)
125 (setq game (org-sudoku-get-game))
126 (setq game (org-sudoku-build-allowed game))
127 (setq game (org-sudoku-solve-game game))
128 (if game
129 (progn
130 (org-table-put i j (number-to-string
131 (nth 1 (assoc (cons i j) game)))
132 'align)
133 (org-table-goto-line i)
134 (org-table-goto-column j))
135 (error "No solution"))))
137 ;;; Internal functions
139 (defun org-sudoku-get-game ()
140 "Interpret table at point as sudoku game and read it.
141 A game structure is returned."
142 (let (b e g i j game)
144 (org-table-goto-line 1)
145 (org-table-goto-column 1)
146 (setq b (point))
147 (org-table-goto-line org-sudoku-size)
148 (org-table-goto-column org-sudoku-size)
149 (setq e (point))
150 (setq g (org-table-copy-region b e))
151 (setq i 0 j 0)
152 (mapc (lambda (c)
153 (setq i (1+ i) j 0)
154 (mapc
155 (lambda (v)
156 (setq j (1+ j))
157 (push (list (cons i j)
158 (string-to-number v))
159 game))
162 (nreverse game)))
164 (defun org-sudoku-build-allowed (game)
165 (let (i j v numbers)
166 (loop for i from 1 to org-sudoku-size do
167 (push i numbers))
168 (setq numbers (nreverse numbers))
169 ;; add the lists of allowed values for each entry
170 (setq game (mapcar
171 (lambda (e)
172 (list (car e) (nth 1 e)
173 (if (= (nth 1 e) 0)
174 (copy-sequence numbers)
175 nil)))
176 game))
177 ;; remove the known values from the list of allowed values
178 (mapc
179 (lambda (e)
180 (setq i (caar e) j (cdar e) v (cadr e))
181 (when (> v 0)
182 ;; We do have a value here
183 (mapc
184 (lambda (f)
185 (setq a (assoc f game))
186 (setf (nth 2 a) (delete v (nth 2 a))))
187 (cons (cons i j) (org-sudoku-rel-fields i j)))))
188 game)
189 game))
191 (defun org-sudoku-find-next-constrained-field (game)
192 (setq game (mapcar (lambda (e) (if (nth 2 e) e nil)) game))
193 (setq game (delq nil game))
194 (let (va vb la lb)
195 (setq game
196 (sort game (lambda (a b)
197 (setq va (nth 1 a) vb (nth 1 b)
198 la (length (nth 2 a)) lb (length (nth 2 b)))
199 (cond
200 ((and (= va 0) (> vb 0)) t)
201 ((and (> va 0) (= vb 0)) nil)
202 ((not (= (* va vb) 0)) nil)
203 (t (< la lb))))))
204 (if (or (not game) (> 0 (nth 1 (car game))))
206 (caar game))))
208 (defun org-sudoku-solve-game (game &optional random stop-at)
209 "Solve GAME.
210 If RANDOM is non-nit, select candidates randomly from a fields option.
211 If RANDOM is nil, always start with the first allowed value and try
212 solving from there.
213 STOP-AT can be a float time, the solver will abort at that time because
214 it is probably stuck."
215 (let (e v v1 allowed next g)
216 (when (and stop-at
217 (> (float-time) stop-at))
218 (setq game nil)
219 (throw 'abort nil))
220 (while (setq next (org-sudoku-find-next-constrained-field game))
221 (setq e (assoc next game)
222 v (nth 1 e)
223 allowed (nth 2 e))
224 (catch 'solved
225 (if (= (length allowed) 1)
226 (setq game (org-sudoku-set-field game next (car allowed)))
227 (while allowed
228 (setq g (org-sudoku-deep-copy game))
229 (if (not random)
230 (setq v1 (car allowed))
231 (setq v1 (nth (random (length allowed)) allowed)))
232 (setq g (org-sudoku-set-field g next v1))
233 (setq g (org-sudoku-solve-game g random stop-at))
234 (when g
235 (setq game g)
236 (throw 'solved g)))
237 (setq game nil))))
238 (if (or (not game)
239 (org-sudoku-unknown-field-p game))
241 game)))
243 (defun org-sudoku-unknown-field-p (game)
244 "Are there still unknown fields in the game?"
245 (delq nil (mapcar (lambda (e) (if (> (nth 1 e) 0) nil t)) game)))
247 (defun org-sudoku-deep-copy (game)
248 "Make a copy of the game so that manipulating the copy does not change the parent."
249 (mapcar (lambda(e)
250 (list (car e) (nth 1 e) (copy-sequence (nth 2 e))))
251 game))
253 (defun org-sudoku-set-field (game field value)
254 "Put VALUE into FIELD, and tell related fields that they cannot be VALUE."
255 (let (i j)
256 (setq i (car field) j (cdr field))
257 (setq a (assoc field game))
258 (setf (nth 1 a) value)
259 (setf (nth 2 a) nil)
261 ;; Remove value from all related fields
262 (mapc
263 (lambda (f)
264 (setq a (assoc f game))
265 (setf (nth 2 a) (delete value (nth 2 a))))
266 (org-sudoku-rel-fields i j))
267 game))
269 (defun org-sudoku-rel-fields (i j)
270 "Compute the list of related fields for field (i j)."
271 (let ((sqrtsize (floor (sqrt org-sudoku-size)))
272 ll imin imax jmin jmax f)
273 (setq f (cons i j))
274 (loop for ii from 1 to org-sudoku-size do
275 (or (= ii i) (push (cons ii j) ll)))
276 (loop for jj from 1 to org-sudoku-size do
277 (or (= jj j) (push (cons i jj) ll)))
278 (setq imin (1+ (* sqrtsize (/ (1- i) sqrtsize)))
279 imax (+ imin sqrtsize -1))
280 (setq jmin (1+ (* sqrtsize (/ (1- j) sqrtsize)))
281 jmax (+ jmin sqrtsize -1))
282 (loop for ii from imin to imax do
283 (loop for jj from jmin to jmax do
284 (setq ff (cons ii jj))
285 (or (equal ff f)
286 (member ff ll)
287 (push ff ll))))
288 ll))
290 ;;; org-sudoku ends here