Commit | Line | Data |
---|---|---|
6594deb0 ER |
1 | ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs |
2 | ||
5fd6d89f | 3 | ;; Copyright (C) 1988, 2001, 2002, 2003, 2004, |
2f043267 | 4 | ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
3a801d0c | 5 | |
4228277d | 6 | ;; Author: Kyle Jones <kyleuunet.uu.net> |
4127f015 | 7 | ;; Maintainer: FSF |
e9571d2a | 8 | ;; Keywords: games |
e5167999 | 9 | |
80a677d9 JA |
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 | |
5a9dffec | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
80a677d9 JA |
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 | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
80a677d9 | 26 | |
e41b2db1 ER |
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 | |
eb8c3be9 | 31 | ;; patterns and evolves it according to the familiar rules. |
e41b2db1 | 32 | |
e5167999 ER |
33 | ;;; Code: |
34 | ||
4127f015 | 35 | (defvar life-patterns |
80a677d9 JA |
36 | [("@@@" " @@" "@@@") |
37 | ("@@@ @@@" "@@ @@ " "@@@ @@@") | |
38 | ("@@@ @@@" "@@ @@" "@@@ @@@") | |
39 | ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") | |
40 | ("@@@@@@@@@@") | |
41 | (" @@@@@@@@@@ " | |
42 | " @@@@@@@@@@ " | |
43 | " @@@@@@@@@@ " | |
44 | "@@@@@@@@@@ " | |
45 | "@@@@@@@@@@ ") | |
46 | ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@") | |
47 | ("@ @" "@ @" "@ @" | |
48 | "@ @" "@ @" "@ @" | |
49 | "@ @" "@ @" "@ @" | |
50 | "@ @" "@ @" "@ @" | |
51 | "@ @" "@ @" "@ @") | |
52 | ("@@ " " @@ " " @@ " | |
53 | " @@ " " @@ " " @@ " | |
54 | " @@ " " @@ " " @@ " | |
55 | " @@ " " @@ " " @@ " | |
56 | " @@ " " @@ " " @@ " | |
57 | " @@") | |
a1506d29 | 58 | ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@" |
5883afc5 JB |
59 | "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@") |
60 | (" @ " | |
61 | " @ @ " | |
62 | " @@ @@ @@" | |
63 | " @ @ @@ @@" | |
64 | "@@ @ @ @@ " | |
65 | "@@ @ @ @@ @ @ " | |
66 | " @ @ @ " | |
67 | " @ @ " | |
68 | " @@ ") | |
69 | (" @ " | |
70 | " @ @@" | |
71 | " @ @ " | |
72 | " @ " | |
73 | " @ " | |
74 | "@ @ ") | |
75 | ("@@@ @" | |
76 | "@ " | |
77 | " @@" | |
78 | " @@ @" | |
79 | "@ @ @") | |
80 | ("@@@@@@@@ @@@@@ @@@ @@@@@@@ @@@@@")] | |
80a677d9 JA |
81 | "Vector of rectangles containing some Life startup patterns.") |
82 | ||
83 | ;; Macros are used macros for manifest constants instead of variables | |
84 | ;; because the compiler will convert them to constants, which should | |
85 | ;; eval faster than symbols. | |
86 | ;; | |
80a677d9 JA |
87 | ;; Don't change any of the life-* macro constants unless you thoroughly |
88 | ;; understand the `life-grim-reaper' function. | |
e8a57935 JB |
89 | |
90 | (defmacro life-life-char () ?@) | |
91 | (defmacro life-death-char () (1+ (life-life-char))) | |
92 | (defmacro life-birth-char () 3) | |
93 | (defmacro life-void-char () ?\ ) | |
94 | ||
95 | (defmacro life-life-string () (char-to-string (life-life-char))) | |
96 | (defmacro life-death-string () (char-to-string (life-death-char))) | |
97 | (defmacro life-birth-string () (char-to-string (life-birth-char))) | |
98 | (defmacro life-void-string () (char-to-string (life-void-char))) | |
99 | (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]")) | |
100 | ||
44fad123 | 101 | (defmacro life-increment (variable) (list 'setq variable (list '1+ variable))) |
e8a57935 | 102 | |
80a677d9 JA |
103 | |
104 | ;; list of numbers that tell how many characters to move to get to | |
105 | ;; each of a cell's eight neighbors. | |
4127f015 | 106 | (defvar life-neighbor-deltas nil) |
80a677d9 JA |
107 | |
108 | ;; window display always starts here. Easier to deal with than | |
109 | ;; (scroll-up) and (scroll-down) when trying to center the display. | |
4127f015 | 110 | (defvar life-window-start nil) |
80a677d9 JA |
111 | |
112 | ;; For mode line | |
4127f015 | 113 | (defvar life-current-generation nil) |
80a677d9 | 114 | ;; Sadly, mode-line-format won't display numbers. |
4127f015 | 115 | (defvar life-generation-string nil) |
80a677d9 | 116 | |
44fad123 RS |
117 | (defvar life-initialized nil |
118 | "Non-nil if `life' has been run at least once.") | |
80a677d9 | 119 | |
e8a57935 | 120 | ;;;###autoload |
80a677d9 JA |
121 | (defun life (&optional sleeptime) |
122 | "Run Conway's Life simulation. | |
e3f99b91 JB |
123 | The starting pattern is randomly selected. Prefix arg (optional first |
124 | arg non-nil from a program) is the number of seconds to sleep between | |
80a677d9 JA |
125 | generations (this defaults to 1)." |
126 | (interactive "p") | |
44fad123 RS |
127 | (or life-initialized |
128 | (random t)) | |
129 | (setq life-initialized t) | |
80a677d9 JA |
130 | (or sleeptime (setq sleeptime 1)) |
131 | (life-setup) | |
e8a57935 JB |
132 | (catch 'life-exit |
133 | (while t | |
134 | (let ((inhibit-quit t)) | |
b0778a31 | 135 | (life-display-generation sleeptime) |
e8a57935 JB |
136 | (life-grim-reaper) |
137 | (life-expand-plane-if-needed) | |
b0778a31 | 138 | (life-increment-generation))))) |
80a677d9 | 139 | |
31e1d920 | 140 | (defalias 'life-mode 'life) |
80a677d9 JA |
141 | (put 'life-mode 'mode-class 'special) |
142 | ||
80a677d9 JA |
143 | (defun life-setup () |
144 | (let (n) | |
145 | (switch-to-buffer (get-buffer-create "*Life*") t) | |
146 | (erase-buffer) | |
147 | (kill-all-local-variables) | |
148 | (setq case-fold-search nil | |
149 | mode-name "Life" | |
150 | major-mode 'life-mode | |
151 | truncate-lines t | |
5883afc5 | 152 | show-trailing-whitespace nil |
80a677d9 JA |
153 | life-current-generation 0 |
154 | life-generation-string "0" | |
155 | mode-line-buffer-identification '("Life: generation " | |
156 | life-generation-string) | |
157 | fill-column (1- (window-width)) | |
158 | life-window-start 1) | |
159 | (buffer-disable-undo (current-buffer)) | |
160 | ;; stuff in the random pattern | |
161 | (life-insert-random-pattern) | |
162 | ;; make sure (life-life-char) is used throughout | |
44fad123 | 163 | (goto-char (point-min)) |
80a677d9 JA |
164 | (while (re-search-forward (life-not-void-regexp) nil t) |
165 | (replace-match (life-life-string) t t)) | |
166 | ;; center the pattern horizontally | |
44fad123 | 167 | (goto-char (point-min)) |
80a677d9 JA |
168 | (setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2)) |
169 | (while (not (eobp)) | |
170 | (indent-to n) | |
171 | (forward-line)) | |
172 | ;; center the pattern vertically | |
173 | (setq n (/ (- (1- (window-height)) | |
174 | (count-lines (point-min) (point-max))) | |
175 | 2)) | |
44fad123 | 176 | (goto-char (point-min)) |
80a677d9 | 177 | (newline n) |
44fad123 | 178 | (goto-char (point-max)) |
80a677d9 JA |
179 | (newline n) |
180 | ;; pad lines out to fill-column | |
44fad123 | 181 | (goto-char (point-min)) |
80a677d9 JA |
182 | (while (not (eobp)) |
183 | (end-of-line) | |
184 | (indent-to fill-column) | |
185 | (move-to-column fill-column) | |
186 | (delete-region (point) (progn (end-of-line) (point))) | |
187 | (forward-line)) | |
188 | ;; expand tabs to spaces | |
189 | (untabify (point-min) (point-max)) | |
190 | ;; before starting be sure the automaton has room to grow | |
191 | (life-expand-plane-if-needed) | |
192 | ;; compute initial neighbor deltas | |
193 | (life-compute-neighbor-deltas))) | |
194 | ||
195 | (defun life-compute-neighbor-deltas () | |
196 | (setq life-neighbor-deltas | |
197 | (list -1 (- fill-column) | |
198 | (- (1+ fill-column)) (- (+ 2 fill-column)) | |
199 | 1 fill-column (1+ fill-column) | |
200 | (+ 2 fill-column)))) | |
201 | ||
202 | (defun life-insert-random-pattern () | |
203 | (insert-rectangle | |
6b6d5edf | 204 | (elt life-patterns (random (length life-patterns)))) |
80a677d9 JA |
205 | (insert ?\n)) |
206 | ||
207 | (defun life-increment-generation () | |
44fad123 | 208 | (life-increment life-current-generation) |
80a677d9 JA |
209 | (setq life-generation-string (int-to-string life-current-generation))) |
210 | ||
211 | (defun life-grim-reaper () | |
212 | ;; Clear the match information. Later we check to see if it | |
213 | ;; is still clear, if so then all the cells have died. | |
ac092f22 | 214 | (set-match-data nil) |
44fad123 | 215 | (goto-char (point-min)) |
80a677d9 JA |
216 | ;; For speed declare all local variable outside the loop. |
217 | (let (point char pivot living-neighbors list) | |
218 | (while (search-forward (life-life-string) nil t) | |
219 | (setq list life-neighbor-deltas | |
220 | living-neighbors 0 | |
221 | pivot (1- (point))) | |
222 | (while list | |
223 | (setq point (+ pivot (car list)) | |
224 | char (char-after point)) | |
225 | (cond ((eq char (life-void-char)) | |
226 | (subst-char-in-region point (1+ point) | |
227 | (life-void-char) 1 t)) | |
228 | ((< char 3) | |
229 | (subst-char-in-region point (1+ point) char (1+ char) t)) | |
230 | ((< char 9) | |
231 | (subst-char-in-region point (1+ point) char 9 t)) | |
232 | ((>= char (life-life-char)) | |
44fad123 | 233 | (life-increment living-neighbors))) |
80a677d9 JA |
234 | (setq list (cdr list))) |
235 | (if (memq living-neighbors '(2 3)) | |
236 | () | |
237 | (subst-char-in-region pivot (1+ pivot) | |
238 | (life-life-char) (life-death-char) t)))) | |
239 | (if (null (match-beginning 0)) | |
240 | (life-extinct-quit)) | |
241 | (subst-char-in-region 1 (point-max) 9 (life-void-char) t) | |
242 | (subst-char-in-region 1 (point-max) 1 (life-void-char) t) | |
243 | (subst-char-in-region 1 (point-max) 2 (life-void-char) t) | |
244 | (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t) | |
245 | (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t)) | |
246 | ||
247 | (defun life-expand-plane-if-needed () | |
248 | (catch 'done | |
44fad123 | 249 | (goto-char (point-min)) |
80a677d9 JA |
250 | (while (not (eobp)) |
251 | ;; check for life at beginning or end of line. If found at | |
252 | ;; either end, expand at both ends, | |
253 | (cond ((or (eq (following-char) (life-life-char)) | |
254 | (eq (progn (end-of-line) (preceding-char)) (life-life-char))) | |
44fad123 | 255 | (goto-char (point-min)) |
80a677d9 JA |
256 | (while (not (eobp)) |
257 | (insert (life-void-char)) | |
258 | (end-of-line) | |
259 | (insert (life-void-char)) | |
260 | (forward-char)) | |
261 | (setq fill-column (+ 2 fill-column)) | |
262 | (scroll-left 1) | |
263 | (life-compute-neighbor-deltas) | |
264 | (throw 'done t))) | |
265 | (forward-line))) | |
44fad123 | 266 | (goto-char (point-min)) |
80a677d9 JA |
267 | ;; check for life within the first two lines of the buffer. |
268 | ;; If present insert two lifeless lines at the beginning.. | |
269 | (cond ((search-forward (life-life-string) | |
270 | (+ (point) fill-column fill-column 2) t) | |
44fad123 | 271 | (goto-char (point-min)) |
80a677d9 JA |
272 | (insert-char (life-void-char) fill-column) |
273 | (insert ?\n) | |
274 | (insert-char (life-void-char) fill-column) | |
275 | (insert ?\n) | |
276 | (setq life-window-start (+ life-window-start fill-column 1)))) | |
44fad123 | 277 | (goto-char (point-max)) |
80a677d9 JA |
278 | ;; check for life within the last two lines of the buffer. |
279 | ;; If present insert two lifeless lines at the end. | |
280 | (cond ((search-backward (life-life-string) | |
281 | (- (point) fill-column fill-column 2) t) | |
44fad123 | 282 | (goto-char (point-max)) |
80a677d9 JA |
283 | (insert-char (life-void-char) fill-column) |
284 | (insert ?\n) | |
285 | (insert-char (life-void-char) fill-column) | |
286 | (insert ?\n) | |
287 | (setq life-window-start (+ life-window-start fill-column 1))))) | |
288 | ||
289 | (defun life-display-generation (sleeptime) | |
290 | (goto-char life-window-start) | |
291 | (recenter 0) | |
a1506d29 | 292 | |
e8a57935 | 293 | ;; Redisplay; if the user has hit a key, exit the loop. |
33aba6a0 EZ |
294 | (or (and (sit-for sleeptime) (< 0 sleeptime)) |
295 | (not (input-pending-p)) | |
e8a57935 | 296 | (throw 'life-exit nil))) |
80a677d9 JA |
297 | |
298 | (defun life-extinct-quit () | |
299 | (life-display-generation 0) | |
300 | (signal 'life-extinct nil)) | |
301 | ||
302 | (put 'life-extinct 'error-conditions '(life-extinct quit)) | |
303 | (put 'life-extinct 'error-message "All life has perished") | |
49116ac0 JB |
304 | |
305 | (provide 'life) | |
306 | ||
ab5796a9 | 307 | ;;; arch-tag: e9373544-755e-42f5-a9a1-4d4c422bb97a |
6594deb0 | 308 | ;;; life.el ends here |