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