annotate lisp/games/mpuz.el @ 90:99da576a67e7 xemacs-20-0

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