Conway's life

Dan Ingalls Dan.Ingalls at disney.com
Fri Jul 14 21:19:49 UTC 2000


>Has anyone ever done Conway's life in Squeak?
>
>I was going to write it, but I figured I would see if I could get it from
>someone first, and learn from their code.

Michael -

It's probably not a good model to learn from, but here's a port to Squeak of my BitBlt Life program that first appeared in the August 1981 issue of Byte magazine.

To run it, execute...
	    Display restoreAfter: [(Life new size: 400) run].
This will run noticeably faster if you set your display depth to 1.

It does a 400x400 generation in 43ms on my 250MHz G3.

Enjoy
	- Dan
-------------- next part --------------
'From Squeak2.6 of 11 October 1999 [latest update: #1647] on 1 December 1999 at 9:37:05 am'!
"Change Set:		BBLife
Date:			1 December 1999
Author:			Dan Ingalls

A port to Squeak of my BitBlt Life program that first appeared in the August 1981 isue of Byte magazine.  To run it, execute...
	    Display restoreAfter: [(Life new size: 400) run].
This will run noticeably faster if you set your display depth to 1.
Enjoy
"!

Object subclass: #Life
	instanceVariableNames: 'size cells nbr1 nbr2 nbr4 carry2 carry4 '
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Graphics-Display Objects'!

!Life methodsFor: 'unclassified' stamp: 'di 12/1/1999 09:23'!
displayThisState
    cells displayAt: 100 at 100! !

!Life methodsFor: 'unclassified' stamp: 'di 12/1/1999 09:22'!
initialize    "Initialize cells for random distribution"
    | rand |
	Display border: ((100 at 100 extent: size) expandBy: 2) width: 2.
	rand _ Random new.
	1 to: size do:
    		[:i | cells reverse: ((rand next * size)@(rand next * size) extent: size // 4).
		"self displayThisState"]! !

!Life methodsFor: 'unclassified' stamp: 'di 12/1/1999 09:30'!
nextLifeGeneration
    "Compute next generation by computing neighbor counts.
    These are tallied in nbr1, nbr2, nbr4."
    | all delta |
    all _ 0 at 0 extent: cells extent.
    nbr1 fillWhite.  nbr2 fillWhite.  nbr4 fillWhite.
    1 to: 8 do:                   "Three-bit add for each shift of cells"
        [:i | delta _ (i+4\\9//3 - 1)@(i+4\\3 - 1).  "8 neighbor deltas"
        carry2 copy: all from: nbr1 to: 0 at 0 rule: Form over.
        carry2 copy: all from: cells to: delta rule: Form and. "carry 2"
        nbr1 copy: all from: cells to: delta rule: Form reverse.   "sum 1" "(reverse is xor)"
        carry4 copy: all from: nbr2 to: 0 at 0 rule: Form over.
        carry4 copy: all from: carry2 to: 0 at 0 rule: Form and.  "carry 4"
        nbr2 copy: all from: carry2 to: 0 at 0 rule: Form reverse.    "sum 2"
        nbr4 copy: all from: carry4 to: 0 at 0 rule: Form reverse].    "sum 4"
    "Next gen = ((2s AND cells) OR (2s AND 1s)) AND (NOT 4s)"
    cells copy: all from: nbr2 to: 0 at 0 rule: Form and.
    nbr1 copy: all from: nbr2 to: 0 at 0 rule: Form and.
    cells copy: all from: nbr1 to: 0 at 0 rule: Form under.
    cells copy: all from: nbr4 to: 0 at 0 rule: Form erase.
    cells borderWidth: 1 fillColor: Color white.  "trim edges"! !

!Life methodsFor: 'unclassified' stamp: 'di 12/1/1999 09:34'!
run
    self initialize.
    [Sensor anyButtonPressed] whileFalse:
        [self displayThisState.
        self nextLifeGeneration].! !

!Life methodsFor: 'unclassified'!
size: s    "Initialize for size of array."
    size _ s.
    cells _ Form extent: s at s.
    nbr1 _ Form extent: s at s.
    nbr2 _ Form extent: s at s.
    nbr4 _ Form extent: s at s.
    carry2 _ Form extent: s at s.
    carry4 _ Form extent: s at s.! !




More information about the Squeak-dev mailing list