1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-16 09:50:25 +00:00
emacs/lisp/play/life.el

297 lines
10 KiB
EmacsLisp
Raw Normal View History

1992-05-30 22:12:04 +00:00
;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
;; Copyright (C) 1988, 2001-2015 Free Software Foundation, Inc.
1992-07-22 04:22:42 +00:00
1997-05-27 17:46:19 +00:00
;; Author: Kyle Jones <kyleuunet.uu.net>
;; Maintainer: emacs-devel@gnu.org
1993-03-18 21:29:42 +00:00
;; Keywords: games
1992-07-16 21:47:34 +00:00
1990-03-06 16:45:37 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1990-03-06 16:45:37 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1990-03-06 16:45:37 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1990-03-06 16:45:37 +00:00
1993-03-22 03:27:18 +00:00
;;; Commentary:
;; A demonstrator for John Horton Conway's "Life" cellular automaton
;; in Emacs Lisp. Picks a random one of a set of interesting Life
1993-06-09 11:59:12 +00:00
;; patterns and evolves it according to the familiar rules.
1993-03-22 03:27:18 +00:00
1992-07-16 21:47:34 +00:00
;;; Code:
(defvar life-patterns
1990-03-06 16:45:37 +00:00
[("@@@" " @@" "@@@")
("@@@ @@@" "@@ @@ " "@@@ @@@")
("@@@ @@@" "@@ @@" "@@@ @@@")
("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
("@@@@@@@@@@")
(" @@@@@@@@@@ "
" @@@@@@@@@@ "
" @@@@@@@@@@ "
"@@@@@@@@@@ "
"@@@@@@@@@@ ")
("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@")
("@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @"
"@ @" "@ @" "@ @")
("@@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@ " " @@ " " @@ "
" @@")
2003-02-04 13:24:35 +00:00
("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@"
"@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")
(" @ "
" @ @ "
" @@ @@ @@"
" @ @ @@ @@"
"@@ @ @ @@ "
"@@ @ @ @@ @ @ "
" @ @ @ "
" @ @ "
" @@ ")
(" @ "
" @ @@"
" @ @ "
" @ "
" @ "
"@ @ ")
("@@@ @"
"@ "
" @@"
" @@ @"
"@ @ @")
("@@@@@@@@ @@@@@ @@@ @@@@@@@ @@@@@")]
1990-03-06 16:45:37 +00:00
"Vector of rectangles containing some Life startup patterns.")
;; Macros are used macros for manifest constants instead of variables
;; because the compiler will convert them to constants, which should
;; eval faster than symbols.
;;
;; Don't change any of the life-* macro constants unless you thoroughly
;; understand the `life-grim-reaper' function.
1992-08-04 04:15:43 +00:00
(defmacro life-life-char () ?@)
(defmacro life-death-char () (1+ (life-life-char)))
(defmacro life-birth-char () 3)
(defmacro life-void-char () ?\ )
(defmacro life-life-string () (char-to-string (life-life-char)))
(defmacro life-death-string () (char-to-string (life-death-char)))
(defmacro life-birth-string () (char-to-string (life-birth-char)))
(defmacro life-void-string () (char-to-string (life-void-char)))
(defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]"))
(defmacro life-increment (variable) (list 'setq variable (list '1+ variable)))
1992-08-04 04:15:43 +00:00
1990-03-06 16:45:37 +00:00
;; list of numbers that tell how many characters to move to get to
;; each of a cell's eight neighbors.
(defvar life-neighbor-deltas nil)
1990-03-06 16:45:37 +00:00
;; window display always starts here. Easier to deal with than
;; (scroll-up) and (scroll-down) when trying to center the display.
(defvar life-window-start nil)
1990-03-06 16:45:37 +00:00
;; For mode line
(defvar life-current-generation nil)
1990-03-06 16:45:37 +00:00
;; Sadly, mode-line-format won't display numbers.
(defvar life-generation-string nil)
1990-03-06 16:45:37 +00:00
1992-08-04 04:15:43 +00:00
;;;###autoload
1990-03-06 16:45:37 +00:00
(defun life (&optional sleeptime)
"Run Conway's Life simulation.
1991-03-12 20:00:05 +00:00
The starting pattern is randomly selected. Prefix arg (optional first
arg non-nil from a program) is the number of seconds to sleep between
1990-03-06 16:45:37 +00:00
generations (this defaults to 1)."
(interactive "p")
(or sleeptime (setq sleeptime 1))
(life-setup)
1992-08-04 04:15:43 +00:00
(catch 'life-exit
(while t
(let ((inhibit-quit t)
(inhibit-read-only t))
(life-display-generation sleeptime)
1992-08-04 04:15:43 +00:00
(life-grim-reaper)
(life-expand-plane-if-needed)
(life-increment-generation)))))
1990-03-06 16:45:37 +00:00
(define-derived-mode life-mode special-mode "Life"
"Major mode for the buffer of `life'."
(setq-local case-fold-search nil)
(setq-local truncate-lines t)
(setq-local show-trailing-whitespace nil)
(setq-local life-current-generation 0)
(setq-local life-generation-string "0")
(setq-local mode-line-buffer-identification '("Life: generation "
life-generation-string))
(setq-local fill-column (1- (window-width)))
(setq-local life-window-start 1)
(buffer-disable-undo))
1990-03-06 16:45:37 +00:00
(defun life-setup ()
(switch-to-buffer (get-buffer-create "*Life*") t)
(erase-buffer)
(life-mode)
;; stuff in the random pattern
(let ((inhibit-read-only t))
1990-03-06 16:45:37 +00:00
(life-insert-random-pattern)
;; make sure (life-life-char) is used throughout
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
(while (re-search-forward (life-not-void-regexp) nil t)
(replace-match (life-life-string) t t))
;; center the pattern horizontally
(goto-char (point-min))
(let ((n (/ (- fill-column (line-end-position)) 2)))
(while (not (eobp))
(indent-to n)
(forward-line)))
1990-03-06 16:45:37 +00:00
;; center the pattern vertically
(let ((n (/ (- (1- (window-height))
(count-lines (point-min) (point-max)))
2)))
(goto-char (point-min))
(newline n)
(goto-char (point-max))
(newline n))
1990-03-06 16:45:37 +00:00
;; pad lines out to fill-column
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
(while (not (eobp))
(end-of-line)
(indent-to fill-column)
(move-to-column fill-column)
(delete-region (point) (progn (end-of-line) (point)))
(forward-line))
;; expand tabs to spaces
(untabify (point-min) (point-max))
;; before starting be sure the automaton has room to grow
(life-expand-plane-if-needed)
;; compute initial neighbor deltas
(life-compute-neighbor-deltas)))
(defun life-compute-neighbor-deltas ()
(setq life-neighbor-deltas
(list -1 (- fill-column)
(- (1+ fill-column)) (- (+ 2 fill-column))
1 fill-column (1+ fill-column)
(+ 2 fill-column))))
(defun life-insert-random-pattern ()
(insert-rectangle
(elt life-patterns (random (length life-patterns))))
1990-03-06 16:45:37 +00:00
(insert ?\n))
(defun life-increment-generation ()
(life-increment life-current-generation)
1990-03-06 16:45:37 +00:00
(setq life-generation-string (int-to-string life-current-generation)))
(defun life-grim-reaper ()
;; Clear the match information. Later we check to see if it
;; is still clear, if so then all the cells have died.
(set-match-data nil)
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
;; For speed declare all local variable outside the loop.
(let (point char pivot living-neighbors list)
(while (search-forward (life-life-string) nil t)
(setq list life-neighbor-deltas
living-neighbors 0
pivot (1- (point)))
(while list
(setq point (+ pivot (car list))
char (char-after point))
(cond ((eq char (life-void-char))
(subst-char-in-region point (1+ point)
(life-void-char) 1 t))
((< char 3)
(subst-char-in-region point (1+ point) char (1+ char) t))
((< char 9)
(subst-char-in-region point (1+ point) char 9 t))
((>= char (life-life-char))
(life-increment living-neighbors)))
1990-03-06 16:45:37 +00:00
(setq list (cdr list)))
(if (memq living-neighbors '(2 3))
()
(subst-char-in-region pivot (1+ pivot)
(life-life-char) (life-death-char) t))))
(if (null (match-beginning 0))
(life-extinct-quit))
(subst-char-in-region 1 (point-max) 9 (life-void-char) t)
(subst-char-in-region 1 (point-max) 1 (life-void-char) t)
(subst-char-in-region 1 (point-max) 2 (life-void-char) t)
(subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t)
(subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t))
(defun life-expand-plane-if-needed ()
(catch 'done
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
(while (not (eobp))
;; check for life at beginning or end of line. If found at
;; either end, expand at both ends,
(cond ((or (eq (following-char) (life-life-char))
(eq (progn (end-of-line) (preceding-char)) (life-life-char)))
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
(while (not (eobp))
(insert (life-void-char))
(end-of-line)
(insert (life-void-char))
(forward-char))
(setq fill-column (+ 2 fill-column))
(scroll-left 1)
(life-compute-neighbor-deltas)
(throw 'done t)))
(forward-line)))
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
;; check for life within the first two lines of the buffer.
;; If present insert two lifeless lines at the beginning..
(cond ((search-forward (life-life-string)
(+ (point) fill-column fill-column 2) t)
(goto-char (point-min))
1990-03-06 16:45:37 +00:00
(insert-char (life-void-char) fill-column)
(insert ?\n)
(insert-char (life-void-char) fill-column)
(insert ?\n)
(setq life-window-start (+ life-window-start fill-column 1))))
(goto-char (point-max))
1990-03-06 16:45:37 +00:00
;; check for life within the last two lines of the buffer.
;; If present insert two lifeless lines at the end.
(cond ((search-backward (life-life-string)
(- (point) fill-column fill-column 2) t)
(goto-char (point-max))
1990-03-06 16:45:37 +00:00
(insert-char (life-void-char) fill-column)
(insert ?\n)
(insert-char (life-void-char) fill-column)
(insert ?\n)
(setq life-window-start (+ life-window-start fill-column 1)))))
(defun life-display-generation (sleeptime)
(goto-char life-window-start)
(recenter 0)
2003-02-04 13:24:35 +00:00
1992-08-04 04:15:43 +00:00
;; Redisplay; if the user has hit a key, exit the loop.
(or (and (sit-for sleeptime) (< 0 sleeptime))
(not (input-pending-p))
1992-08-04 04:15:43 +00:00
(throw 'life-exit nil)))
1990-03-06 16:45:37 +00:00
(defun life-extinct-quit ()
(life-display-generation 0)
(signal 'life-extinct nil))
* lisp/subr.el (define-error): New function. * doc/lispref/control.texi (Signaling Errors): Refer to define-error. (Error Symbols): Add `define-error'. * doc/lispref/errors.texi (Standard Errors): Don't refer to `error-conditions'. * lisp/progmodes/ada-xref.el (ada-error-file-not-found): Rename from error-file-not-found and define with define-error. * lisp/emacs-lisp/cl-lib.el (cl-assertion-failed): Move here from subr.el and define with define-error. * lisp/userlock.el (file-locked, file-supersession): * lisp/simple.el (mark-inactive): * lisp/progmodes/js.el (js-moz-bad-rpc, js-js-error): * lisp/progmodes/ada-mode.el (ada-mode-errors): * lisp/play/life.el (life-extinct): * lisp/nxml/xsd-regexp.el (xsdre-invalid-regexp, xsdre-parse-error): * lisp/nxml/xmltok.el (xmltok-markup-declaration-parse-error): * lisp/nxml/rng-util.el (rng-error): * lisp/nxml/rng-uri.el (rng-uri-error): * lisp/nxml/rng-match.el (rng-compile-error): * lisp/nxml/rng-cmpct.el (rng-c-incorrect-schema): * lisp/nxml/nxml-util.el (nxml-error, nxml-file-parse-error): * lisp/nxml/nxml-rap.el (nxml-scan-error): * lisp/nxml/nxml-outln.el (nxml-outline-error): * lisp/net/soap-client.el (soap-error): * lisp/net/gnutls.el (gnutls-error): * lisp/net/ange-ftp.el (ftp-error): * lisp/mpc.el (mpc-proc-error): * lisp/json.el (json-error, json-readtable-error, json-unknown-keyword) (json-number-format, json-string-escape, json-string-format) (json-key-format, json-object-format): * lisp/jka-compr.el (compression-error): * lisp/international/quail.el (quail-error): * lisp/international/kkc.el (kkc-error): * lisp/emacs-lisp/ert.el (ert-test-failed): * lisp/calc/calc.el (calc-error, inexact-result, math-overflow) (math-underflow): * lisp/bookmark.el (bookmark-error-no-filename): * lisp/epg.el (epg-error): Define with define-error.
2013-08-09 21:22:44 +00:00
(define-error 'life-extinct "All life has perished" 'quit) ;FIXME: quit really?
1992-03-16 20:39:07 +00:00
(provide 'life)
1992-05-30 22:12:04 +00:00
;;; life.el ends here