0
|
1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
|
|
2
|
|
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
|
|
4
|
4
|
5 ;; Author: Kyle Jones <kyle@uunet.uu.net>
|
0
|
6 ;; Keywords: games
|
|
7
|
|
8 ;; This file is part of XEmacs.
|
|
9
|
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
|
19
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
4
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
0
|
24
|
4
|
25 ;;; Synched up with: FSF 19.34.
|
0
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; A demonstrator for John Horton Conway's "Life" cellular automaton
|
|
30 ;; in Emacs Lisp. Picks a random one of a set of interesting Life
|
|
31 ;; patterns and evolves it according to the familiar rules.
|
|
32
|
|
33 ;;; Code:
|
|
34
|
|
35 (defconst life-patterns
|
|
36 [("@@@" " @@" "@@@")
|
|
37 ("@@@ @@@" "@@ @@ " "@@@ @@@")
|
|
38 ("@@@ @@@" "@@ @@" "@@@ @@@")
|
|
39 ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
|
|
40 ("@@@@@@@@@@")
|
|
41 (" @@@@@@@@@@ "
|
|
42 " @@@@@@@@@@ "
|
|
43 " @@@@@@@@@@ "
|
|
44 "@@@@@@@@@@ "
|
|
45 "@@@@@@@@@@ ")
|
|
46 ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@")
|
|
47 ("@ @" "@ @" "@ @"
|
|
48 "@ @" "@ @" "@ @"
|
|
49 "@ @" "@ @" "@ @"
|
|
50 "@ @" "@ @" "@ @"
|
|
51 "@ @" "@ @" "@ @")
|
|
52 ("@@ " " @@ " " @@ "
|
|
53 " @@ " " @@ " " @@ "
|
|
54 " @@ " " @@ " " @@ "
|
|
55 " @@ " " @@ " " @@ "
|
|
56 " @@ " " @@ " " @@ "
|
|
57 " @@")
|
|
58 ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@"
|
|
59 "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")]
|
|
60 "Vector of rectangles containing some Life startup patterns.")
|
|
61
|
|
62 ;; Macros are used macros for manifest constants instead of variables
|
|
63 ;; because the compiler will convert them to constants, which should
|
|
64 ;; eval faster than symbols.
|
|
65 ;;
|
|
66 ;; Don't change any of the life-* macro constants unless you thoroughly
|
|
67 ;; understand the `life-grim-reaper' function.
|
|
68
|
|
69 (defmacro life-life-char () ?@)
|
|
70 (defmacro life-death-char () (1+ (life-life-char)))
|
|
71 (defmacro life-birth-char () 3)
|
|
72 (defmacro life-void-char () ?\ )
|
|
73
|
|
74 (defmacro life-life-string () (char-to-string (life-life-char)))
|
|
75 (defmacro life-death-string () (char-to-string (life-death-char)))
|
|
76 (defmacro life-birth-string () (char-to-string (life-birth-char)))
|
|
77 (defmacro life-void-string () (char-to-string (life-void-char)))
|
|
78 (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]"))
|
|
79
|
|
80 (defmacro life-increment (variable) (list 'setq variable (list '1+ variable)))
|
|
81
|
|
82
|
|
83 ;; list of numbers that tell how many characters to move to get to
|
|
84 ;; each of a cell's eight neighbors.
|
|
85 (defconst life-neighbor-deltas nil)
|
|
86
|
|
87 ;; window display always starts here. Easier to deal with than
|
|
88 ;; (scroll-up) and (scroll-down) when trying to center the display.
|
|
89 (defconst life-window-start nil)
|
|
90
|
|
91 ;; For mode line
|
|
92 (defconst life-current-generation nil)
|
|
93 ;; Sadly, mode-line-format won't display numbers.
|
|
94 (defconst life-generation-string nil)
|
|
95
|
|
96 (defvar life-initialized nil
|
|
97 "Non-nil if `life' has been run at least once.")
|
|
98
|
|
99 ;;;###autoload
|
|
100 (defun life (&optional sleeptime)
|
|
101 "Run Conway's Life simulation.
|
|
102 The starting pattern is randomly selected. Prefix arg (optional first
|
|
103 arg non-nil from a program) is the number of seconds to sleep between
|
|
104 generations (this defaults to 1)."
|
|
105 (interactive "p")
|
|
106 (or life-initialized
|
|
107 (random t))
|
|
108 (setq life-initialized t)
|
|
109 (or sleeptime (setq sleeptime 1))
|
|
110 (life-setup)
|
|
111 (life-display-generation sleeptime)
|
|
112 (catch 'life-exit
|
|
113 (while t
|
|
114 (let ((inhibit-quit t))
|
|
115 (life-grim-reaper)
|
|
116 (life-expand-plane-if-needed)
|
|
117 (life-increment-generation)
|
|
118 (life-display-generation sleeptime)))))
|
|
119
|
|
120 (defalias 'life-mode 'life)
|
|
121 (put 'life-mode 'mode-class 'special)
|
|
122
|
|
123 (defun life-setup ()
|
|
124 (let (n)
|
|
125 (switch-to-buffer (get-buffer-create "*Life*") t)
|
|
126 (erase-buffer)
|
|
127 (kill-all-local-variables)
|
|
128 ;; XEmacs change:
|
|
129 (set-specifier scrollbar-height 0 (current-buffer))
|
|
130 (set-specifier scrollbar-width 0 (current-buffer))
|
|
131 (setq case-fold-search nil
|
|
132 mode-name "Life"
|
|
133 major-mode 'life-mode
|
|
134 truncate-lines t
|
|
135 life-current-generation 0
|
|
136 life-generation-string "0"
|
|
137 mode-line-buffer-identification '("Life: generation "
|
|
138 life-generation-string)
|
|
139 fill-column (1- (window-width))
|
|
140 life-window-start 1)
|
|
141 (buffer-disable-undo (current-buffer))
|
|
142 ;; stuff in the random pattern
|
|
143 (life-insert-random-pattern)
|
|
144 ;; make sure (life-life-char) is used throughout
|
|
145 (goto-char (point-min))
|
|
146 (while (re-search-forward (life-not-void-regexp) nil t)
|
|
147 (replace-match (life-life-string) t t))
|
|
148 ;; center the pattern horizontally
|
|
149 (goto-char (point-min))
|
|
150 (setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2))
|
|
151 (while (not (eobp))
|
|
152 (indent-to n)
|
|
153 (forward-line))
|
|
154 ;; center the pattern vertically
|
|
155 (setq n (/ (- (1- (window-height))
|
|
156 (count-lines (point-min) (point-max)))
|
|
157 2))
|
|
158 (goto-char (point-min))
|
|
159 (newline n)
|
|
160 (goto-char (point-max))
|
|
161 (newline n)
|
|
162 ;; pad lines out to fill-column
|
|
163 (goto-char (point-min))
|
|
164 (while (not (eobp))
|
|
165 (end-of-line)
|
|
166 (indent-to fill-column)
|
|
167 (move-to-column fill-column)
|
|
168 (delete-region (point) (progn (end-of-line) (point)))
|
|
169 (forward-line))
|
|
170 ;; expand tabs to spaces
|
|
171 (untabify (point-min) (point-max))
|
|
172 ;; before starting be sure the automaton has room to grow
|
|
173 (life-expand-plane-if-needed)
|
|
174 ;; compute initial neighbor deltas
|
|
175 (life-compute-neighbor-deltas)))
|
|
176
|
|
177 (defun life-compute-neighbor-deltas ()
|
|
178 (setq life-neighbor-deltas
|
|
179 (list -1 (- fill-column)
|
|
180 (- (1+ fill-column)) (- (+ 2 fill-column))
|
|
181 1 fill-column (1+ fill-column)
|
|
182 (+ 2 fill-column))))
|
|
183
|
|
184 (defun life-insert-random-pattern ()
|
|
185 (insert-rectangle
|
|
186 (elt life-patterns (random (length life-patterns))))
|
|
187 (insert ?\n))
|
|
188
|
|
189 (defun life-increment-generation ()
|
|
190 (life-increment life-current-generation)
|
|
191 (setq life-generation-string (int-to-string life-current-generation)))
|
|
192
|
|
193 (defun life-grim-reaper ()
|
|
194 ;; Clear the match information. Later we check to see if it
|
|
195 ;; is still clear, if so then all the cells have died.
|
|
196 (store-match-data nil)
|
|
197 (goto-char (point-min))
|
|
198 ;; For speed declare all local variable outside the loop.
|
|
199 (let (point char pivot living-neighbors list)
|
|
200 (while (search-forward (life-life-string) nil t)
|
|
201 (setq list life-neighbor-deltas
|
|
202 living-neighbors 0
|
|
203 pivot (1- (point)))
|
|
204 (while list
|
|
205 (setq point (+ pivot (car list))
|
|
206 char (char-after point))
|
|
207 (cond ((eq char (life-void-char))
|
|
208 (subst-char-in-region point (1+ point)
|
|
209 (life-void-char) 1 t))
|
|
210 ((< char 3)
|
|
211 (subst-char-in-region point (1+ point) char (1+ char) t))
|
|
212 ((< char 9)
|
|
213 (subst-char-in-region point (1+ point) char 9 t))
|
|
214 ((>= char (life-life-char))
|
|
215 (life-increment living-neighbors)))
|
|
216 (setq list (cdr list)))
|
|
217 (if (memq living-neighbors '(2 3))
|
|
218 ()
|
|
219 (subst-char-in-region pivot (1+ pivot)
|
|
220 (life-life-char) (life-death-char) t))))
|
|
221 (if (null (match-beginning 0))
|
|
222 (life-extinct-quit))
|
|
223 (subst-char-in-region 1 (point-max) 9 (life-void-char) t)
|
|
224 (subst-char-in-region 1 (point-max) 1 (life-void-char) t)
|
|
225 (subst-char-in-region 1 (point-max) 2 (life-void-char) t)
|
|
226 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t)
|
|
227 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t))
|
|
228
|
|
229 (defun life-expand-plane-if-needed ()
|
|
230 (catch 'done
|
|
231 (goto-char (point-min))
|
|
232 (while (not (eobp))
|
|
233 ;; check for life at beginning or end of line. If found at
|
|
234 ;; either end, expand at both ends,
|
|
235 (cond ((or (eq (following-char) (life-life-char))
|
|
236 (eq (progn (end-of-line) (preceding-char)) (life-life-char)))
|
|
237 (goto-char (point-min))
|
|
238 (while (not (eobp))
|
|
239 (insert (life-void-char))
|
|
240 (end-of-line)
|
|
241 (insert (life-void-char))
|
|
242 (forward-char))
|
|
243 (setq fill-column (+ 2 fill-column))
|
|
244 (scroll-left 1)
|
|
245 (life-compute-neighbor-deltas)
|
|
246 (throw 'done t)))
|
|
247 (forward-line)))
|
|
248 (goto-char (point-min))
|
|
249 ;; check for life within the first two lines of the buffer.
|
|
250 ;; If present insert two lifeless lines at the beginning..
|
|
251 (cond ((search-forward (life-life-string)
|
|
252 (+ (point) fill-column fill-column 2) t)
|
|
253 (goto-char (point-min))
|
|
254 (insert-char (life-void-char) fill-column)
|
|
255 (insert ?\n)
|
|
256 (insert-char (life-void-char) fill-column)
|
|
257 (insert ?\n)
|
|
258 (setq life-window-start (+ life-window-start fill-column 1))))
|
|
259 (goto-char (point-max))
|
|
260 ;; check for life within the last two lines of the buffer.
|
|
261 ;; If present insert two lifeless lines at the end.
|
|
262 (cond ((search-backward (life-life-string)
|
|
263 (- (point) fill-column fill-column 2) t)
|
|
264 (goto-char (point-max))
|
|
265 (insert-char (life-void-char) fill-column)
|
|
266 (insert ?\n)
|
|
267 (insert-char (life-void-char) fill-column)
|
|
268 (insert ?\n)
|
|
269 (setq life-window-start (+ life-window-start fill-column 1)))))
|
|
270
|
|
271 (defun life-display-generation (sleeptime)
|
|
272 (goto-char life-window-start)
|
|
273 (recenter 0)
|
|
274
|
|
275 ;; Redisplay; if the user has hit a key, exit the loop.
|
|
276 (or (eq t (sit-for sleeptime))
|
|
277 (throw 'life-exit nil)))
|
|
278
|
|
279 (defun life-extinct-quit ()
|
|
280 (life-display-generation 0)
|
|
281 (signal 'life-extinct nil))
|
|
282
|
|
283 ;; XEmacs change
|
|
284 (define-error 'life-extinct "All life has perished" 'quit)
|
|
285
|
|
286 (provide 'life)
|
|
287
|
|
288 ;;; life.el ends here
|