1 ;;; mpuz.el --- multiplication puzzle for GNU Emacs
3 ;; Copyright (C) 1990, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Philippe Schnoebelen <phs@lsv.ens-cachan.fr>
6 ;; Overhauled: Daniel Pfeiffer <occitan@esperanto.org>
9 ;; This file is part of GNU Emacs.
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 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; `M-x mpuz' generates a random multiplication puzzle. This is a
27 ;; multiplication example in which each digit has been consistently replaced
28 ;; with some letter. Your job is to reconstruct the original digits. Type
29 ;; `?' while the mode is active for detailed help.
34 "Multiplication puzzle."
38 (random t
) ; randomize
40 (defcustom mpuz-silent
'error
41 "Set this to nil if you want dings on inputs.
42 The value t means never ding, and `error' means only ding on wrong input."
43 :type
'(choice (const :tag
"No" nil
)
45 (const :tag
"If correct" error
))
48 (defcustom mpuz-solve-when-trivial t
49 "Solve any row that can be trivially calculated from what you've found."
53 (defcustom mpuz-allow-double-multiplicator nil
54 "Allow 2nd factors like 33 or 77."
58 (defface mpuz-unsolved
59 '((((class color
)) (:foreground
"red1" :bold t
))
61 "Face to use for letters to be solved."
65 '((((class color
)) (:foreground
"green1" :bold t
))
67 "Face to use for solved digits."
71 '((((class color
)) (:foreground
"blue" :bold t
))
73 "Face to use for trivial digits solved for you."
77 '((t (:inherit variable-pitch
)))
78 "Face to use for text on right."
82 ;; Mpuz mode and keymaps
83 ;;----------------------
84 (defcustom mpuz-mode-hook nil
85 "Hook to run upon entry to mpuz."
90 (let ((map (make-sparse-keymap)))
92 (define-key map
(char-to-string ch
) 'mpuz-try-letter
))
93 "abcdefghijABCDEFGHIJ")
94 (define-key map
"\C-g" 'mpuz-offer-abort
)
95 (define-key map
"?" 'describe-mode
)
97 "Local keymap to use in Mult Puzzle.")
100 "Multiplication puzzle mode.
102 You have to guess which letters stand for which digits in the
103 multiplication displayed inside the `*Mult Puzzle*' buffer.
105 You may enter a guess for a letter's value by typing first the letter,
106 then the digit. Thus, to guess that A=3, type `A 3'.
108 To leave the game to do other editing work, just switch buffers.
109 Then you may resume the game with M-x mpuz.
110 You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
112 (kill-all-local-variables)
113 (setq major-mode
'mpuz-mode
114 mode-name
"Mult Puzzle"
116 (use-local-map mpuz-mode-map
)
117 (run-mode-hooks 'mpuz-mode-hook
))
120 ;; Some variables for statistics
121 ;;------------------------------
122 (defvar mpuz-nb-errors
0
123 "Number of errors made in current game.")
125 (defvar mpuz-nb-completed-games
0
126 "Number of games completed.")
128 (defvar mpuz-nb-cumulated-errors
0
129 "Number of errors made in previous games.")
132 ;; Some variables for game tracking
133 ;;---------------------------------
134 (defvar mpuz-in-progress nil
135 "True if a game is currently in progress.")
137 (defvar mpuz-found-digits
(make-bool-vector 10 nil
)
138 "A vector recording which digits have been decrypted.")
140 (defvar mpuz-trivial-digits
(make-bool-vector 10 nil
)
141 "A vector recording which digits have been solved for you.")
143 (defmacro mpuz-digit-solved-p
(digit)
144 `(or (aref mpuz-found-digits
,digit
)
145 (aref mpuz-trivial-digits
,digit
)))
148 ;; A puzzle uses a permutation of [0..9] into itself.
149 ;; We use both the permutation and its inverse.
150 ;;---------------------------------------------------
151 (defvar mpuz-digit-to-letter
(make-vector 10 0)
152 "A permutation from [0..9] to [0..9].")
154 (defvar mpuz-letter-to-digit
(make-vector 10 0)
155 "The inverse of `mpuz-digit-to-letter'.")
157 (defmacro mpuz-to-digit
(letter)
158 (list 'aref
'mpuz-letter-to-digit letter
))
160 (defmacro mpuz-to-letter
(digit)
161 (list 'aref
'mpuz-digit-to-letter digit
))
163 (defun mpuz-build-random-perm ()
164 "Initialize puzzle coding with a random permutation."
165 (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
169 (setq elem
(nth (random index
) letters
)
170 letters
(delq elem letters
)
172 (aset mpuz-digit-to-letter index elem
)
173 (aset mpuz-letter-to-digit elem index
))))
176 ;; A puzzle also uses a board displaying a multiplication.
177 ;; Every digit appears in the board, crypted or not.
178 ;;------------------------------------------------------
179 (defvar mpuz-board
(make-vector 10 nil
)
180 "The board associates to any digit the list of squares where it appears.")
182 (defun mpuz-put-number-on-board (number row
&rest columns
)
183 "Put (last digit of) NUMBER on ROW and COLUMNS of the puzzle board."
185 (dolist (column columns
)
186 (setq digit
(% number
10)
187 number
(/ number
10))
188 (aset mpuz-board digit
`((,row .
,column
) ,@(aref mpuz-board digit
))))))
190 (defun mpuz-check-all-solved (&optional row col
)
191 "Check whether all digits have been solved. Return t if yes."
193 (let (A B1 B2 C D E squares
)
194 (and mpuz-solve-when-trivial
197 (cond ((or (and (setq B1
(or B1
(mpuz-check-all-solved 4 7))
198 B2
(or B2
(mpuz-check-all-solved 4 9))
199 E
(or E
(mpuz-check-all-solved 10))
200 A
(or A
(mpuz-check-all-solved 2)))
202 (and E
(or A
(and B1 B2
))))
206 ((and (setq D
(or D
(mpuz-check-all-solved 8))
207 C
(or C
(mpuz-check-all-solved 6)))
210 ((and E
(not (eq C D
)))
211 (mpuz-solve (if D
6 8)))
212 ((and A
(not (eq B2 C
)))
213 (mpuz-solve (if C
4 6) (if C
9)))
214 ((and A
(not (eq B1 D
)))
215 (mpuz-solve (if D
4 8) (if D
7)))
216 ((and (not A
) (or (and B2 C
) (and B1 D
)))
219 (mapc (lambda (digit)
220 (and (not (mpuz-digit-solved-p digit
)) ; unsolved
221 (setq squares
(aref mpuz-board digit
))
224 (member (cons row col
) squares
)
226 squares
) ; and appearing in the puzzle!
227 (throw 'solved nil
)))
228 [0 1 2 3 4 5 6 7 8 9]))
232 ;; To build a puzzle, we take two random numbers and multiply them.
233 ;; We also take a random permutation for encryption.
234 ;; The random numbers are only use to see which digit appears in which square
235 ;; of the board. Everything is stored in individual squares.
236 ;;---------------------------------------------------------------------------
237 (defun mpuz-random-puzzle ()
238 "Draw random values to be multiplied in a puzzle."
239 (mpuz-build-random-perm)
240 (fillarray mpuz-board nil
) ; erase the board
241 ;; A,B,C,D & E, are the five rows of our multiplication.
242 ;; Choose random values, discarding cases with leading zeros in C or D.
243 (let* ((A (if mpuz-allow-double-multiplicator
(+ 112 (random 888))
244 (+ 125 (random 875))))
246 (B1 (+ min
(random (- 10 min
))))
248 (while (if (= B1
(setq B2
(+ min
(random (- 10 min
)))))
249 (not mpuz-allow-double-multiplicator
)))
253 ;; Individual digits are now put on their respective squares.
254 ;; [NB: A square is a pair (row . column) of the screen.]
255 (mpuz-put-number-on-board A
2 9 7 5)
256 (mpuz-put-number-on-board (+ (* B1
10) B2
) 4 9 7)
257 (mpuz-put-number-on-board C
6 9 7 5 3)
258 (mpuz-put-number-on-board D
8 7 5 3 1)
259 (mpuz-put-number-on-board E
10 9 7 5 3 1)))
263 (defconst mpuz-framework
266 Number of errors (this game): 0
270 Number of completed games: 0
272 --------- Average number of errors: 0.00
274 "The general picture of the puzzle screen, as a string.")
276 (defun mpuz-create-buffer ()
277 "Create (or recreate) the puzzle buffer. Return it."
278 (let ((buf (get-buffer-create "*Mult Puzzle*"))
279 (face '(face mpuz-text
))
281 (with-current-buffer buf
283 (insert mpuz-framework
)
284 (set-text-properties 13 42 face
)
285 (set-text-properties 79 105 face
)
286 (set-text-properties 128 153 face
)
289 (mpuz-paint-statistics))
292 (defun mpuz-paint-number (n &optional eol words
)
294 (let (buffer-read-only)
295 (delete-region (point)
296 (progn (backward-word (or words
1)) (point)))
299 (defun mpuz-paint-errors ()
300 "Paint error count on the puzzle screen."
301 (mpuz-switch-to-window)
302 (goto-char (point-min))
304 (mpuz-paint-number (prin1-to-string mpuz-nb-errors
)))
306 (defun mpuz-paint-statistics ()
307 "Paint statistics about previous games on the puzzle screen."
308 (goto-char (point-min))
310 (mpuz-paint-number (prin1-to-string mpuz-nb-completed-games
))
313 (if (zerop mpuz-nb-completed-games
)
315 (/ (+ 0.0 mpuz-nb-cumulated-errors
)
316 mpuz-nb-completed-games
)))
319 (defun mpuz-paint-board ()
320 "Paint board situation on the puzzle screen."
321 (mpuz-switch-to-window)
322 (mapc 'mpuz-paint-digit
[0 1 2 3 4 5 6 7 8 9])
323 (goto-char (point-min)))
325 (defun mpuz-paint-digit (digit)
326 "Paint all occurrences of DIGIT on the puzzle board."
327 (let ((char (if (mpuz-digit-solved-p digit
)
329 (+ (mpuz-to-letter digit
) ?A
)))
331 ,(cond ((aref mpuz-trivial-digits digit
) 'mpuz-trivial
)
332 ((aref mpuz-found-digits digit
) 'mpuz-solved
)
335 (mapc (lambda (square)
336 (goto-char (point-min))
337 (forward-line (1- (car square
))) ; line before column!
338 (move-to-column (cdr square
))
340 (set-text-properties (1- (point)) (point) face
)
342 (aref mpuz-board digit
))))
344 (defun mpuz-get-buffer ()
345 "Get the puzzle buffer if it exists."
346 (get-buffer "*Mult Puzzle*"))
348 (defun mpuz-switch-to-window ()
349 "Find or create the Mult-Puzzle buffer, and display it."
350 (let ((buf (mpuz-get-buffer)))
351 (or buf
(setq buf
(mpuz-create-buffer)))
352 (switch-to-buffer buf
)
353 (setq buffer-read-only t
)
359 (defun mpuz-start-new-game ()
360 "Start a new puzzle."
361 (message "Here we go...")
362 (setq mpuz-nb-errors
0
364 (fillarray mpuz-found-digits nil
) ; initialize mpuz-found-digits
365 (fillarray mpuz-trivial-digits nil
)
367 (mpuz-switch-to-window)
374 "Multiplication puzzle with GNU Emacs."
377 (mpuz-switch-to-window)
380 (mpuz-start-new-game)))
382 (defun mpuz-offer-abort ()
383 "Ask if user wants to abort current puzzle."
385 (if (y-or-n-p "Abort game? ")
386 (let ((buf (mpuz-get-buffer)))
387 (message "Mult Puzzle aborted.")
388 (setq mpuz-in-progress nil
390 (fillarray mpuz-board nil
)
391 (if buf
(kill-buffer buf
)))
394 (defun mpuz-ask-for-try ()
395 "Ask for user proposal in puzzle."
396 (message "Your try?"))
398 (defun mpuz-ding (error)
399 "Dings, unless global variable `mpuz-silent' forbids it."
400 (cond ((eq mpuz-silent t
))
401 ((not mpuz-silent
) (ding t
))
404 (defun mpuz-try-letter ()
405 "Propose a digit for a letter in puzzle."
408 (let (letter-char digit digit-char
)
409 (setq letter-char
(upcase last-command-event
)
410 digit
(mpuz-to-digit (- letter-char ?A
)))
411 (cond ((mpuz-digit-solved-p digit
)
412 (message "%c already solved." letter-char
)
414 ((null (aref mpuz-board digit
))
415 (message "%c does not appear." letter-char
)
417 ((progn (message "%c = " letter-char
)
418 ;; <char> has been entered.
419 ;; Print "<char> =" and
420 ;; read <num> or = <num>
421 (setq digit-char
(read-char))
422 (if (eq digit-char ?
=)
423 (setq digit-char
(read-char)))
424 (or (> digit-char ?
9) (< digit-char ?
0))) ; bad input
425 (message "%c = %c" letter-char digit-char
)
428 (mpuz-try-proposal letter-char digit-char
))))
429 (if (y-or-n-p "Start a new game? ")
430 (mpuz-start-new-game)
431 (message "OK. I won't."))))
433 (defun mpuz-try-proposal (letter-char digit-char
)
434 "Propose LETTER-CHAR as code for DIGIT-CHAR."
435 (let* ((letter (- letter-char ?A
))
436 (digit (- digit-char ?
0))
437 (correct-digit (mpuz-to-digit letter
)))
438 (cond ((mpuz-digit-solved-p correct-digit
)
439 (message "%c has already been found." (+ correct-digit ?
0)))
440 ((mpuz-digit-solved-p digit
)
441 (message "%c has already been placed." digit-char
))
442 ((= digit correct-digit
)
443 (message "%c = %c correct!" letter-char digit-char
)
445 (aset mpuz-found-digits digit t
) ; Mark digit as solved
446 (and (mpuz-check-all-solved)
448 (t ;;; incorrect guess
449 (message "%c = %c incorrect!" letter-char digit-char
)
451 (setq mpuz-nb-errors
(1+ mpuz-nb-errors
))
452 (mpuz-paint-errors)))))
454 (defun mpuz-close-game ()
455 "Housecleaning when puzzle has been solved."
456 (setq mpuz-in-progress nil
457 mpuz-nb-cumulated-errors
(+ mpuz-nb-cumulated-errors mpuz-nb-errors
)
458 mpuz-nb-completed-games
(1+ mpuz-nb-completed-games
))
459 (mpuz-paint-statistics)
460 (let ((message (format "Puzzle solved with %d error%s. That's %s"
462 (if (= mpuz-nb-errors
1) "" "s")
463 (cond ((= mpuz-nb-errors
0) "perfect!")
464 ((= mpuz-nb-errors
1) "very good!")
465 ((= mpuz-nb-errors
2) "good.")
466 ((= mpuz-nb-errors
3) "not bad.")
467 ((= mpuz-nb-errors
4) "not too bad...")
468 ((< mpuz-nb-errors
10) "bad!")
469 ((< mpuz-nb-errors
15) "awful.")
470 (t "not serious.")))))
471 (message "%s" message
)
473 (if (y-or-n-p (concat message
" Start a new game? "))
474 (mpuz-start-new-game)
475 (message "Good Bye!"))))
477 (defun mpuz-solve (&optional row col
)
478 "Find solution for autosolving."
479 (mapc (lambda (digit)
480 (or (mpuz-digit-solved-p digit
)
483 (member (cons row col
) (aref mpuz-board digit
))
484 (assq row
(aref mpuz-board digit
)))))
485 (aset mpuz-trivial-digits digit t
)))
486 [0 1 2 3 4 5 6 7 8 9])
489 (defun mpuz-show-solution (row)
490 "Display solution for debugging purposes."
492 (mpuz-switch-to-window)
493 (mpuz-solve (if row
(* 2 (prefix-numeric-value row
))))
495 (if (mpuz-check-all-solved)
500 ;;; mpuz.el ends here