]> code.delx.au - gnu-emacs/blob - lisp/play/mpuz.el
(mouse-drag-track): Renamed, from `mouse-drag-region-1'. Includes
[gnu-emacs] / lisp / play / mpuz.el
1 ;;; mpuz.el --- multiplication puzzle for GNU Emacs
2
3 ;; Copyright (C) 1990, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Philippe Schnoebelen <phs@lsv.ens-cachan.fr>
6 ;; Overhauled: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: games
8
9 ;; This file is 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 2, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; `M-x mpuz' generates a random multiplication puzzle. This is a
29 ;; multiplication example in which each digit has been consistently replaced
30 ;; with some letter. Your job is to reconstruct the original digits. Type
31 ;; `?' while the mode is active for detailed help.
32
33 ;;; Code:
34
35 (defgroup mpuz nil
36 "Multiplication puzzle."
37 :prefix "mpuz-"
38 :group 'games)
39
40 (random t) ; randomize
41
42 (defcustom mpuz-silent 'error
43 "*Set this to nil if you want dings on inputs.
44 t means never ding, and `error' means only ding on wrong input."
45 :type '(choice (const :tag "No" nil)
46 (const :tag "Yes" t)
47 (const :tag "If correct" error))
48 :group 'mpuz)
49
50 (defcustom mpuz-solve-when-trivial t
51 "*Solve any row that can be trivially calculated from what you've found."
52 :type 'boolean
53 :group 'mpuz)
54
55 (defcustom mpuz-allow-double-multiplicator nil
56 "*Allow 2nd factors like 33 or 77."
57 :type 'boolean
58 :group 'mpuz)
59
60 (defface mpuz-unsolved
61 '((((class color)) (:foreground "red1" :bold t))
62 (t (:bold t)))
63 "*Face to use for letters to be solved."
64 :group 'mpuz)
65
66 (defface mpuz-solved
67 '((((class color)) (:foreground "green1" :bold t))
68 (t (:bold t)))
69 "*Face to use for solved digits."
70 :group 'mpuz)
71
72 (defface mpuz-trivial
73 '((((class color)) (:foreground "blue" :bold t))
74 (t (:bold t)))
75 "*Face to use for trivial digits solved for you."
76 :group 'mpuz)
77
78 (defface mpuz-text
79 '((t (:inherit variable-pitch)))
80 "*Face to use for text on right."
81 :group 'mpuz)
82
83 \f
84 ;; Mpuz mode and keymaps
85 ;;----------------------
86 (defcustom mpuz-mode-hook nil
87 "Hook to run upon entry to mpuz."
88 :type 'hook
89 :group 'mpuz)
90
91 (defvar mpuz-mode-map nil
92 "Local keymap to use in Mult Puzzle.")
93
94 (if mpuz-mode-map nil
95 (setq mpuz-mode-map (make-sparse-keymap))
96 (define-key mpuz-mode-map "a" 'mpuz-try-letter)
97 (define-key mpuz-mode-map "b" 'mpuz-try-letter)
98 (define-key mpuz-mode-map "c" 'mpuz-try-letter)
99 (define-key mpuz-mode-map "d" 'mpuz-try-letter)
100 (define-key mpuz-mode-map "e" 'mpuz-try-letter)
101 (define-key mpuz-mode-map "f" 'mpuz-try-letter)
102 (define-key mpuz-mode-map "g" 'mpuz-try-letter)
103 (define-key mpuz-mode-map "h" 'mpuz-try-letter)
104 (define-key mpuz-mode-map "i" 'mpuz-try-letter)
105 (define-key mpuz-mode-map "j" 'mpuz-try-letter)
106 (define-key mpuz-mode-map "A" 'mpuz-try-letter)
107 (define-key mpuz-mode-map "B" 'mpuz-try-letter)
108 (define-key mpuz-mode-map "C" 'mpuz-try-letter)
109 (define-key mpuz-mode-map "D" 'mpuz-try-letter)
110 (define-key mpuz-mode-map "E" 'mpuz-try-letter)
111 (define-key mpuz-mode-map "F" 'mpuz-try-letter)
112 (define-key mpuz-mode-map "G" 'mpuz-try-letter)
113 (define-key mpuz-mode-map "H" 'mpuz-try-letter)
114 (define-key mpuz-mode-map "I" 'mpuz-try-letter)
115 (define-key mpuz-mode-map "J" 'mpuz-try-letter)
116 (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
117 (define-key mpuz-mode-map "?" 'describe-mode))
118
119 (defun mpuz-mode ()
120 "Multiplication puzzle mode.
121
122 You have to guess which letters stand for which digits in the
123 multiplication displayed inside the `*Mult Puzzle*' buffer.
124
125 You may enter a guess for a letter's value by typing first the letter,
126 then the digit. Thus, to guess that A=3, type `A 3'.
127
128 To leave the game to do other editing work, just switch buffers.
129 Then you may resume the game with M-x mpuz.
130 You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
131 (interactive)
132 (kill-all-local-variables)
133 (setq major-mode 'mpuz-mode
134 mode-name "Mult Puzzle"
135 tab-width 30)
136 (use-local-map mpuz-mode-map)
137 (run-mode-hooks 'mpuz-mode-hook))
138
139 \f
140 ;; Some variables for statistics
141 ;;------------------------------
142 (defvar mpuz-nb-errors 0
143 "Number of errors made in current game.")
144
145 (defvar mpuz-nb-completed-games 0
146 "Number of games completed.")
147
148 (defvar mpuz-nb-cumulated-errors 0
149 "Number of errors made in previous games.")
150
151
152 ;; Some variables for game tracking
153 ;;---------------------------------
154 (defvar mpuz-in-progress nil
155 "True if a game is currently in progress.")
156
157 (defvar mpuz-found-digits (make-bool-vector 10 nil)
158 "A vector recording which digits have been decrypted.")
159
160 (defvar mpuz-trivial-digits (make-bool-vector 10 nil)
161 "A vector recording which digits have been solved for you.")
162
163 (defmacro mpuz-digit-solved-p (digit)
164 `(or (aref mpuz-found-digits ,digit)
165 (aref mpuz-trivial-digits ,digit)))
166
167
168 ;; A puzzle uses a permutation of [0..9] into itself.
169 ;; We use both the permutation and its inverse.
170 ;;---------------------------------------------------
171 (defvar mpuz-digit-to-letter (make-vector 10 0)
172 "A permutation from [0..9] to [0..9].")
173
174 (defvar mpuz-letter-to-digit (make-vector 10 0)
175 "The inverse of mpuz-digit-to-letter.")
176
177 (defmacro mpuz-to-digit (letter)
178 (list 'aref 'mpuz-letter-to-digit letter))
179
180 (defmacro mpuz-to-letter (digit)
181 (list 'aref 'mpuz-digit-to-letter digit))
182
183 (defun mpuz-build-random-perm ()
184 "Initialize puzzle coding with a random permutation."
185 (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
186 (index 10)
187 elem)
188 (while letters
189 (setq elem (nth (random index) letters)
190 letters (delq elem letters)
191 index (1- index))
192 (aset mpuz-digit-to-letter index elem)
193 (aset mpuz-letter-to-digit elem index))))
194
195
196 ;; A puzzle also uses a board displaying a multiplication.
197 ;; Every digit appears in the board, crypted or not.
198 ;;------------------------------------------------------
199 (defvar mpuz-board (make-vector 10 nil)
200 "The board associates to any digit the list of squares where it appears.")
201
202 (defun mpuz-put-number-on-board (number row &rest l)
203 "Put (last digit of) NUMBER on ROW and COLUMNS of the puzzle board."
204 (let (digit)
205 (while l
206 (setq digit (% number 10)
207 number (/ number 10))
208 (aset mpuz-board digit `((,row . ,(car l)) ,@(aref mpuz-board digit)))
209 (setq l (cdr l)))))
210
211 (defun mpuz-check-all-solved (&optional row col)
212 "Check whether all digits have been solved. Return t if yes."
213 (catch 'solved
214 (let (A B1 B2 C D E squares)
215 (and mpuz-solve-when-trivial
216 (not row)
217 (while
218 (cond ((or (and (setq B1 (or B1 (mpuz-check-all-solved 4 7))
219 B2 (or B2 (mpuz-check-all-solved 4 9))
220 E (or E (mpuz-check-all-solved 10))
221 A (or A (mpuz-check-all-solved 2)))
222 B1 B2)
223 (and E (or A (and B1 B2))))
224 (mpuz-solve)
225 (mpuz-paint-board)
226 (throw 'solved t))
227 ((and (setq D (or D (mpuz-check-all-solved 8))
228 C (or C (mpuz-check-all-solved 6)))
229 D (not E))
230 (mpuz-solve 10))
231 ((and E (not (eq C D)))
232 (mpuz-solve (if D 6 8)))
233 ((and A (not (eq B2 C)))
234 (mpuz-solve (if C 4 6) (if C 9)))
235 ((and A (not (eq B1 D)))
236 (mpuz-solve (if D 4 8) (if D 7)))
237 ((and (not A) (or (and B2 C) (and B1 D)))
238 (mpuz-solve 2)))))
239 (mpuz-paint-board)
240 (mapc (lambda (digit)
241 (and (not (mpuz-digit-solved-p digit)) ; unsolved
242 (setq squares (aref mpuz-board digit))
243 (if row
244 (if col
245 (member (cons row col) squares)
246 (assq row squares))
247 squares) ; and appearing in the puzzle!
248 (throw 'solved nil)))
249 [0 1 2 3 4 5 6 7 8 9]))
250 t))
251
252
253 ;; To build a puzzle, we take two random numbers and multiply them.
254 ;; We also take a random permutation for encryption.
255 ;; The random numbers are only use to see which digit appears in which square
256 ;; of the board. Everything is stored in individual squares.
257 ;;---------------------------------------------------------------------------
258 (defun mpuz-random-puzzle ()
259 "Draw random values to be multiplied in a puzzle."
260 (mpuz-build-random-perm)
261 (fillarray mpuz-board nil) ; erase the board
262 ;; A,B,C,D & E, are the five rows of our multiplication.
263 ;; Choose random values, discarding cases with leading zeros in C or D.
264 (let* ((A (+ 112 (random 888)))
265 (min (1+ (/ 1000 A)))
266 (B1 (+ min (random (- 10 min))))
267 B2 C D E)
268 (while (if (= B1 (setq B2 (+ min (random (- 10 min)))))
269 (not mpuz-allow-double-multiplicator)))
270 (setq C (* A B2)
271 D (* A B1)
272 E (+ C (* D 10)))
273 ;; Individual digits are now put on their respective squares.
274 ;; [NB: A square is a pair (row . column) of the screen.]
275 (mpuz-put-number-on-board A 2 9 7 5)
276 (mpuz-put-number-on-board (+ (* B1 10) B2) 4 9 7)
277 (mpuz-put-number-on-board C 6 9 7 5 3)
278 (mpuz-put-number-on-board D 8 7 5 3 1)
279 (mpuz-put-number-on-board E 10 9 7 5 3 1)))
280 \f
281 ;; Display
282 ;;--------
283 (defconst mpuz-framework
284 "
285 . . .
286 Number of errors (this game): 0
287 x . .
288 -------
289 . . . .
290 Number of completed games: 0
291 . . . .
292 --------- Average number of errors: 0.00
293 . . . . ."
294 "The general picture of the puzzle screen, as a string.")
295
296 (defun mpuz-create-buffer ()
297 "Create (or recreate) the puzzle buffer. Return it."
298 (let ((buf (get-buffer-create "*Mult Puzzle*"))
299 (face '(face mpuz-text))
300 buffer-read-only)
301 (save-excursion
302 (set-buffer buf)
303 (erase-buffer)
304 (insert mpuz-framework)
305 (set-text-properties 13 42 face)
306 (set-text-properties 79 105 face)
307 (set-text-properties 128 153 face)
308 (mpuz-paint-board)
309 (mpuz-paint-errors)
310 (mpuz-paint-statistics))
311 buf))
312
313 (defun mpuz-paint-number (n &optional eol words)
314 (end-of-line eol)
315 (let (buffer-read-only)
316 (delete-region (point)
317 (progn (backward-word (or words 1)) (point)))
318 (insert n)))
319
320 (defun mpuz-paint-errors ()
321 "Paint error count on the puzzle screen."
322 (mpuz-switch-to-window)
323 (goto-line 3)
324 (mpuz-paint-number (prin1-to-string mpuz-nb-errors)))
325
326 (defun mpuz-paint-statistics ()
327 "Paint statistics about previous games on the puzzle screen."
328 (goto-line 7)
329 (mpuz-paint-number (prin1-to-string mpuz-nb-completed-games))
330 (mpuz-paint-number
331 (format "%.2f"
332 (if (zerop mpuz-nb-completed-games)
333 0
334 (/ (+ 0.0 mpuz-nb-cumulated-errors)
335 mpuz-nb-completed-games)))
336 3 2))
337
338 (defun mpuz-paint-board ()
339 "Paint board situation on the puzzle screen."
340 (mpuz-switch-to-window)
341 (mapc 'mpuz-paint-digit [0 1 2 3 4 5 6 7 8 9])
342 (goto-char (point-min)))
343
344 (defun mpuz-paint-digit (digit)
345 "Paint all occurrences of DIGIT on the puzzle board."
346 (let ((char (if (mpuz-digit-solved-p digit)
347 (+ digit ?0)
348 (+ (mpuz-to-letter digit) ?A)))
349 (face `(face
350 ,(cond ((aref mpuz-trivial-digits digit) 'mpuz-trivial)
351 ((aref mpuz-found-digits digit) 'mpuz-solved)
352 ('mpuz-unsolved))))
353 buffer-read-only)
354 (mapc (lambda (square)
355 (goto-line (car square)) ; line before column!
356 (move-to-column (cdr square))
357 (insert char)
358 (set-text-properties (1- (point)) (point) face)
359 (delete-char 1))
360 (aref mpuz-board digit))))
361
362 (defun mpuz-get-buffer ()
363 "Get the puzzle buffer if it exists."
364 (get-buffer "*Mult Puzzle*"))
365
366 (defun mpuz-switch-to-window ()
367 "Find or create the Mult-Puzzle buffer, and display it."
368 (let ((buf (mpuz-get-buffer)))
369 (or buf (setq buf (mpuz-create-buffer)))
370 (switch-to-buffer buf)
371 (setq buffer-read-only t)
372 (mpuz-mode)))
373
374 \f
375 ;; Game control
376 ;;-------------
377 (defun mpuz-start-new-game ()
378 "Start a new puzzle."
379 (message "Here we go...")
380 (setq mpuz-nb-errors 0
381 mpuz-in-progress t)
382 (fillarray mpuz-found-digits nil) ; initialize mpuz-found-digits
383 (fillarray mpuz-trivial-digits nil)
384 (mpuz-random-puzzle)
385 (mpuz-switch-to-window)
386 (mpuz-paint-board)
387 (mpuz-paint-errors)
388 (mpuz-ask-for-try))
389
390 ;;;###autoload
391 (defun mpuz ()
392 "Multiplication puzzle with GNU Emacs."
393 ;; Main entry point
394 (interactive)
395 (mpuz-switch-to-window)
396 (if mpuz-in-progress
397 (mpuz-offer-abort)
398 (mpuz-start-new-game)))
399
400 (defun mpuz-offer-abort ()
401 "Ask if user wants to abort current puzzle."
402 (interactive)
403 (if (y-or-n-p "Abort game? ")
404 (let ((buf (mpuz-get-buffer)))
405 (message "Mult Puzzle aborted.")
406 (setq mpuz-in-progress nil
407 mpuz-nb-errors 0)
408 (fillarray mpuz-board nil)
409 (if buf (kill-buffer buf)))
410 (mpuz-ask-for-try)))
411
412 (defun mpuz-ask-for-try ()
413 "Ask for user proposal in puzzle."
414 (message "Your try?"))
415
416 (defun mpuz-ding (error)
417 "Dings, unless global variable `mpuz-silent' forbids it."
418 (cond ((eq mpuz-silent t))
419 ((not mpuz-silent) (ding t))
420 (error (ding t))))
421
422 (defun mpuz-try-letter ()
423 "Propose a digit for a letter in puzzle."
424 (interactive)
425 (if mpuz-in-progress
426 (let (letter-char digit digit-char message)
427 (setq letter-char (upcase last-command-char)
428 digit (mpuz-to-digit (- letter-char ?A)))
429 (cond ((mpuz-digit-solved-p digit)
430 (message "%c already solved." letter-char)
431 (mpuz-ding t))
432 ((null (aref mpuz-board digit))
433 (message "%c does not appear." letter-char)
434 (mpuz-ding t))
435 ((progn (message "%c = " letter-char)
436 ;; <char> has been entered.
437 ;; Print "<char> =" and
438 ;; read <num> or = <num>
439 (setq digit-char (read-char))
440 (if (eq digit-char ?=)
441 (setq digit-char (read-char)))
442 (or (> digit-char ?9) (< digit-char ?0))) ; bad input
443 (message "%c = %c" letter-char digit-char)
444 (mpuz-ding t))
445 (t
446 (mpuz-try-proposal letter-char digit-char))))
447 (if (y-or-n-p "Start a new game? ")
448 (mpuz-start-new-game)
449 (message "OK. I won't."))))
450
451 (defun mpuz-try-proposal (letter-char digit-char)
452 "Propose LETTER-CHAR as code for DIGIT-CHAR."
453 (let* ((letter (- letter-char ?A))
454 (digit (- digit-char ?0))
455 (correct-digit (mpuz-to-digit letter))
456 (game mpuz-nb-completed-games))
457 (cond ((mpuz-digit-solved-p correct-digit)
458 (message "%c has already been found." (+ correct-digit ?0)))
459 ((mpuz-digit-solved-p digit)
460 (message "%c has already been placed." digit-char))
461 ((= digit correct-digit)
462 (message "%c = %c correct!" letter-char digit-char)
463 (mpuz-ding nil)
464 (aset mpuz-found-digits digit t) ; Mark digit as solved
465 (and (mpuz-check-all-solved)
466 (mpuz-close-game)))
467 (t ;;; incorrect guess
468 (message "%c = %c incorrect!" letter-char digit-char)
469 (mpuz-ding t)
470 (setq mpuz-nb-errors (1+ mpuz-nb-errors))
471 (mpuz-paint-errors)))))
472
473 (defun mpuz-close-game ()
474 "Housecleaning when puzzle has been solved."
475 (setq mpuz-in-progress nil
476 mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
477 mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
478 (mpuz-paint-statistics)
479 (let ((message (format "Puzzle solved with %d error%s. That's %s"
480 mpuz-nb-errors
481 (if (= mpuz-nb-errors 1) "" "s")
482 (cond ((= mpuz-nb-errors 0) "perfect!")
483 ((= mpuz-nb-errors 1) "very good!")
484 ((= mpuz-nb-errors 2) "good.")
485 ((= mpuz-nb-errors 3) "not bad.")
486 ((= mpuz-nb-errors 4) "not too bad...")
487 ((< mpuz-nb-errors 10) "bad!")
488 ((< mpuz-nb-errors 15) "awful.")
489 (t "not serious.")))))
490 (message message)
491 (sit-for 4)
492 (if (y-or-n-p (concat message " Start a new game? "))
493 (mpuz-start-new-game)
494 (message "Good Bye!"))))
495
496 (defun mpuz-solve (&optional row col)
497 "Find solution for autosolving."
498 (mapc (lambda (digit)
499 (or (mpuz-digit-solved-p digit)
500 (if row
501 (not (if col
502 (member (cons row col) (aref mpuz-board digit))
503 (assq row (aref mpuz-board digit)))))
504 (aset mpuz-trivial-digits digit t)))
505 [0 1 2 3 4 5 6 7 8 9])
506 t)
507
508 (defun mpuz-show-solution (row)
509 "Display solution for debugging purposes."
510 (interactive "P")
511 (mpuz-switch-to-window)
512 (mpuz-solve (if row (* 2 (prefix-numeric-value row))))
513 (mpuz-paint-board)
514 (if (mpuz-check-all-solved)
515 (mpuz-close-game)))
516
517 (provide 'mpuz)
518
519 ;;; arch-tag: 2781d6ba-89e7-43b5-85c7-5d3a2e73feb1
520 ;;; mpuz.el ends here