* lisp/subr.el (define-error): New function.
[emacs.git] / lisp / play / life.el
bloba73f3a58e6670e20257ae2aaa0606bf2e90a1d51
1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
3 ;; Copyright (C) 1988, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Kyle Jones <kyleuunet.uu.net>
6 ;; Maintainer: FSF
7 ;; Keywords: games
9 ;; This file is part of GNU Emacs.
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
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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
28 ;; patterns and evolves it according to the familiar rules.
30 ;;; Code:
32 (defvar life-patterns
33 [("@@@" " @@" "@@@")
34 ("@@@ @@@" "@@ @@ " "@@@ @@@")
35 ("@@@ @@@" "@@ @@" "@@@ @@@")
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 " @ @ @@ @@"
61 "@@ @ @ @@ "
62 "@@ @ @ @@ @ @ "
63 " @ @ @ "
64 " @ @ "
65 " @@ ")
66 (" @ "
67 " @ @@"
68 " @ @ "
69 " @ "
70 " @ "
71 "@ @ ")
72 ("@@@ @"
73 "@ "
74 " @@"
75 " @@ @"
76 "@ @ @")
77 ("@@@@@@@@ @@@@@ @@@ @@@@@@@ @@@@@")]
78 "Vector of rectangles containing some Life startup patterns.")
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.
84 ;; Don't change any of the life-* macro constants unless you thoroughly
85 ;; understand the `life-grim-reaper' function.
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 () ?\ )
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]"))
98 (defmacro life-increment (variable) (list 'setq variable (list '1+ variable)))
101 ;; list of numbers that tell how many characters to move to get to
102 ;; each of a cell's eight neighbors.
103 (defvar life-neighbor-deltas nil)
105 ;; window display always starts here. Easier to deal with than
106 ;; (scroll-up) and (scroll-down) when trying to center the display.
107 (defvar life-window-start nil)
109 ;; For mode line
110 (defvar life-current-generation nil)
111 ;; Sadly, mode-line-format won't display numbers.
112 (defvar life-generation-string nil)
114 ;;;###autoload
115 (defun life (&optional sleeptime)
116 "Run Conway's Life simulation.
117 The starting pattern is randomly selected. Prefix arg (optional first
118 arg non-nil from a program) is the number of seconds to sleep between
119 generations (this defaults to 1)."
120 (interactive "p")
121 (or sleeptime (setq sleeptime 1))
122 (life-setup)
123 (catch 'life-exit
124 (while t
125 (let ((inhibit-quit t))
126 (life-display-generation sleeptime)
127 (life-grim-reaper)
128 (life-expand-plane-if-needed)
129 (life-increment-generation)))))
131 (defalias 'life-mode 'life)
132 (put 'life-mode 'mode-class 'special)
134 (defun life-setup ()
135 (let (n)
136 (switch-to-buffer (get-buffer-create "*Life*") t)
137 (erase-buffer)
138 (kill-all-local-variables)
139 (setq case-fold-search nil
140 mode-name "Life"
141 major-mode 'life-mode
142 truncate-lines t
143 show-trailing-whitespace nil
144 life-current-generation 0
145 life-generation-string "0"
146 mode-line-buffer-identification '("Life: generation "
147 life-generation-string)
148 fill-column (1- (window-width))
149 life-window-start 1)
150 (buffer-disable-undo (current-buffer))
151 ;; stuff in the random pattern
152 (life-insert-random-pattern)
153 ;; make sure (life-life-char) is used throughout
154 (goto-char (point-min))
155 (while (re-search-forward (life-not-void-regexp) nil t)
156 (replace-match (life-life-string) t t))
157 ;; center the pattern horizontally
158 (goto-char (point-min))
159 (setq n (/ (- fill-column (line-end-position)) 2))
160 (while (not (eobp))
161 (indent-to n)
162 (forward-line))
163 ;; center the pattern vertically
164 (setq n (/ (- (1- (window-height))
165 (count-lines (point-min) (point-max)))
167 (goto-char (point-min))
168 (newline n)
169 (goto-char (point-max))
170 (newline n)
171 ;; pad lines out to fill-column
172 (goto-char (point-min))
173 (while (not (eobp))
174 (end-of-line)
175 (indent-to fill-column)
176 (move-to-column fill-column)
177 (delete-region (point) (progn (end-of-line) (point)))
178 (forward-line))
179 ;; expand tabs to spaces
180 (untabify (point-min) (point-max))
181 ;; before starting be sure the automaton has room to grow
182 (life-expand-plane-if-needed)
183 ;; compute initial neighbor deltas
184 (life-compute-neighbor-deltas)))
186 (defun life-compute-neighbor-deltas ()
187 (setq life-neighbor-deltas
188 (list -1 (- fill-column)
189 (- (1+ fill-column)) (- (+ 2 fill-column))
190 1 fill-column (1+ fill-column)
191 (+ 2 fill-column))))
193 (defun life-insert-random-pattern ()
194 (insert-rectangle
195 (elt life-patterns (random (length life-patterns))))
196 (insert ?\n))
198 (defun life-increment-generation ()
199 (life-increment life-current-generation)
200 (setq life-generation-string (int-to-string life-current-generation)))
202 (defun life-grim-reaper ()
203 ;; Clear the match information. Later we check to see if it
204 ;; is still clear, if so then all the cells have died.
205 (set-match-data nil)
206 (goto-char (point-min))
207 ;; For speed declare all local variable outside the loop.
208 (let (point char pivot living-neighbors list)
209 (while (search-forward (life-life-string) nil t)
210 (setq list life-neighbor-deltas
211 living-neighbors 0
212 pivot (1- (point)))
213 (while list
214 (setq point (+ pivot (car list))
215 char (char-after point))
216 (cond ((eq char (life-void-char))
217 (subst-char-in-region point (1+ point)
218 (life-void-char) 1 t))
219 ((< char 3)
220 (subst-char-in-region point (1+ point) char (1+ char) t))
221 ((< char 9)
222 (subst-char-in-region point (1+ point) char 9 t))
223 ((>= char (life-life-char))
224 (life-increment living-neighbors)))
225 (setq list (cdr list)))
226 (if (memq living-neighbors '(2 3))
228 (subst-char-in-region pivot (1+ pivot)
229 (life-life-char) (life-death-char) t))))
230 (if (null (match-beginning 0))
231 (life-extinct-quit))
232 (subst-char-in-region 1 (point-max) 9 (life-void-char) t)
233 (subst-char-in-region 1 (point-max) 1 (life-void-char) t)
234 (subst-char-in-region 1 (point-max) 2 (life-void-char) t)
235 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t)
236 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t))
238 (defun life-expand-plane-if-needed ()
239 (catch 'done
240 (goto-char (point-min))
241 (while (not (eobp))
242 ;; check for life at beginning or end of line. If found at
243 ;; either end, expand at both ends,
244 (cond ((or (eq (following-char) (life-life-char))
245 (eq (progn (end-of-line) (preceding-char)) (life-life-char)))
246 (goto-char (point-min))
247 (while (not (eobp))
248 (insert (life-void-char))
249 (end-of-line)
250 (insert (life-void-char))
251 (forward-char))
252 (setq fill-column (+ 2 fill-column))
253 (scroll-left 1)
254 (life-compute-neighbor-deltas)
255 (throw 'done t)))
256 (forward-line)))
257 (goto-char (point-min))
258 ;; check for life within the first two lines of the buffer.
259 ;; If present insert two lifeless lines at the beginning..
260 (cond ((search-forward (life-life-string)
261 (+ (point) fill-column fill-column 2) t)
262 (goto-char (point-min))
263 (insert-char (life-void-char) fill-column)
264 (insert ?\n)
265 (insert-char (life-void-char) fill-column)
266 (insert ?\n)
267 (setq life-window-start (+ life-window-start fill-column 1))))
268 (goto-char (point-max))
269 ;; check for life within the last two lines of the buffer.
270 ;; If present insert two lifeless lines at the end.
271 (cond ((search-backward (life-life-string)
272 (- (point) fill-column fill-column 2) t)
273 (goto-char (point-max))
274 (insert-char (life-void-char) fill-column)
275 (insert ?\n)
276 (insert-char (life-void-char) fill-column)
277 (insert ?\n)
278 (setq life-window-start (+ life-window-start fill-column 1)))))
280 (defun life-display-generation (sleeptime)
281 (goto-char life-window-start)
282 (recenter 0)
284 ;; Redisplay; if the user has hit a key, exit the loop.
285 (or (and (sit-for sleeptime) (< 0 sleeptime))
286 (not (input-pending-p))
287 (throw 'life-exit nil)))
289 (defun life-extinct-quit ()
290 (life-display-generation 0)
291 (signal 'life-extinct nil))
293 (define-error 'life-extinct "All life has perished" 'quit) ;FIXME: quit really?
295 (provide 'life)
297 ;;; life.el ends here