update packages
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
;;; org-sudoku.el --- Create and solve SUDOKU games in Org tables
|
||||
;;; org-sudoku.el --- Create and solve SUDOKU games in Org tables -*- lexical-binding: t; -*-
|
||||
|
||||
;; Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
||||
;;
|
||||
@@ -56,18 +56,17 @@ a game.")
|
||||
(defun org-sudoku-create (nfilled)
|
||||
"Create a sudoku game."
|
||||
(interactive "nNumber of pre-filled fields: ")
|
||||
(let ((sizesq org-sudoku-size)
|
||||
game)
|
||||
(loop for i from 1 to org-sudoku-size do
|
||||
(loop for j from 1 to org-sudoku-size do
|
||||
(push (list (cons i j) 0) game)))
|
||||
(let (game)
|
||||
(cl-loop for i from 1 to org-sudoku-size do
|
||||
(cl-loop for j from 1 to org-sudoku-size do
|
||||
(push (list (cons i j) 0) game)))
|
||||
(setq game (nreverse game))
|
||||
(random t)
|
||||
(setq game (org-sudoku-build-allowed game))
|
||||
(setq game (org-sudoku-set-field game (cons 1 1)
|
||||
(1+ (random org-sudoku-size))))
|
||||
(catch 'solved
|
||||
(let ((cnt 0))
|
||||
(let ((cnt 0) game1)
|
||||
(while t
|
||||
(catch 'abort
|
||||
(message "Attempt %d to create a game" (setq cnt (1+ cnt)))
|
||||
@@ -78,10 +77,10 @@ a game.")
|
||||
(setq game game1)
|
||||
(throw 'solved t))))))
|
||||
(let ((sqrtsize (floor (sqrt org-sudoku-size))))
|
||||
(loop for i from 1 to org-sudoku-size do
|
||||
(insert "| |\n")
|
||||
(if (and (= (mod i sqrtsize) 0) (< i org-sudoku-size))
|
||||
(insert "|-\n")))
|
||||
(cl-loop for i from 1 to org-sudoku-size do
|
||||
(insert "| |\n")
|
||||
(if (and (= (mod i sqrtsize) 0) (< i org-sudoku-size))
|
||||
(insert "|-\n")))
|
||||
(backward-char 5)
|
||||
(org-table-align))
|
||||
(while (> (length game) nfilled)
|
||||
@@ -159,9 +158,9 @@ A game structure is returned."
|
||||
(nreverse game)))
|
||||
|
||||
(defun org-sudoku-build-allowed (game)
|
||||
(let (i j v numbers)
|
||||
(loop for i from 1 to org-sudoku-size do
|
||||
(push i numbers))
|
||||
(let (i j v numbers a)
|
||||
(cl-loop for i from 1 to org-sudoku-size do
|
||||
(push i numbers))
|
||||
(setq numbers (nreverse numbers))
|
||||
;; add the lists of allowed values for each entry
|
||||
(setq game (mapcar
|
||||
@@ -209,14 +208,18 @@ If RANDOM is nil, always start with the first allowed value and try
|
||||
solving from there.
|
||||
STOP-AT can be a float time, the solver will abort at that time because
|
||||
it is probably stuck."
|
||||
(let (e v v1 allowed next g)
|
||||
(let (e
|
||||
;; FIXME unused.
|
||||
;; v
|
||||
v1 allowed next g)
|
||||
(when (and stop-at
|
||||
(> (float-time) stop-at))
|
||||
(setq game nil)
|
||||
(throw 'abort nil))
|
||||
(while (setq next (org-sudoku-find-next-constrained-field game))
|
||||
(setq e (assoc next game)
|
||||
v (nth 1 e)
|
||||
;; FIXME: Unused, potential fault is logic.
|
||||
;; v (nth 1 e)
|
||||
allowed (nth 2 e))
|
||||
(catch 'solved
|
||||
(if (= (length allowed) 1)
|
||||
@@ -242,14 +245,15 @@ it is probably stuck."
|
||||
(delq nil (mapcar (lambda (e) (if (> (nth 1 e) 0) nil t)) game)))
|
||||
|
||||
(defun org-sudoku-deep-copy (game)
|
||||
"Make a copy of the game so that manipulating the copy does not change the parent."
|
||||
"Make a copy of GAME.
|
||||
Manipulating the copy does not change the parent."
|
||||
(mapcar (lambda(e)
|
||||
(list (car e) (nth 1 e) (copy-sequence (nth 2 e))))
|
||||
game))
|
||||
|
||||
(defun org-sudoku-set-field (game field value)
|
||||
"Put VALUE into FIELD, and tell related fields that they cannot be VALUE."
|
||||
(let (i j)
|
||||
(let (i j a)
|
||||
(setq i (car field) j (cdr field))
|
||||
(setq a (assoc field game))
|
||||
(setf (nth 1 a) value)
|
||||
@@ -268,20 +272,21 @@ it is probably stuck."
|
||||
(let ((sqrtsize (floor (sqrt org-sudoku-size)))
|
||||
ll imin imax jmin jmax f)
|
||||
(setq f (cons i j))
|
||||
(loop for ii from 1 to org-sudoku-size do
|
||||
(or (= ii i) (push (cons ii j) ll)))
|
||||
(loop for jj from 1 to org-sudoku-size do
|
||||
(or (= jj j) (push (cons i jj) ll)))
|
||||
(cl-loop for ii from 1 to org-sudoku-size do
|
||||
(or (= ii i) (push (cons ii j) ll)))
|
||||
(cl-loop for jj from 1 to org-sudoku-size do
|
||||
(or (= jj j) (push (cons i jj) ll)))
|
||||
(setq imin (1+ (* sqrtsize (/ (1- i) sqrtsize)))
|
||||
imax (+ imin sqrtsize -1))
|
||||
(setq jmin (1+ (* sqrtsize (/ (1- j) sqrtsize)))
|
||||
jmax (+ jmin sqrtsize -1))
|
||||
(loop for ii from imin to imax do
|
||||
(loop for jj from jmin to jmax do
|
||||
(setq ff (cons ii jj))
|
||||
(or (equal ff f)
|
||||
(member ff ll)
|
||||
(push ff ll))))
|
||||
(let (ff)
|
||||
(cl-loop for ii from imin to imax do
|
||||
(cl-loop for jj from jmin to jmax do
|
||||
(setq ff (cons ii jj))
|
||||
(or (equal ff f)
|
||||
(member ff ll)
|
||||
(push ff ll)))))
|
||||
ll))
|
||||
|
||||
;;; org-sudoku ends here
|
||||
|
||||
Reference in New Issue
Block a user