Wireworld
Conway's Game of Life
It is capable of doing sophisticated computations with appropriate programs
(it is actually Turing complete),
and is much simpler to program for.

A Wireworld arena consists of a Cartesian grid of cells,
each of which can be in one of four states.
All cell transitions happen simultaneously.

The cell transition rules are this:



 Input State

 Output State

 Condition


 empty

 empty




 electron head 

 electron tail 




 electron tail 

 conductor




 conductor

 electron head 

 if 1 or 2 cells in the neighborhood of the cell are in the state “electron head”


 conductor

 conductor

 otherwise



Task

Create a program that reads a Wireworld program from a file and displays an animation of the processing. Here is a sample description file (using "H" for an electron head, "t" for a tail, "." for a conductor and a space for empty) you may wish to test with, which demonstrates two cycle-3 generators and an inhibit gate:

tH.........
.   .
   ...
.   .
Ht.. ......

While text-only implementations of this task are possible, mapping cells to pixels is advisable if you wish to be able to display large designs. The logic is not significantly more complex.




include c:\cxpl\codes;          \intrinsic 'code' declarations
char New(53,40), Old(53,40);
 
proc Block(X0, Y0, C);          \Display a colored block
int  X0, Y0, C;                 \big (6x5) coordinates, char
int  X, Y;
[case C of                      \convert char to color
  ^H:   C:= $9;                 \blue
  ^t:   C:= $C;                 \red
  ^.:   C:= $E                  \yellow
other   C:= 0;                  \black
for Y:= Y0*5 to Y0*5+4 do       \make square blocks by correcting aspect ratio
    for X:= X0*6 to X0*6+5 do   \ (6x5 = square)
        Point(X,Y,C);
];
 
int X, Y, C;
[SetVid($13);                   \set 320x200 graphics display
for Y:= 0 to 40-1 do            \initialize New with space (empty) characters
    for X:= 0 to 53-1 do
        New(X, Y):= ^ ;
X:= 1;  Y:= 1;                  \read file from command line, skipping borders
loop    [C:= ChIn(1);
        case C of
          $0D:  X:= 1;          \carriage return
          $0A:  Y:= Y+1;        \line feed
          $1A:  quit            \end of file
        other   [New(X,Y):= C;  X:= X+1];
        ];
repeat  C:= Old;  Old:= New;  New:= C;  \swap arrays, by swapping their pointers
        for Y:= 1 to 39-1 do            \generate New array from Old
            for X:= 1 to 52-1 do        \ (skipping borders)
                [case Old(X,Y) of
                  ^ :   New(X,Y):= ^ ;  \copy empty to empty
                  ^H:   New(X,Y):= ^t;  \convert head to tail
                  ^t:   New(X,Y):= ^.   \convert tail to conductor
                other   [C:= (Old(X-1,Y-1)=^H) + (Old(X+0,Y-1)=^H) + \head count
                             (Old(X+1,Y-1)=^H) + (Old(X-1,Y+0)=^H) + \ in neigh-
                             (Old(X+1,Y+0)=^H) + (Old(X-1,Y+1)=^H) + \ boring
                             (Old(X+0,Y+1)=^H) + (Old(X+1,Y+1)=^H);  \ cells
                        New(X,Y):= if C=-1 or C=-2 then ^H else ^.;  \ (true=-1)
                        ];
                Block(X, Y, New(X,Y));  \display result
                ];
        Sound(0, 6, 1);                 \delay about 1/3 second
until   KeyHit;                         \keystroke terminates program
SetVid(3);                              \restore normal text mode
]