annotate lisp/play/mpuz.el @ 75227:e90d04cd455a

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