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