Mercurial > emacs
annotate lisp/play/mpuz.el @ 2376:f7a3795bd1c5
(fill-paragraph, justify-current-line) Now uses the skip-syntax-
forward and backward characters and char-syntax to be smart about
filling syntaxes other than text.
This change was inspired by Richard Caley's fill-para package from LCD,
but the implementation is original.
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Sat, 27 Mar 1993 01:58:16 +0000 |
parents | f287613dfc28 |
children | a149f1464f40 |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
232
diff
changeset
|
1 ;;; mpuz.el --- multiplication puzzle for GNU Emacs |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
232
diff
changeset
|
2 |
838 | 3 ;;; Copyright (C) 1990 Free Software Foundation, Inc. |
4 | |
791
203c23c9f22c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
5 ;; Author: Philippe Schnoebelen <phs@lifia.imag.fr> |
203c23c9f22c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
6 ;; Keywords: games |
203c23c9f22c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
7 |
142 | 8 ;; This file is part of GNU Emacs. |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
2308
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
25 ;;; Commentary: |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
26 |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
27 ;; When this package is loaded, `M-x mpuz' generates a random multiplication |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
28 ;; puzzle. This is a multiplication example in which each digit has been |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
29 ;; consistently replaced with some letter. Your job is to reconstruct |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
30 ;; the original digits. Type `?' while the mode is active for detailed help. |
f287613dfc28
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1217
diff
changeset
|
31 |
791
203c23c9f22c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
32 ;;; Code: |
203c23c9f22c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
659
diff
changeset
|
33 |
142 | 34 (random t) ; randomize |
35 | |
36 (defun mpuz-random (n) | |
37 "Return a random integer between 0 and N - 1 inclusive." | |
38 (setq n (% (random) n)) | |
39 (if (< n 0) (- n) n)) | |
40 | |
41 (defvar mpuz-silent nil | |
42 "*Set this to T if you don't want dings on inputs.") | |
43 | |
44 (defun mpuz-ding () | |
232 | 45 "Dings, unless global variable `mpuz-silent' forbids it." |
142 | 46 (or mpuz-silent (ding t))) |
47 | |
48 | |
49 ;; Mpuz mode and keymaps | |
50 ;;---------------------- | |
51 (defvar mpuz-mode-hook nil) | |
52 | |
53 (defvar mpuz-mode-map nil | |
54 "Local keymap to use in Mult Puzzle.") | |
55 | |
56 (defvar mpuz-read-map nil | |
57 "Local keymap to use (sometimes) in Mult Puzzle.") | |
58 | |
59 (if mpuz-mode-map nil | |
60 (setq mpuz-mode-map (make-sparse-keymap)) | |
61 (define-key mpuz-mode-map "a" 'mpuz-try-letter) | |
62 (define-key mpuz-mode-map "b" 'mpuz-try-letter) | |
63 (define-key mpuz-mode-map "c" 'mpuz-try-letter) | |
64 (define-key mpuz-mode-map "d" 'mpuz-try-letter) | |
65 (define-key mpuz-mode-map "e" 'mpuz-try-letter) | |
66 (define-key mpuz-mode-map "f" 'mpuz-try-letter) | |
67 (define-key mpuz-mode-map "g" 'mpuz-try-letter) | |
68 (define-key mpuz-mode-map "h" 'mpuz-try-letter) | |
69 (define-key mpuz-mode-map "i" 'mpuz-try-letter) | |
70 (define-key mpuz-mode-map "j" 'mpuz-try-letter) | |
71 (define-key mpuz-mode-map "A" 'mpuz-try-letter) | |
72 (define-key mpuz-mode-map "B" 'mpuz-try-letter) | |
73 (define-key mpuz-mode-map "C" 'mpuz-try-letter) | |
74 (define-key mpuz-mode-map "D" 'mpuz-try-letter) | |
75 (define-key mpuz-mode-map "E" 'mpuz-try-letter) | |
76 (define-key mpuz-mode-map "F" 'mpuz-try-letter) | |
77 (define-key mpuz-mode-map "G" 'mpuz-try-letter) | |
78 (define-key mpuz-mode-map "H" 'mpuz-try-letter) | |
79 (define-key mpuz-mode-map "I" 'mpuz-try-letter) | |
80 (define-key mpuz-mode-map "J" 'mpuz-try-letter) | |
81 (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort) | |
82 (define-key mpuz-mode-map "?" 'describe-mode)) | |
83 | |
84 (if mpuz-read-map nil | |
85 (setq mpuz-read-map (make-keymap)) | |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
86 (let ((i 0)) |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
87 (while (< i (length mpuz-read-map)) |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
88 (define-key mpuz-read-map (char-to-string i) 'exit-minibuffer) |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
89 (setq i (1+ i))))) |
142 | 90 |
91 (defun mpuz-mode () | |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
92 "Multiplication puzzle mode. |
142 | 93 |
232 | 94 You have to guess which letters stand for which digits in the |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
95 multiplication displayed inside the `*Mult Puzzle*' buffer. |
142 | 96 |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
97 You may enter a guess for a letter's value by typing first the letter, |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
98 then the digit. Thus, to guess that A=3, type A 3. |
142 | 99 |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
100 To leave the game to do other editing work, just switch buffers. |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
101 Then you may resume the game with M-x mpuz. |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
102 You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]." |
142 | 103 (interactive) |
104 (setq major-mode 'mpuz-mode | |
105 mode-name "Mult Puzzle") | |
106 (use-local-map mpuz-mode-map) | |
107 (run-hooks 'mpuz-mode-hook)) | |
108 | |
109 | |
110 ;; Some variables for statistics | |
111 ;;------------------------------ | |
112 (defvar mpuz-nb-errors 0 | |
232 | 113 "Number of errors made in current game.") |
142 | 114 |
115 (defvar mpuz-nb-completed-games 0 | |
232 | 116 "Number of games completed.") |
142 | 117 |
118 (defvar mpuz-nb-cumulated-errors 0 | |
119 "Number of errors made in previous games.") | |
120 | |
121 | |
122 ;; Some variables for game tracking | |
123 ;;--------------------------------- | |
124 (defvar mpuz-in-progress nil | |
125 "True if a game is currently in progress.") | |
126 | |
127 (defvar mpuz-found-digits (make-vector 10 nil) | |
128 "A vector recording which digits have been decrypted.") | |
129 | |
130 (defmacro mpuz-digit-solved-p (digit) | |
131 (list 'aref 'mpuz-found-digits digit)) | |
132 | |
133 | |
134 ;; A puzzle uses a permutation of [0..9] into itself. | |
135 ;; We use both the permutation and its inverse. | |
136 ;;--------------------------------------------------- | |
137 (defvar mpuz-digit-to-letter (make-vector 10 0) | |
138 "A permutation from [0..9] to [0..9].") | |
139 | |
140 (defvar mpuz-letter-to-digit (make-vector 10 0) | |
141 "The inverse of mpuz-digit-to-letter.") | |
142 | |
143 (defmacro mpuz-to-digit (letter) | |
144 (list 'aref 'mpuz-letter-to-digit letter)) | |
145 | |
146 (defmacro mpuz-to-letter (digit) | |
147 (list 'aref 'mpuz-digit-to-letter digit)) | |
148 | |
149 (defun mpuz-build-random-perm () | |
150 "Initialize puzzle coding with a random permutation." | |
151 (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq | |
152 (index 10) | |
153 elem) | |
154 (while letters | |
155 (setq elem (nth (mpuz-random index) letters) | |
156 letters (delq elem letters) | |
157 index (1- index)) | |
158 (aset mpuz-digit-to-letter index elem) | |
159 (aset mpuz-letter-to-digit elem index)))) | |
160 | |
161 | |
162 ;; A puzzle also uses a board displaying a mulplication. | |
163 ;; Every digit appears in the board, crypted or not. | |
164 ;;------------------------------------------------------ | |
165 (defvar mpuz-board (make-vector 10 nil) | |
166 "The board associates ot any digit the list of squares where it appears.") | |
167 | |
168 (defun mpuz-put-digit-on-board (number square) | |
169 "Put (last digit of) NUMBER on SQUARE of the puzzle board." | |
170 ;; i.e. push SQUARE on NUMBER square-list | |
171 (setq number (% number 10)) | |
172 (aset mpuz-board number (cons square (aref mpuz-board number)))) | |
173 | |
174 (defun mpuz-check-all-solved () | |
175 "Check whether all digits have been solved. Return t if yes." | |
176 (catch 'found | |
177 (let ((digit -1)) | |
178 (while (> 10 (setq digit (1+ digit))) | |
179 (if (and (not (mpuz-digit-solved-p digit)) ; unsolved | |
180 (aref mpuz-board digit)) ; and appearing in the puzzle ! | |
181 (throw 'found nil)))) | |
182 t)) | |
183 | |
184 | |
185 ;; To build a puzzle, we take two random numbers and multiply them. | |
186 ;; We also take a random permutation for encryption. | |
187 ;; The random numbers are only use to see which digit appears in which square | |
188 ;; of the board. Everything is stored in individual squares. | |
189 ;;--------------------------------------------------------------------------- | |
190 (defun mpuz-random-puzzle () | |
191 "Draw random values to be multiplied in a puzzle." | |
192 (mpuz-build-random-perm) | |
193 (fillarray mpuz-board nil) ; erase the board | |
194 (let (A B C D E) | |
195 ;; A,B,C,D & E, are the five rows of our multiplication. | |
196 ;; Choose random values, discarding uninteresting cases. | |
197 (while (progn | |
198 (setq A (mpuz-random 1000) | |
199 B (mpuz-random 100) | |
200 C (* A (% B 10)) | |
201 D (* A (/ B 10)) | |
202 E (* A B)) | |
203 (or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D | |
204 ;; Individual digits are now put on their respectives squares. | |
205 ;; [NB: A square is a pair <row,column> of the screen.] | |
206 (mpuz-put-digit-on-board A '(2 . 9)) | |
207 (mpuz-put-digit-on-board (/ A 10) '(2 . 7)) | |
208 (mpuz-put-digit-on-board (/ A 100) '(2 . 5)) | |
209 (mpuz-put-digit-on-board B '(4 . 9)) | |
210 (mpuz-put-digit-on-board (/ B 10) '(4 . 7)) | |
211 (mpuz-put-digit-on-board C '(6 . 9)) | |
212 (mpuz-put-digit-on-board (/ C 10) '(6 . 7)) | |
213 (mpuz-put-digit-on-board (/ C 100) '(6 . 5)) | |
214 (mpuz-put-digit-on-board (/ C 1000) '(6 . 3)) | |
215 (mpuz-put-digit-on-board D '(8 . 7)) | |
216 (mpuz-put-digit-on-board (/ D 10) '(8 . 5)) | |
217 (mpuz-put-digit-on-board (/ D 100) '(8 . 3)) | |
218 (mpuz-put-digit-on-board (/ D 1000) '(8 . 1)) | |
219 (mpuz-put-digit-on-board E '(10 . 9)) | |
220 (mpuz-put-digit-on-board (/ E 10) '(10 . 7)) | |
221 (mpuz-put-digit-on-board (/ E 100) '(10 . 5)) | |
222 (mpuz-put-digit-on-board (/ E 1000) '(10 . 3)) | |
223 (mpuz-put-digit-on-board (/ E 10000) '(10 . 1)))) | |
224 | |
225 ;; Display | |
226 ;;-------- | |
227 (defconst mpuz-framework | |
228 " | |
229 . . . | |
230 Number of errors (this game): 0 | |
231 x . . | |
232 ------- | |
233 . . . . | |
234 Number of completed games: 0 | |
235 . . . . | |
236 --------- Average number of errors: 0.00 | |
237 . . . . ." | |
238 "The general picture of the puzzle screen, as a string.") | |
239 | |
240 (defun mpuz-create-buffer () | |
241 "Create (or recreate) the puzzle buffer. Return it." | |
242 (let ((buff (get-buffer-create "*Mult Puzzle*"))) | |
243 (save-excursion | |
244 (set-buffer buff) | |
245 (let ((buffer-read-only nil)) | |
246 (erase-buffer) | |
247 (insert mpuz-framework) | |
248 (mpuz-paint-board) | |
249 (mpuz-paint-errors) | |
250 (mpuz-paint-statistics))) | |
251 buff)) | |
252 | |
253 (defun mpuz-paint-errors () | |
254 "Paint error count on the puzzle screen." | |
255 (mpuz-switch-to-window) | |
256 (let ((buffer-read-only nil)) | |
257 (goto-line 3) | |
258 (move-to-column 49) | |
259 (mpuz-delete-line) | |
260 (insert (prin1-to-string mpuz-nb-errors)))) | |
261 | |
262 (defun mpuz-paint-statistics () | |
263 "Paint statistics about previous games on the puzzle screen." | |
264 (let* ((mean (if (zerop mpuz-nb-completed-games) 0 | |
265 (/ (+ mpuz-nb-completed-games (* 200 mpuz-nb-cumulated-errors)) | |
266 (* 2 mpuz-nb-completed-games)))) | |
267 (frac-part (% mean 100))) | |
268 (let ((buffer-read-only nil)) | |
269 (goto-line 7) | |
270 (move-to-column 51) | |
271 (mpuz-delete-line) | |
272 (insert (prin1-to-string mpuz-nb-completed-games)) | |
273 (goto-line 9) | |
274 (move-to-column 50) | |
275 (mpuz-delete-line) | |
276 (insert (format "%d.%d%d" (/ mean 100) (/ frac-part 10) (% frac-part 10)))))) | |
277 | |
278 (defun mpuz-paint-board () | |
279 "Paint board situation on the puzzle screen." | |
280 (mpuz-switch-to-window) | |
281 (let ((letter -1)) | |
282 (while (> 10 (setq letter (1+ letter))) | |
283 (mpuz-paint-digit (mpuz-to-digit letter)))) | |
284 (goto-char (point-min))) | |
285 | |
286 (defun mpuz-paint-digit (digit) | |
287 "Paint all occurrences of DIGIT on the puzzle board." | |
288 ;; (mpuz-switch-to-window) | |
289 (let ((char (if (mpuz-digit-solved-p digit) | |
290 (+ digit ?0) | |
291 (+ (mpuz-to-letter digit) ?A))) | |
292 (square-l (aref mpuz-board digit))) | |
293 (let ((buffer-read-only nil)) | |
294 (while square-l | |
295 (goto-line (car (car square-l))) ; line before column ! | |
296 (move-to-column (cdr (car square-l))) | |
297 (insert char) | |
298 (delete-char 1) | |
299 (backward-char 1) | |
300 (setq square-l (cdr square-l)))))) | |
301 | |
302 (defun mpuz-delete-line () | |
303 "Clear from point to next newline." ; & put nothing in the kill ring | |
304 (while (not (= ?\n (char-after (point)))) | |
305 (delete-char 1))) | |
306 | |
307 (defun mpuz-get-buffer () | |
308 "Get the puzzle buffer if it exists." | |
309 (get-buffer "*Mult Puzzle*")) | |
310 | |
311 (defun mpuz-switch-to-window () | |
312 "Find or create the Mult-Puzzle buffer, and display it." | |
313 (let ((buff (mpuz-get-buffer))) | |
314 (or buff (setq buff (mpuz-create-buffer))) | |
315 (switch-to-buffer buff) | |
316 (or buffer-read-only (toggle-read-only)) | |
317 (mpuz-mode))) | |
318 | |
319 | |
320 ;; Game control | |
321 ;;------------- | |
322 (defun mpuz-abort-game () | |
323 "Abort any puzzle in progess." | |
324 (message "Mult Puzzle aborted.") | |
325 (setq mpuz-in-progress nil | |
326 mpuz-nb-errors 0) | |
327 (fillarray mpuz-board nil) | |
328 (let ((buff (mpuz-get-buffer))) | |
329 (if buff (kill-buffer buff)))) | |
330 | |
331 (defun mpuz-start-new-game () | |
332 "Start a new puzzle." | |
333 (message "Here we go...") | |
334 (setq mpuz-nb-errors 0 | |
335 mpuz-in-progress t) | |
336 (fillarray mpuz-found-digits nil) ; initialize mpuz-found-digits | |
337 (mpuz-random-puzzle) | |
338 (mpuz-switch-to-window) | |
339 (mpuz-paint-board) | |
340 (mpuz-paint-errors) | |
341 (mpuz-ask-for-try)) | |
342 | |
343 (defun mpuz-offer-new-game () | |
344 "Ask if user wants to start a new puzzle." | |
345 (if (y-or-n-p "Start a new game ") | |
346 (mpuz-start-new-game) | |
347 (message "OK. I won't."))) | |
348 | |
1217
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
349 ;;;###autoload |
d0b19afef0ae
Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents:
838
diff
changeset
|
350 (defun mpuz () |
142 | 351 "Multiplication puzzle with GNU Emacs." |
352 ;; Main entry point | |
353 (interactive) | |
354 (mpuz-switch-to-window) | |
355 (if mpuz-in-progress | |
356 (mpuz-offer-abort) | |
357 (mpuz-start-new-game))) | |
358 | |
359 (defun mpuz-offer-abort () | |
360 "Ask if user wants to abort current puzzle." | |
361 (interactive) | |
362 (if (y-or-n-p "Abort game ") | |
363 (mpuz-abort-game) | |
364 (mpuz-ask-for-try))) | |
365 | |
366 (defun mpuz-ask-for-try () | |
367 "Ask for user proposal in puzzle." | |
368 (message "Your try ?")) | |
369 | |
370 (defun mpuz-try-letter () | |
371 "Propose a digit for a letter in puzzle." | |
372 (interactive) | |
373 (if mpuz-in-progress | |
374 (let (letter-char digit digit-char message) | |
375 (setq letter-char (if (or (< last-command-char ?a) | |
376 (> last-command-char ?z)) | |
377 last-command-char | |
378 (- last-command-char 32)) | |
379 digit (mpuz-to-digit (- letter-char ?A))) | |
380 (cond ((mpuz-digit-solved-p digit) | |
381 (message "%c already solved." letter-char)) | |
382 ((null (aref mpuz-board digit)) | |
383 (message "%c does not appear." letter-char)) | |
384 ((progn (setq message (format "%c = " letter-char)) | |
385 ;; <char> has been entered. | |
386 ;; Print "<char> =" and | |
387 ;; read <num> or = <num> | |
388 (read-from-minibuffer message nil mpuz-read-map) | |
389 (if (= last-input-char ?\=) | |
390 (read-from-minibuffer message nil mpuz-read-map)) | |
391 (setq digit-char last-input-char) | |
392 (message "%c = %c" letter-char digit-char) | |
393 (or (> digit-char ?9) (< digit-char ?0))) ; bad input | |
394 (ding t)) | |
395 (t | |
396 (mpuz-try-proposal letter-char digit-char)))) | |
397 (mpuz-offer-new-game))) | |
398 | |
399 (defun mpuz-try-proposal (letter-char digit-char) | |
400 "Propose LETTER-CHAR as code for DIGIT-CHAR." | |
401 (let* ((letter (- letter-char ?A)) | |
402 (digit (- digit-char ?0)) | |
403 (correct-digit (mpuz-to-digit letter))) | |
404 (cond ((mpuz-digit-solved-p correct-digit) | |
405 (message "%c has already been found.")) | |
406 ((= digit correct-digit) | |
407 (message "%c = %c correct !" letter-char digit-char) | |
408 (mpuz-ding) | |
409 (mpuz-correct-guess digit)) | |
410 (t ;;; incorrect guess | |
411 (message "%c = %c incorrect !" letter-char digit-char) | |
412 (mpuz-ding) | |
413 (setq mpuz-nb-errors (1+ mpuz-nb-errors)) | |
414 (mpuz-paint-errors))))) | |
415 | |
416 (defun mpuz-correct-guess (digit) | |
417 "Handle correct guessing of DIGIT." | |
418 (aset mpuz-found-digits digit t) ; Mark digit as solved | |
419 (mpuz-paint-digit digit) ; Repaint it (now as a digit) | |
420 (if (mpuz-check-all-solved) | |
421 (mpuz-close-game))) | |
422 | |
423 (defun mpuz-close-game () | |
424 "Housecleaning when puzzle has been solved." | |
425 (setq mpuz-in-progress nil | |
426 mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors) | |
427 mpuz-nb-completed-games (1+ mpuz-nb-completed-games)) | |
428 (mpuz-paint-statistics) | |
429 (let ((message (mpuz-congratulate))) | |
430 (message message) | |
431 (sit-for 4) | |
432 (if (y-or-n-p (concat message " Start a new game ")) | |
433 (mpuz-start-new-game) | |
434 (message "Good Bye !")))) | |
435 | |
436 (defun mpuz-congratulate () | |
437 "Build a congratulation message when puzzle is solved." | |
438 (format "Puzzle solved with %d errors. %s" | |
439 mpuz-nb-errors | |
440 (cond ((= mpuz-nb-errors 0) "That's perfect !") | |
441 ((= mpuz-nb-errors 1) "That's very good !") | |
442 ((= mpuz-nb-errors 2) "That's good.") | |
443 ((= mpuz-nb-errors 3) "That's not bad.") | |
444 ((= mpuz-nb-errors 4) "That's not too bad...") | |
445 ((and (>= mpuz-nb-errors 5) | |
446 (< mpuz-nb-errors 10)) "That's bad !") | |
447 ((and (>= mpuz-nb-errors 10) | |
448 (< mpuz-nb-errors 15)) "That's awful.") | |
449 ((>= mpuz-nb-errors 15) "That's not serious.")))) | |
450 | |
451 (defun mpuz-show-solution () | |
452 "Display solution for debugging purposes." | |
453 (interactive) | |
454 (mpuz-switch-to-window) | |
455 (let (digit list) | |
456 (setq digit -1) | |
457 (while (> 10 (setq digit (1+ digit))) | |
458 (or (mpuz-digit-solved-p digit) | |
459 (setq list (cons digit list)))) | |
460 (mapcar 'mpuz-correct-guess list))) | |
461 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
232
diff
changeset
|
462 ;;; mpuz.el ends here |