Mercurial > emacs
annotate lisp/play/blackbox.el @ 93824:217fd7a2dc38
(mail-source-value):
Prefer fboundp to functionp so it works with macros as well.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 07 Apr 2008 16:03:31 +0000 |
parents | d83fb5ce6160 |
children | c70e45a7acfd 949bd6ad1ba4 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
473
diff
changeset
|
1 ;;; blackbox.el --- blackbox game in Emacs Lisp |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
473
diff
changeset
|
2 |
64701
34bd8e434dd7
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 2001, 2002, 2003, 2004, |
79716 | 4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
845 | 5 |
801
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
6 ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> |
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
7 ;; Adapted-By: ESR |
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
8 ;; Keywords: games |
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
9 |
473 | 10 ;; This file is part of GNU Emacs. |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 ;; it under the terms of the GNU General Public License as published by | |
78227
527f752989fc
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
473 | 15 ;; any later version. |
16 | |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
14169 | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
473 | 26 |
801
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
27 ;;; Commentary: |
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
28 |
14169 | 29 ;; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> |
30 ;; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89 | |
31 ;; interface improvements by ESR, Dec 5 1991. | |
473 | 32 |
14169 | 33 ;; The object of the game is to find four hidden balls by shooting rays |
34 ;; into the black box. There are four possibilities: 1) the ray will | |
35 ;; pass thru the box undisturbed, 2) it will hit a ball and be absorbed, | |
36 ;; 3) it will be deflected and exit the box, or 4) be deflected immediately, | |
37 ;; not even being allowed entry into the box. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
38 ;; |
14169 | 39 ;; The strange part is the method of deflection. It seems that rays will |
40 ;; not pass next to a ball, and change direction at right angles to avoid it. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
41 ;; |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
42 ;; R 3 |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
43 ;; 1 - - - - - - - - 1 |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
44 ;; - - - - - - - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
45 ;; - O - - - - - - 3 |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
46 ;; 2 - - - - O - O - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
47 ;; 4 - - - - - - - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
48 ;; 5 - - - - - - - - 5 |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
49 ;; - - - - - - - - R |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
50 ;; H - - - - - - - O |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
51 ;; 2 H 4 H |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
52 ;; |
14169 | 53 ;; Rays which enter and exit are numbered. You can see that rays 1 & 5 pass |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
54 ;; thru the box undisturbed. Ray 2 is deflected by the northwesternmost |
14169 | 55 ;; ball. Likewise rays 3 and 4. Rays which hit balls and are absorbed are |
56 ;; marked with H. The bottom of the left and the right of the bottom hit | |
57 ;; the southeastern ball directly. Rays may also hit balls after being | |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
58 ;; reflected. Consider the H on the bottom next to the 4. It bounces off |
14169 | 59 ;; the NW-ern most ball and hits the central ball. A ray shot from above |
60 ;; the right side 5 would hit the SE-ern most ball. The R beneath the 5 | |
61 ;; is because the ball is returned instantly. It is not allowed into | |
62 ;; the box if it would reflect immediately. The R on the top is a more | |
63 ;; leisurely return. Both central balls would tend to deflect it east | |
64 ;; or west, but it cannot go either way, so it just retreats. | |
65 ;; | |
66 ;; At the end of the game, if you've placed guesses for as many balls as | |
67 ;; there are in the box, the true board position will be revealed. Each | |
68 ;; `x' is an incorrect guess of yours; `o' is the true location of a ball. | |
473 | 69 |
801
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
70 ;;; Code: |
e9e34745ae3b
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
727
diff
changeset
|
71 |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
72 (defvar bb-board nil |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
73 "Blackbox board.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
74 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
75 (defvar bb-x -1 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
76 "Current x-position.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
77 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
78 (defvar bb-y -1 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
79 "Current y-position.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
80 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
81 (defvar bb-score 0 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
82 "Current score.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
83 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
84 (defvar bb-detour-count 0 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
85 "Number of detours.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
86 |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
87 (defvar bb-balls-placed nil |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
88 "List of already placed balls.") |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
89 |
66433
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
90 ;; This is used below to remap existing bindings for cursor motion to |
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
91 ;; blackbox-specific bindings in blackbox-mode-map. This is so that |
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
92 ;; users who prefer non-default key bindings for cursor motion don't |
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
93 ;; lose that when they play Blackbox. |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
94 (defun blackbox-redefine-key (map oldfun newfun) |
66433
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
95 "Redefine keys that run the function OLDFUN to run NEWFUN instead." |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
96 (define-key map (vector 'remap oldfun) newfun)) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
97 |
66433
4f54bc741533
(blackbox-redefine-key): New function.
Eli Zaretskii <eliz@gnu.org>
parents:
64701
diff
changeset
|
98 |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
99 (defvar blackbox-mode-map |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
100 (let ((map (make-keymap))) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
101 (suppress-keymap map t) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
102 (blackbox-redefine-key map 'backward-char 'bb-left) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
103 (blackbox-redefine-key map 'forward-char 'bb-right) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
104 (blackbox-redefine-key map 'previous-line 'bb-up) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
105 (blackbox-redefine-key map 'next-line 'bb-down) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
106 (blackbox-redefine-key map 'move-end-of-line 'bb-eol) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
107 (blackbox-redefine-key map 'move-beginning-of-line 'bb-bol) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
108 (define-key map " " 'bb-romp) |
87657
d83fb5ce6160
Tom Tromey <tromey at redhat.com>
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
109 (define-key map "q" 'bury-buffer) |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
110 (define-key map [insert] 'bb-romp) |
87657
d83fb5ce6160
Tom Tromey <tromey at redhat.com>
Glenn Morris <rgm@gnu.org>
parents:
87649
diff
changeset
|
111 (define-key map [return] 'bb-done) |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
112 (blackbox-redefine-key map 'newline 'bb-done) |
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
113 map)) |
473 | 114 |
115 ;; Blackbox mode is suitable only for specially formatted data. | |
116 (put 'blackbox-mode 'mode-class 'special) | |
117 | |
118 (defun blackbox-mode () | |
41668
29bd5069226c
(blackbox-mode, blackbox): Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
119 "Major mode for playing blackbox. |
29bd5069226c
(blackbox-mode, blackbox): Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
120 To learn how to play blackbox, see the documentation for function `blackbox'. |
473 | 121 |
122 The usual mnemonic keys move the cursor around the box. | |
123 \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively. | |
124 | |
125 \\[bb-romp] -- send in a ray from point, or toggle a ball at point | |
63239
f48178f79206
(blackbox-mode): Use run-mode-hooks.
Lute Kamstra <lute@gnu.org>
parents:
62249
diff
changeset
|
126 \\[bb-done] -- end game and get score" |
473 | 127 (interactive) |
128 (kill-all-local-variables) | |
129 (use-local-map blackbox-mode-map) | |
130 (setq truncate-lines t) | |
131 (setq major-mode 'blackbox-mode) | |
63239
f48178f79206
(blackbox-mode): Use run-mode-hooks.
Lute Kamstra <lute@gnu.org>
parents:
62249
diff
changeset
|
132 (setq mode-name "Blackbox") |
f48178f79206
(blackbox-mode): Use run-mode-hooks.
Lute Kamstra <lute@gnu.org>
parents:
62249
diff
changeset
|
133 (run-mode-hooks 'blackbox-mode-hook)) |
473 | 134 |
727 | 135 ;;;###autoload |
473 | 136 (defun blackbox (num) |
41668
29bd5069226c
(blackbox-mode, blackbox): Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
137 "Play blackbox. |
29bd5069226c
(blackbox-mode, blackbox): Doc fix.
Pavel Janík <Pavel@Janik.cz>
parents:
18383
diff
changeset
|
138 Optional prefix argument is the number of balls; the default is 4. |
473 | 139 |
140 What is blackbox? | |
141 | |
142 Blackbox is a game of hide and seek played on an 8 by 8 grid (the | |
143 Blackbox). Your opponent (Emacs, in this case) has hidden several | |
144 balls (usually 4) within this box. By shooting rays into the box and | |
145 observing where they emerge it is possible to deduce the positions of | |
146 the hidden balls. The fewer rays you use to find the balls, the lower | |
147 your score. | |
148 | |
149 Overview of play: | |
150 | |
826 | 151 \\<blackbox-mode-map>\ |
152 To play blackbox, type \\[blackbox]. An optional prefix argument | |
153 specifies the number of balls to be hidden in the box; the default is | |
154 four. | |
473 | 155 |
156 The cursor can be moved around the box with the standard cursor | |
157 movement keys. | |
158 | |
159 To shoot a ray, move the cursor to the edge of the box and press SPC. | |
160 The result will be determined and the playfield updated. | |
161 | |
162 You may place or remove balls in the box by moving the cursor into the | |
826 | 163 box and pressing \\[bb-romp]. |
473 | 164 |
165 When you think the configuration of balls you have placed is correct, | |
826 | 166 press \\[bb-done]. You will be informed whether you are correct or |
167 not, and be given your score. Your score is the number of letters and | |
168 numbers around the outside of the box plus five for each incorrectly | |
169 placed ball. If you placed any balls incorrectly, they will be | |
170 indicated with `x', and their actual positions indicated with `o'. | |
473 | 171 |
172 Details: | |
173 | |
174 There are three possible outcomes for each ray you send into the box: | |
175 | |
176 Detour: the ray is deflected and emerges somewhere other than | |
177 where you sent it in. On the playfield, detours are | |
178 denoted by matching pairs of numbers -- one where the | |
179 ray went in, and the other where it came out. | |
180 | |
181 Reflection: the ray is reflected and emerges in the same place | |
182 it was sent in. On the playfield, reflections are | |
183 denoted by the letter `R'. | |
184 | |
185 Hit: the ray strikes a ball directly and is absorbed. It does | |
186 not emerge from the box. On the playfield, hits are | |
187 denoted by the letter `H'. | |
188 | |
189 The rules for how balls deflect rays are simple and are best shown by | |
190 example. | |
191 | |
192 As a ray approaches a ball it is deflected ninety degrees. Rays can | |
193 be deflected multiple times. In the diagrams below, the dashes | |
194 represent empty box locations and the letter `O' represents a ball. | |
195 The entrance and exit points of each ray are marked with numbers as | |
196 described under \"Detour\" above. Note that the entrance and exit | |
197 points are always interchangeable. `*' denotes the path taken by the | |
198 ray. | |
199 | |
200 Note carefully the relative positions of the ball and the ninety | |
201 degree deflection it causes. | |
202 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
203 1 |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
204 - * - - - - - - - - - - - - - - - - - - - - - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
205 - * - - - - - - - - - - - - - - - - - - - - - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
206 1 * * - - - - - - - - - - - - - - - O - - - - O - |
473 | 207 - - O - - - - - - - O - - - - - - - * * * * - - |
208 - - - - - - - - - - - * * * * * 2 3 * * * - - * - - | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
209 - - - - - - - - - - - * - - - - - - - O - * - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
210 - - - - - - - - - - - * - - - - - - - - * * - - |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
211 - - - - - - - - - - - * - - - - - - - - * - O - |
473 | 212 2 3 |
213 | |
214 As mentioned above, a reflection occurs when a ray emerges from the same point | |
215 it was sent in. This can happen in several ways: | |
216 | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
217 |
473 | 218 - - - - - - - - - - - - - - - - - - - - - - - - |
219 - - - - O - - - - - O - O - - - - - - - - - - - | |
220 R * * * * - - - - - - - * - - - - O - - - - - - - | |
221 - - - - O - - - - - - * - - - - R - - - - - - - - | |
222 - - - - - - - - - - - * - - - - - - - - - - - - | |
223 - - - - - - - - - - - * - - - - - - - - - - - - | |
224 - - - - - - - - R * * * * - - - - - - - - - - - - | |
225 - - - - - - - - - - - - O - - - - - - - - - - - | |
226 | |
227 In the first example, the ray is deflected downwards by the upper | |
228 ball, then left by the lower ball, and finally retraces its path to | |
229 its point of origin. The second example is similar. The third | |
230 example is a bit anomalous but can be rationalized by realizing the | |
231 ray never gets a chance to get into the box. Alternatively, the ray | |
232 can be thought of as being deflected downwards and immediately | |
233 emerging from the box. | |
234 | |
235 A hit occurs when a ray runs straight into a ball: | |
236 | |
237 - - - - - - - - - - - - - - - - - - - - - - - - | |
238 - - - - - - - - - - - - - - - - - - - - O - - - | |
239 - - - - - - - - - - - - O - - - H * * * * - - - - | |
240 - - - - - - - - H * * * * O - - - - - - * - - - - | |
241 - - - - - - - - - - - - O - - - - - - O - - - - | |
242 H * * * O - - - - - - - - - - - - - - - - - - - - | |
243 - - - - - - - - - - - - - - - - - - - - - - - - | |
244 - - - - - - - - - - - - - - - - - - - - - - - - | |
245 | |
246 Be sure to compare the second example of a hit with the first example of | |
247 a reflection." | |
248 (interactive "P") | |
249 (switch-to-buffer "*Blackbox*") | |
250 (blackbox-mode) | |
251 (setq buffer-read-only t) | |
252 (buffer-disable-undo (current-buffer)) | |
253 (setq bb-board (bb-init-board (or num 4))) | |
254 (setq bb-balls-placed nil) | |
255 (setq bb-x -1) | |
256 (setq bb-y -1) | |
257 (setq bb-score 0) | |
258 (setq bb-detour-count 0) | |
259 (bb-insert-board) | |
260 (bb-goto (cons bb-x bb-y))) | |
261 | |
262 (defun bb-init-board (num-balls) | |
263 (random t) | |
264 (let (board pos) | |
265 (while (>= (setq num-balls (1- num-balls)) 0) | |
266 (while | |
267 (progn | |
268 (setq pos (cons (random 8) (random 8))) | |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
269 (member pos board))) |
473 | 270 (setq board (cons pos board))) |
271 board)) | |
272 | |
273 (defun bb-insert-board () | |
274 (let (i (buffer-read-only nil)) | |
275 (erase-buffer) | |
276 (insert " \n") | |
277 (setq i 8) | |
278 (while (>= (setq i (1- i)) 0) | |
279 (insert " - - - - - - - - \n")) | |
280 (insert " \n") | |
281 (insert (format "\nThere are %d balls in the box" (length bb-board))) | |
282 )) | |
283 | |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
284 (defun bb-right (count) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
285 (interactive "p") |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
286 (while (and (> count 0) (< bb-x 8)) |
473 | 287 (forward-char 2) |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
288 (setq bb-x (1+ bb-x)) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
289 (setq count (1- count)))) |
473 | 290 |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
291 (defun bb-left (count) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
292 (interactive "p") |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
293 (while (and (> count 0) (> bb-x -1)) |
473 | 294 (backward-char 2) |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
295 (setq bb-x (1- bb-x)) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
296 (setq count (1- count)))) |
473 | 297 |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
298 (defun bb-up (count) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
299 (interactive "p") |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
300 (while (and (> count 0) (> bb-y -1)) |
86903
c2cad5807c15
(bb-up, bb-down): Revert 2007-10-21 change and
Martin Rudalics <rudalics@gmx.at>
parents:
85500
diff
changeset
|
301 (with-no-warnings (previous-line)) |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
302 (setq bb-y (1- bb-y)) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
303 (setq count (1- count)))) |
473 | 304 |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
305 (defun bb-down (count) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
306 (interactive "p") |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
307 (while (and (> count 0) (< bb-y 8)) |
86903
c2cad5807c15
(bb-up, bb-down): Revert 2007-10-21 change and
Martin Rudalics <rudalics@gmx.at>
parents:
85500
diff
changeset
|
308 (with-no-warnings (next-line)) |
47562
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
309 (setq bb-y (1+ bb-y)) |
56e5df2ccc8d
(bb-right): Respect prefix argument.
Richard M. Stallman <rms@gnu.org>
parents:
42199
diff
changeset
|
310 (setq count (1- count)))) |
473 | 311 |
312 (defun bb-eol () | |
313 (interactive) | |
314 (setq bb-x 8) | |
315 (bb-goto (cons bb-x bb-y))) | |
316 | |
317 (defun bb-bol () | |
318 (interactive) | |
319 (setq bb-x -1) | |
320 (bb-goto (cons bb-x bb-y))) | |
321 | |
322 (defun bb-romp () | |
323 (interactive) | |
324 (cond | |
325 ((and | |
326 (or (= bb-x -1) (= bb-x 8)) | |
327 (or (= bb-y -1) (= bb-y 8)))) | |
328 ((bb-outside-box bb-x bb-y) | |
329 (bb-trace-ray bb-x bb-y)) | |
330 (t | |
331 (bb-place-ball bb-x bb-y)))) | |
332 | |
333 (defun bb-place-ball (x y) | |
334 (let ((coord (cons x y))) | |
335 (cond | |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
336 ((member coord bb-balls-placed) |
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
337 (setq bb-balls-placed (delete coord bb-balls-placed)) |
473 | 338 (bb-update-board "-")) |
339 (t | |
340 (setq bb-balls-placed (cons coord bb-balls-placed)) | |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
341 (bb-update-board (propertize "O" 'help-echo "Placed ball")))))) |
473 | 342 |
343 (defun bb-trace-ray (x y) | |
56989
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
344 (when (= (following-char) 32) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
345 (let ((result (bb-trace-ray-2 |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
346 t |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
347 x |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
348 (cond |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
349 ((= x -1) 1) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
350 ((= x 8) -1) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
351 (t 0)) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
352 y |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
353 (cond |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
354 ((= y -1) 1) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
355 ((= y 8) -1) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
356 (t 0))))) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
357 (cond |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
358 ((eq result 'hit) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
359 (bb-update-board (propertize "H" 'help-echo "Hit")) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
360 (setq bb-score (1+ bb-score))) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
361 ((equal result (cons x y)) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
362 (bb-update-board (propertize "R" 'help-echo "Reflection")) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
363 (setq bb-score (1+ bb-score))) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
364 (t |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
365 (setq bb-detour-count (1+ bb-detour-count)) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
366 (bb-update-board (propertize (format "%d" bb-detour-count) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
367 'help-echo "Detour")) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
368 (save-excursion |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
369 (bb-goto result) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
370 (bb-update-board (propertize (format "%d" bb-detour-count) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
371 'help-echo "Detour"))) |
7cbddcabb63e
(bb-trace-ray): Avoid double tracing.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
52401
diff
changeset
|
372 (setq bb-score (+ bb-score 2))))))) |
473 | 373 |
374 (defun bb-trace-ray-2 (first x dx y dy) | |
375 (cond | |
376 ((and (not first) | |
377 (bb-outside-box x y)) | |
378 (cons x y)) | |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
379 ((member (cons (+ x dx) (+ y dy)) bb-board) |
473 | 380 'hit) |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
381 ((member (cons (+ x dx dy) (+ y dy dx)) bb-board) |
473 | 382 (bb-trace-ray-2 nil x (- dy) y (- dx))) |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
383 ((member (cons (+ x dx (- dy)) (+ y dy (- dx))) bb-board) |
473 | 384 (bb-trace-ray-2 nil x dy y dx)) |
385 (t | |
386 (bb-trace-ray-2 nil (+ x dx) dx (+ y dy) dy)))) | |
387 | |
388 (defun bb-done () | |
389 "Finish the game and report score." | |
390 (interactive) | |
391 (let (bogus-balls) | |
392 (cond | |
393 ((not (= (length bb-balls-placed) (length bb-board))) | |
394 (message "There %s %d hidden ball%s; you have placed %d." | |
395 (if (= (length bb-board) 1) "is" "are") | |
396 (length bb-board) | |
397 (if (= (length bb-board) 1) "" "s") | |
398 (length bb-balls-placed))) | |
399 (t | |
400 (setq bogus-balls (bb-show-bogus-balls bb-balls-placed bb-board)) | |
401 (if (= bogus-balls 0) | |
402 (message "Right! Your score is %d." bb-score) | |
403 (message "Oops! You missed %d ball%s. Your score is %d." | |
404 bogus-balls | |
405 (if (= bogus-balls 1) "" "s") | |
406 (+ bb-score (* 5 bogus-balls)))) | |
407 (bb-goto '(-1 . -1)))))) | |
408 | |
409 (defun bb-show-bogus-balls (balls-placed board) | |
410 (bb-show-bogus-balls-2 balls-placed board "x") | |
411 (bb-show-bogus-balls-2 board balls-placed "o")) | |
412 | |
413 (defun bb-show-bogus-balls-2 (list-1 list-2 c) | |
414 (cond | |
415 ((null list-1) | |
416 0) | |
42199
6f3215f24a28
(bb-member): Remove, use member instead.
Pavel Janík <Pavel@Janik.cz>
parents:
41668
diff
changeset
|
417 ((member (car list-1) list-2) |
473 | 418 (bb-show-bogus-balls-2 (cdr list-1) list-2 c)) |
419 (t | |
420 (bb-goto (car list-1)) | |
421 (bb-update-board c) | |
422 (1+ (bb-show-bogus-balls-2 (cdr list-1) list-2 c))))) | |
423 | |
826 | 424 (defun bb-outside-box (x y) |
425 (or (= x -1) (= x 8) (= y -1) (= y 8))) | |
473 | 426 |
427 (defun bb-goto (pos) | |
428 (goto-char (+ (* (car pos) 2) (* (cdr pos) 22) 26))) | |
429 | |
430 (defun bb-update-board (c) | |
431 (let ((buffer-read-only nil)) | |
432 (backward-char (1- (length c))) | |
433 (delete-char (length c)) | |
434 (insert c) | |
435 (backward-char 1))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
47562
diff
changeset
|
436 |
18383 | 437 (provide 'blackbox) |
438 | |
66440
5a126d3f6688
(blackbox-mode-map): Move init into declaration.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
66433
diff
changeset
|
439 ;; arch-tag: 6c474c62-5617-4b10-9b44-ac430168c0e2 |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
473
diff
changeset
|
440 ;;; blackbox.el ends here |