annotate lisp/play/blackbox.el @ 22088:39f7c265d3a2

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