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