org-sudoku.el 8.8 KB

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