Wireworld: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|REXX}}: added whitespace.) |
m (→{{header|Wren}}: Minor tidy) |
||
(20 intermediate revisions by 15 users not shown) | |||
Line 14:
The cell transition rules are this:
::{| class=wikitable
|-
! Input State
Line 53:
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.
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V allstates = ‘Ht. ’
V head = allstates[0]
V tail = allstates[1]
V conductor = allstates[2]
V empty = allstates[3]
V w =
|‘tH.........
. .
...
. .
Ht.. ......’
T WW
[[Char]] world
Int w
Int h
F (world, w, h)
.world = world
.w = w
.h = h
F readfile(f)
V world = f.map(row -> row.rtrim(Array[Char]("\r\n")))
V height = world.len
V width = max(world.map(row -> row.len))
V nonrow = [‘ ’(‘ ’ * width)‘ ’]
V world2 = nonrow [+] world.map(row -> ‘ ’String(row).ljust(@width)‘ ’) [+] nonrow
V world3 = world2.map(row -> Array(row))
R WW(world3, width, height)
F newcell(currentworld, x, y)
V istate = currentworld[y][x]
assert(istate C :allstates, ‘Wireworld cell set to unknown value "#."’.format(istate))
V ostate = :empty
I istate == :head
ostate = :tail
E I istate == :tail
ostate = :conductor
E I istate == :empty
ostate = :empty
E
V n = sum([(-1, -1), (-1, +0), (-1, +1),
(+0, -1), (+0, +1),
(+1, -1), (+1, +0), (+1, +1)].map((dx, dy) -> Int(@currentworld[@y + dy][@x + dx] == :head)))
ostate = I n C 1..2 {:head} E :conductor
R ostate
F nextgen(ww)
V (world, width, height) = ww
V newworld = copy(world)
L(x) 1 .. width
L(y) 1 .. height
newworld[y][x] = newcell(world, x, y)
R WW(newworld, width, height)
F world2string(ww)
R ww.world[1 .< (len)-1].map(row -> (row[1 .< (len)-1]).join(‘’).rtrim((‘ ’, "\t", "\r", "\n"))).join("\n")
V ww = readfile(w.split("\n"))
L(gen) 10
print(("\n#3 ".format(gen))‘’(‘=’ * (ww.w - 4))"\n")
print(world2string(ww))
ww = nextgen(ww)</syntaxhighlight>
{{out}}
<pre style="height:45ex;overflow:scroll">
0 =======
tH.........
. .
...
. .
Ht.. ......
1 =======
.tH........
H .
...
H .
t... ......
2 =======
H.tH.......
t .
...
t .
.H.. ......
3 =======
tH.tH......
. H
...
. .
HtH. ......
4 =======
.tH.tH.....
H t
HHH
H .
t.tH ......
5 =======
H.tH.tH....
t .
ttt
t .
.H.t ......
6 =======
tH.tH.tH...
. H
...
. .
HtH. ......
7 =======
.tH.tH.tH..
H t
HHH
H .
t.tH ......
8 =======
H.tH.tH.tH.
t .
ttt
t .
.H.t ......
9 =======
tH.tH.tH.tH
. H
...
. .
HtH. ......
</pre>
=={{header|Ada}}==
<
procedure Test_Wireworld is
Line 120 ⟶ 273:
Wireworld (Oscillator);
end loop;
end Test_Wireworld;</
The solution assumes that the border of the board is empty. When transition is performed these cells are not changed. Automation transition is an in-place operation that allocates memory for to keep one row of the board size.
<pre style="height:30ex;overflow:scroll">
Line 170 ⟶ 323:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - missing transput function "printf" and non standard associate}}
<
Wireworld implementation.
CO
Line 291 ⟶ 444:
print ( world2string(ww) );
ww := next gen(ww)
OD</
{{out}}
<pre style="height:45ex;overflow:scroll">
Line 369 ⟶ 522:
{{works with|AutoHotkey_L}}
{{libheader|GDIP}}
<
#NoEnv
SetBatchLines, -1
Line 475 ⟶ 628:
Exit:
Gdip_Shutdown(pToken)
ExitApp</
=={{header|AutoIt}}==
<
$ww = ""
$ww &= "tH........." & @CR
Line 582 ⟶ 735:
WEnd
EndFunc ;==>Wireworld
</syntaxhighlight>
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:wireworld_bbc.gif|right]]
<
DIM P&(Size%-1,Size%-1), Q&(Size%-1,Size%-1)
Line 628 ⟶ 781:
SWAP P&(), Q&()
WAIT 50
UNTIL FALSE</
=={{header|C}}==
Line 642 ⟶ 795:
Compile with <code>-D_POSIX_C_SOURCE=199309L</code> or greater to make <code>nanosleep</code> visible in <code><time.h></code>.
<
#define ANIMATE_VT100_POSIX
#include <stdio.h>
Line 703 ⟶ 856:
return 0;
}</
=={{header|C sharp}}==
Line 713 ⟶ 866:
{{libheader|POSIX}} (for usleep)
<
#include <set>
#include <map>
Line 958 ⟶ 1,111:
}
std::cout << std::endl;
}</
=={{header|Ceylon}}==
<
shared Cell output({Cell*} neighbors) =>
Line 1,062 ⟶ 1,215:
}
}
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(destructuring-bind (rows cols) (array-dimensions wireworld)
(loop for off-row from (max 0 (1- row)) to (min (1- rows) (1+ row)) sum
Line 1,130 ⟶ 1,283:
" ... "
". . "
"Ht.. ......")))</
{{out}}
<pre style="height:30ex;overflow:scroll">CL-USER> (wireworld-show-gens (make-rosetta-wireworld) 12)
Line 1,207 ⟶ 1,360:
=={{header|D}}==
<
void wireworldStep(char[][] W1, char[][] W2) pure nothrow @safe @nogc {
Line 1,244 ⟶ 1,397:
swap(world, world2);
}
}</
{{out}}
<pre>
Line 1,281 ⟶ 1,434:
. ....
.. </pre>
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.IOUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Wireworld;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.IOUtils;
var
rows, cols: Integer;
rx, cx: Integer;
mn: TArray<Integer>;
procedure Print(grid: TArray<byte>);
begin
writeln(string.Create('_', cols * 2), #10);
for var r := 1 to rows do
begin
for var c := 1 to cols do
begin
if grid[r * cx + c] = 0 then
write(' ')
else
write(' ', chr(grid[r * cx + c]));
end;
writeln;
end;
end;
procedure Step(var dst: TArray<byte>; src: TArray<byte>);
begin
for var r := 1 to rows do
begin
for var c := 1 to cols do
begin
var x := r * cx + c;
dst[x] := src[x];
case chr(dst[x]) of
'H':
dst[x] := ord('t');
't':
dst[x] := ord('.');
'.':
begin
var nn := 0;
for var n in mn do
if src[x + n] = ord('H') then
inc(nn);
if (nn = 1) or (nn = 2) then
dst[x] := ord('H');
end;
end;
end;
end;
end;
procedure Main();
const
CONFIG_FILE = 'ww.config';
begin
if not FileExists(CONFIG_FILE) then
begin
Writeln(CONFIG_FILE, ' not exist');
exit;
end;
var srcRows := TFile.ReadAllLines(CONFIG_FILE);
rows := length(srcRows);
cols := 0;
for var r in srcRows do
begin
if Length(r) > cols then
cols := length(r);
end;
rx := rows + 2;
cx := cols + 2;
mn := [-cx - 1, -cx, -cx + 1, -1, 1, cx - 1, cx, cx + 1];
var _odd: TArray<byte>;
var _even: TArray<byte>;
SetLength(_odd, rx * cx);
SetLength(_even, rx * cx);
FillChar(_odd[0], rx * cx, 0);
FillChar(_even[0], rx * cx, 0);
for var i := 0 to High(srcRows) do
begin
var r := srcRows[i];
var offset := (i + 1) * cx + 1;
for var j := 1 to length(r) do
_odd[offset + j - 1] := ord(r[j]);
end;
while True do
begin
print(_odd);
step(_even, _odd);
Readln;
print(_even);
step(_odd, _even);
Readln;
end;
end;
begin
Main;
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
{{out}}
<pre>__________________
t H
. . . . .
. .
__________________
. t
. H . . .
. .
__________________
. .
. t H . .
. H
__________________
. .
. . t H .
H t
__________________
. .
H . . t H
t .
__________________
H .
t . . . t
. .
__________________
t H
. . . . .
. .
__________________
. t
. H . . .
. .
</pre>
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=hVTNbuMgEL7zFKNstdptFdaOVSU95J53WFUrikmLlhgLSJv06XfAYLDjarkYz3zz980M9mrB6V6JoyOvSr8wBaffz9BxoOSF8b+vRp+7FrbbLfkGpDeag33TH0BRDwBcCWb8xXKmBOyhrir4heZB9omCQXEPFX169MKjNiAxJCjR+VBehueC0B8S1lD/hJNuowc810LRyvesOOl3gWb3McI13aJaHtG7fEbrKkp8ulph+KpKIqEyrJ7DnnZLsM0c1jS7EWbFXLtrmiii8WsEd54a+0mClA6sGsHaxKq9w0Cy68/O/2Ev9oEtFD8M8UcSMxvYtO+x1uDDiF4wt6CN5vwOI4B1hr8xY9F3zhyL5T6DFV1lYfZSEBWIGbCHRexmEesWsU2JLZmEmwIg05m+BSU4LJGudZnszEcynJKN59w5qQbxajXy+T/KKfEd9EsSBrtDZZrwtDjnvmVOpCZ/sQiLk4bugihRVA5kcwurF2AFEXa6FD6R1u/fuvbpFMCkvH6hxCO9ocS5bP0CIvd4u8xBFh72sbDLrDLIjZheEe7z3AAmYKf5Q650Ml/zmeluKp8MzbBqH6wfuuVbQInuwMmTCG/a0K+AwifPf4MKn7JHRAZZFmCTwxD9QRtG3IGmQ+gYcPw5OEohqgn5Bw== Run it]
<syntaxhighlight>
sys topleft
global m[] nc .
background 777
#
proc show . .
clear
scale = 100 / nc
sz = scale * 0.95
for i to len m[]
x = (i - 1) mod nc
y = (i - 1) div nc
move x * scale y * scale
if m[i] = 0
color 000
elif m[i] = 1
color 980
elif m[i] = 2
color 338
else
color 833
.
rect sz sz
.
.
proc read . .
s$ = input
nc = len s$ + 2
for i to nc
m[] &= 0
.
repeat
m[] &= 0
for c$ in strchars s$
if c$ = "."
m[] &= 1
elif c$ = "H"
m[] &= 2
elif c$ = "t"
m[] &= 3
else
m[] &= 0
.
.
for i to nc - len s$ - 1
m[] &= 0
.
s$ = input
until s$ = ""
.
for i to nc
m[] &= 0
.
.
read
#
len mn[] len m[]
#
proc update . .
for i to len m[]
if m[i] = 2
mn[i] = 3
elif m[i] = 3
mn[i] = 1
elif m[i] = 1
s = 0
for dx = -1 to 1
for dy = -1 to 1
ix = i + dy * nc + dx
s += if m[ix] = 2
.
.
if s = 2 or s = 1
mn[i] = 2
else
mn[i] = 1
.
.
.
swap mn[] m[]
.
on timer
update
show
timer 0.5
.
show
timer 0.5
#
input_data
tH.........
. .
...
. .
Ht.. ......
</syntaxhighlight>
=={{header|Elena}}==
ELENA
<
import extensions;
import cellular;
const string sample =
" tH......
Line 1,298 ⟶ 1,742:
. ......
...Ht...";
const string conductorLabel = ".";
const string headLabel = "H";
const string tailLabel = "t";
const string emptyLabel = " ";
const int empty = 0;
const int conductor = 1;
const int electronHead = 2;
const int electronTail = 3;
wireWorldRuleSet = new RuleSet
{
int proceed(Space s, int x, int y
{
int cell := s.at(x, y);
cell =>
conductor
Line 1,321 ⟶ 1,765:
if (number == 1 || number == 2)
{
}
else
{
}
}
electronHead
{
}
electronTail
{
}
}
}
};
sealed class Model
{
Space theSpace;
constructor load(string stateString, int maxX, int maxY)
{
var strings := stateString.splitBy(
theSpace := IntMatrixSpace.allocate(maxX, maxY, RuleSet
{
int proceed(Space s, int x, int y
{
int retVal := 0;
if (x < strings.Length)
{
Line 1,373 ⟶ 1,818:
{
retVal := empty
};
^ retVal
}
})
}
run()
{
theSpace.update(wireWorldRuleSet)
}
print()
{
int columns := theSpace.Columns;
int rows := theSpace.Rows;
int i := 0;
int j := 0;
Line 1,394 ⟶ 1,840:
{
j := 0;
while (j < columns)
{
var label := emptyLabel;
int cell := theSpace.at(i, j);
cell =>
conductor { label := conductorLabel }
electronHead { label := headLabel }
electronTail { label := tailLabel };
console.write(label);
j := j + 1
};
i := i + 1;
console.writeLine()
Line 1,415 ⟶ 1,861:
}
}
public program()
{
Model model := Model.load(sample,10,30);
for(int i := 0
{
console.printLineFormatted("Iteration {0}",i);
model.print().run()
}
}</
{{out}}
<pre>
Line 1,487 ⟶ 1,933:
=={{header|Elixir}}==
{{trans|Ruby}}
<
@empty " "
@head "H"
Line 1,570 ⟶ 2,016:
"""
Wireworld.run(text)</
{{out}}
Line 1,841 ⟶ 2,287:
</pre>
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Wireworld. Nigel Galloway: January 22nd., 2024
type Cell= |E |T |H |C
let n=array2D [[T;H;C;C;C;C;C;C;C;C;C];
[C;E;E;E;C;E;E;E;E;E;E];
[E;E;E;C;C;C;E;E;E;E;E];
[C;E;E;E;C;E;E;E;E;E;E];
[H;T;C;C;E;C;C;C;C;C;C]]
let fG n g=match n|>Seq.sumBy(fun n->match Array2D.get g (fst n) (snd n) with H->1 |_->0) with 1 |2->H |_->C
let fX i=i|>Array2D.mapi(fun n g->function |E->E |H->T |T->C |C->fG (Seq.allPairs [max 0 (n-1)..min (n+1) (Array2D.length1 i-1)] [max 0 (g-1)..min (g+1) (Array2D.length2 i-1)]) i)
Seq.unfold(fun n->Some(n,fX n))n|>Seq.take 15|>Seq.iteri(fun n g->printfn "%d:\n%A\n" n g)
</syntaxhighlight>
{{out}}
after 7: 8,9,10 are repeated as 11,12,13 then 14,15,16 etc
<pre>
0:
[[T; H; C; C; C; C; C; C; C; C; C]
[C; E; E; E; C; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[C; E; E; E; C; E; E; E; E; E; E]
[H; T; C; C; E; C; C; C; C; C; C]]
1:
[[C; T; H; C; C; C; C; C; C; C; C]
[H; E; E; E; C; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[H; E; E; E; C; E; E; E; E; E; E]
[T; C; C; C; E; C; C; C; C; C; C]]
2:
[[H; C; T; H; C; C; C; C; C; C; C]
[T; E; E; E; C; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[T; E; E; E; C; E; E; E; E; E; E]
[C; H; C; C; E; C; C; C; C; C; C]]
3:
[[T; H; C; T; H; C; C; C; C; C; C]
[C; E; E; E; H; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[C; E; E; E; C; E; E; E; E; E; E]
[H; T; H; C; E; C; C; C; C; C; C]]
4:
[[C; T; H; C; T; H; C; C; C; C; C]
[H; E; E; E; T; E; E; E; E; E; E]
[E; E; E; H; H; H; E; E; E; E; E]
[H; E; E; E; C; E; E; E; E; E; E]
[T; C; T; H; E; C; C; C; C; C; C]]
5:
[[H; C; T; H; C; T; H; C; C; C; C]
[T; E; E; E; C; E; E; E; E; E; E]
[E; E; E; T; T; T; E; E; E; E; E]
[T; E; E; E; C; E; E; E; E; E; E]
[C; H; C; T; E; C; C; C; C; C; C]]
6:
[[T; H; C; T; H; C; T; H; C; C; C]
[C; E; E; E; H; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[C; E; E; E; C; E; E; E; E; E; E]
[H; T; H; C; E; C; C; C; C; C; C]]
7:
[[C; T; H; C; T; H; C; T; H; C; C]
[H; E; E; E; T; E; E; E; E; E; E]
[E; E; E; H; H; H; E; E; E; E; E]
[H; E; E; E; C; E; E; E; E; E; E]
[T; C; T; H; E; C; C; C; C; C; C]]
8:
[[H; C; T; H; C; T; H; C; T; H; C]
[T; E; E; E; C; E; E; E; E; E; E]
[E; E; E; T; T; T; E; E; E; E; E]
[T; E; E; E; C; E; E; E; E; E; E]
[C; H; C; T; E; C; C; C; C; C; C]]
9:
[[T; H; C; T; H; C; T; H; C; T; H]
[C; E; E; E; H; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[C; E; E; E; C; E; E; E; E; E; E]
[H; T; H; C; E; C; C; C; C; C; C]]
10:
[[C; T; H; C; T; H; C; T; H; C; T]
[H; E; E; E; T; E; E; E; E; E; E]
[E; E; E; H; H; H; E; E; E; E; E]
[H; E; E; E; C; E; E; E; E; E; E]
[T; C; T; H; E; C; C; C; C; C; C]]
11:
[[H; C; T; H; C; T; H; C; T; H; C]
[T; E; E; E; C; E; E; E; E; E; E]
[E; E; E; T; T; T; E; E; E; E; E]
[T; E; E; E; C; E; E; E; E; E; E]
[C; H; C; T; E; C; C; C; C; C; C]]
12:
[[T; H; C; T; H; C; T; H; C; T; H]
[C; E; E; E; H; E; E; E; E; E; E]
[E; E; E; C; C; C; E; E; E; E; E]
[C; E; E; E; C; E; E; E; E; E; E]
[H; T; H; C; E; C; C; C; C; C; C]]
13:
[[C; T; H; C; T; H; C; T; H; C; T]
[H; E; E; E; T; E; E; E; E; E; E]
[E; E; E; H; H; H; E; E; E; E; E]
[H; E; E; E; C; E; E; E; E; E; E]
[T; C; T; H; E; C; C; C; C; C; C]]
14:
[[H; C; T; H; C; T; H; C; T; H; C]
[T; E; E; E; C; E; E; E; E; E; E]
[E; E; E; T; T; T; E; E; E; E; E]
[T; E; E; E; C; E; E; E; E; E; E]
[C; H; C; T; E; C; C; C; C; C; C]]
</pre>
=={{header|Forth}}==
<
8 constant h
Line 1,922 ⟶ 2,489:
: gen ['] newrow foreachrow age ;
: wireworld begin gen 0 0 at-xy show key? until ;</
{{out}}
Line 2,047 ⟶ 2,614:
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
<
implicit none
Line 2,131 ⟶ 2,698:
end do
end subroutine Nextgen
end program Wireworld</
{{out}}
<pre style="height:30ex;overflow:scroll">Generation 0
Line 2,225 ⟶ 2,792:
=={{header|FreeBASIC}}==
<
#define MAXY 199
Line 2,294 ⟶ 2,861:
buffer = 1 - buffer
active = 1 - buffer
loop</
=={{header|GML}}==
Only visual output. Not an all-out simulator, but has some functions not on by default.
<
/*
Wireworld first declares constants and then reads a wireworld from a textfile.
Line 2,396 ⟶ 2,963:
ds_grid_destroy(gridid);
ds_list_destroy(listid);
}</
Now the step event
<syntaxhighlight lang="gml">
//Step event
/*
Line 2,617 ⟶ 3,184:
}
}
</syntaxhighlight>
Now the draw event
<syntaxhighlight lang="gml">
//Draw event
/*
Line 2,682 ⟶ 3,249:
}
draw_set_color(c_black);
</syntaxhighlight>
=={{header|Go}}==
Text output. Press Enter to compute and display successive generations.
<
import (
Line 2,775 ⟶ 3,342:
}
}
}</
=={{header|Haskell}}==
<
import Control.Monad
import Control.Arrow
Line 2,798 ⟶ 3,365:
noH = length $ filter (=='H') $ concat xs
runCircuit = iterate (map(map nwState).take3x3)</
Example executed in GHCi:
<
". ....",
" .. "
]
example = mapM_ (mapM_ putStrLn) .map (borden ' ').take 9 $ runCircuit oscillator</
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 2,861 ⟶ 3,428:
This simulation starts in single step mode and can be switched to run uninterrupted. The window can be saved at any point in single step mode.
This uses 1 pixel per cell so this animation looks tiny. Also the orientation has been flipped.
<
$define EDGE -1
Line 2,952 ⟶ 3,519:
}
}
end</
{{libheader|Icon Programming Library}}
Line 2,958 ⟶ 3,525:
=={{header|J}}==
The example circuit:<
tH........
. .
Line 2,964 ⟶ 3,531:
. .
Ht.. .....
)</
A 'boarding' verb board and the next cell state verb nwS:
<
nwS=: 3 : 0
Line 2,972 ⟶ 3,539:
if. ('.'=e)*. e.&1 2 +/'H'=,y do. 'H' return. end.
' t..' {~ ' Ht.' i. e
)</
The 'most' powerful part is contained in the following iterating sentence, namely the dyad cut ;. [http://www.jsoftware.com/help/dictionary/d331.htm ]. In this way verb nwS can work on all the 3x3 matrices containing each cell surrounded by its 8 relevant neighbors.
<
(<10) process circuit</
Example run:
<pre style="height:30ex;overflow:scroll">
Line 3,042 ⟶ 3,609:
Note also that a graphical presentation can be achieved using viewmat. For example:
<
viewmat"2 ' .tH'i. (<10) process circ0</
(This example opens 10 windows, one for each generation.)
Line 3,059 ⟶ 3,626:
<input type='file' accept='text/plain' onchange='openFile( event )' />
<br /></body></html></pre>
<
var ctx, sizeW, sizeH, scl = 10, map, tmp;
function getNeighbour( i, j ) {
Line 3,148 ⟶ 3,715:
simulate();
}
</syntaxhighlight>
=={{header|jq}}==
Line 3,160 ⟶ 3,727:
* For speed, the simulation uses the exploded string (an array).
* The ASCII values of the symbols used to display the state are hardcoded.
<
def cols: split("\n")[0]|length + 1; # allow for the newline
Line 3,196 ⟶ 3,763:
$world | evolve($i; $w) as $next
| if .[$i] == $next then . else .[$i] = $next end )
| [., $lines, $w] ; # </
'''Animation'''
<
def cls: "\u001b[2J";
Line 3,223 ⟶ 3,790:
# Input: a string representing the initial state
def frames(n): animation(n; -1);#</
'''Examples''':
<
"+-----------+\n" +
"|tH.........|\n" +
Line 3,239 ⟶ 3,806:
" . .... \n" +
" .. \n" +
" \n" ;</
'''Illustration 1''':
<
world9 | animation(10; 1000)</
'''Illustration 2''':
<
world11 | frames(10)</
To run: jq -n -r -f wireworld.rc
=={{header|Julia}}==
<
h, w = size(b)
[b[x,y] for x in i-1:i+1, y in j-1:j+1 if (0 < x <= h && 0 < y <= w)]
Line 3,292 ⟶ 3,859:
mat .= mat2
end
</
Starting Wireworld board:
Line 3,350 ⟶ 3,917:
=={{header|Liberty BASIC}}==
[[File:AnimWW.gif]]
<syntaxhighlight lang="lb">
WindowWidth = 840
WindowHeight = 600
Line 3,480 ⟶ 4,047:
timer 1000, [tmr]
wait
</syntaxhighlight>
=={{header|Logo}}==
{{Works with|MSWlogo}}
(The wireworld given in the file must be bounded by spaces for the program to work. Also it is notable that the program takes the width as the longest of the lines.)
<
Make "speed 60/:speed
wireworldread :filename
Line 3,553 ⟶ 4,120:
if (mditem (list :y-1 :x-1) :field)=:thing [Make "neighbours :neighbours+1]
ifelse OR :neighbours=1 :neighbours=2 [op "true] [op "false]
end</
=={{header|Lua}}==
If ran using [[L%C3%96VE]], it will animate the simulation on a window. Otherwise it will print the first 10 steps on the console.
<syntaxhighlight lang="lua">
local map = {{'t', 'H', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', ' ', ' ', ' ', '.'},
Line 3,627 ⟶ 4,194:
end
end
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ArrayPad[PadRight[Characters /@ StringSplit["tH.........
. .
Line 3,643 ⟶ 4,210:
3, {{a_, b_, c_}, {d_, 3, e_}, {f_, g_, h_}} :>
Switch[Count[{a, b, c, d, e, f, g, h}, 1], 1, 1, 2, 1, _, 3]},
data], ColorRules -> {1 -> Yellow, 2 -> Red}]]</
=={{header|MiniScript}}==
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro].
<syntaxhighlight lang="miniscript">
colors = [color.black, color.yellow, color.aqua, color.red]
deltas = [[-1,-1], [-1,0], [-1,1],
[ 0,-1], [ 0,1],
[ 1,-1], [ 1,0], [ 1,1]]
displayGrid = function(grid, td)
for y in range(0, grid.len - 1)
for x in range(0, grid[0].len - 1)
td.setCell x,y, grid[y][x]
end for
end for
end function
buildGrid = function(s)
lines = s.split(char(13))
nRows = lines.len
nCols = 0
for line in lines
if line.len > nCols then nCols = line.len
end for
grid = []
emptyRow = []
for c in range(1,nCols)
emptyRow.push(0)
end for
for line in lines
row = emptyRow[:]
for i in range(0, line.len - 1)
row[i] = " .Ht".indexOf(line[i])
end for
grid.push(row)
end for
return grid
end function
getNewState = function(td, x, y)
cellState = td.cell(x, y)
if cellState == 3 then
return 1
else if cellState == 2 then
return 3
else if cellState == 1 then
sum = 0
for delta in deltas
x1 = x + delta[0]
y1 = y + delta[1]
if td.cell(x1, y1) == 2 then sum += 1
end for
if sum == 1 or sum == 2 then
return 2
else
return 1
end if
end if
return cellState
end function
clear
wireWorldProgram = "tH........." + char(13)
wireWorldProgram += ". ." + char(13)
wireWorldProgram += " ..." + char(13)
wireWorldProgram += ". ." + char(13)
wireWorldProgram += "Ht.. ......"
grid = buildGrid(wireWorldProgram)
// Prepare a tile display
// Generate image used for the tiles from the colors defined above.
img = Image.create(colors.len, 1);
for i in range(0, colors.len - 1)
img.setPixel(i, 0, colors[i])
end for
cols = grid[0].len
rows = grid.len
display(4).mode = displayMode.tile
td = display(4)
cSize = 25
td.cellSize = cSize // size of cells on screen
td.scrollX = -(960 - cols * (cSize + 1)) / 2
td.scrollY = -(640 - rows * (cSize + 1)) / 2
td.extent = [cols, rows]
td.overlap = -1 // adds a small gap between cells
td.tileSet = img; td.tileSetTileSize = 1
td.clear 0
while true
displayGrid(grid, td)
for y in range(0, rows - 1)
for x in range(0, cols - 1)
grid[y][x] = getNewState(td, x, y)
end for
end for
wait 0.5
end while
</syntaxhighlight>
=={{header|Nim}}==
{{trans|C}}
<
var world, world2 = """
Line 3,657 ⟶ 4,325:
|Ht.. ......|
+-----------+"""
let h = world.splitLines.len
let w = world.splitLines[0].len
template isH(x, y): int = int(s[i + w * y + x] == 'H')
proc next(o: var string, s: string, w: int) =
Line 3,682 ⟶ 4,351:
world2.next(world, w)
swap world, world2</
=={{header|OCaml}}==
<
" ......tH ";
" . ...... ";
Line 3,741 ⟶ 4,410:
aux n
in
aux w</
=={{header|Oz}}==
Includes a simple animation, using a text widget.
<
Rules =
[rule(& & )
Line 3,836 ⟶ 4,505:
{Field set(text:{ShowArena Gi})}
{Delay 500}
end</
=={{header|PARI/GP}}==
<
wireworldStep(M)={
my(sz=matsize(M),t);
Line 3,869 ⟶ 4,538:
)
};
animate(read("wireworld.gp"))</
=={{header|Perl}}==
Read the initial World from stdin and print 10 steps to stdout
<
for (1 .. 10) {
Line 3,893 ⟶ 4,562:
}
@f = (@a,[]);
}</
Input:
<pre>tH.........
Line 3,974 ⟶ 4,643:
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Wireworld.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Wireworld.exw
-- ==========================
--
-- Invoke with file to read, or let it read the one below (if compiled assumes source is in the same directory)
--
-- Note that tabs in description files are not supported - where necessary spaces can be replaced with _ chars.
-- (tab chars in text files should technically always represent (to-next) 8 spaces, but not many editors respect
-- that, and instead assume the file will only ever be read by the same program/with matching settings. </rant>)
-- (see also demo\edix\src\tabs.e\ExpandTabs() for what you'd need if you knew what the tab chars really meant.)
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">default_description</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
tH.........
.___.
___...
.___.
Ht.. ......
"""</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">counts</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">longest</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">valid_line</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" _.tH"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'\t'</span> <span style="color: #008080;">then</span>
<span style="color: #000080;font-style:italic;">-- as above</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"error: tab char on line %d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">load_desc</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">text</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">default_description</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">filename</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">command_line</span><span style="color: #0000FF;">()[$],</span><span style="color: #008000;">".exe"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">".exw"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"error opening %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">filename</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">abort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #004600;">GT_LF_STRIPPED</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">valid_line</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">line</span><span style="color: #0000FF;">}</span>
<span style="color: #000000;">longest</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">valid_line</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">longest</span><span style="color: #0000FF;"><</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">longest</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">counts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">dxy</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{+</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{+</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span> <span style="color: #0000FF;">{+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">set_counts</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dxy</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">({</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">},</span><span style="color: #000000;">dxy</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;"><=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">][</span><span style="color: #000000;">cx</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'H'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">or</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">title</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Wireworld"</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">timer</span>
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w</span><span style="color: #0000FF;">/(</span><span style="color: #000000;">longest</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dy</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h</span><span style="color: #0000FF;">/(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">set_counts</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">])</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">colour</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" _"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">CD_BLACK</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">CD_YELLOW</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">counts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'H'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'H'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">CD_BLUE</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'t'</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'t'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">colour</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">CD_RED</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">y</span><span style="color: #0000FF;">][</span><span style="color: #000000;">x</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'.'</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">colour</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasBox</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">-</span><span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">,</span><span style="color: #000000;">h</span><span style="color: #0000FF;">-(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">*</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">+</span><span style="color: #000000;">dy</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">timer_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_IGNORE</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLACK</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">load_desc</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=300x180"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallbacks</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">),</span>
<span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">)})</span>
<span style="color: #000000;">timer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupTimer</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"timer_cb"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">500</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="%s"`</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">title</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
=={{header|PHP}}==
<syntaxhighlight lang="php">
$desc = 'tH.........
. .
Line 4,237 ⟶ 4,918:
draw_world($world);
};
</syntaxhighlight>
=={{header|PicoLisp}}==
This example uses 'grid' from "lib/simul.l", which maintains a two-dimensional
structure.
<
(let
Line 4,277 ⟶ 4,958:
(for This Col
(=: val (: next)) ) )
(prinl) ) )</
{{out}}
<pre> +---+---+---+---+---+---+---+---+---+---+---+
Line 4,320 ⟶ 5,001:
=={{header|PureBasic}}==
===Standalone version===
<
#Empty
#Electron_head
Line 4,434 ⟶ 5,115:
Data.i 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data.i 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
EndDataSection</
===Load from external source, graphical presentations===
<
CompilerError "The file handling in this small program is only in ASCII."
CompilerEndIf
Line 4,580 ⟶ 5,261:
EndIf
Until Event=#PB_Event_CloseWindow
EndIf</
Example of data file to load
<pre>; Save as "WireWorld.txt"
Line 4,638 ⟶ 5,319:
=={{header|Python}}==
<
Wireworld implementation.
'''
Line 4,707 ⟶ 5,388:
print ( ("\n%3i " % gen) + '=' * (ww.w-4) + '\n' )
print ( world2string(ww) )
ww = nextgen(ww)</
{{out}}
Line 4,793 ⟶ 5,474:
=={{header|Racket}}==
<
#lang racket
(require 2htdp/universe)
Line 4,901 ⟶ 5,582:
(run-wire-world #:initial-state initial-list)
</syntaxhighlight>
=={{header|Raku}}==
Line 4,907 ⟶ 5,588:
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku"
has @.line;
method height
method width returns Int { max @!line».chars }
multi method new(@line) { samewith :@line
multi method new($str ) { samewith $str.lines }
Line 4,930 ⟶ 5,611:
for ^$.height X ^$.width -> ($i, $j) {
@succ[$i] ~=
do given @
when 'H' { 't' }
when 't' { '.' }
Line 4,959 ⟶ 5,640:
Bool :$stop-on-repeat,
) {
run-loop
Q:to/§/
.
...
. .
Ht.. ......
§
}
Line 4,975 ⟶ 5,660:
my %seen is SetHash;
print "\e7"; # save cursor position
for $initial ...^ * eqv * { # generate a sequence (uses .succ)
last if $stop-on-repeat and %seen{ .gist }++;
}
}</
When run with <code>--stop-on-repeat</code>
{{out}}
<pre>
t .
ttt
t .
.H.t ......</pre>
=={{header|REXX}}==
<
parse arg iFID . '(' generations rows cols bare head tail wire clearScreen reps
if iFID=='' then iFID= "WIREWORLD.TXT" /*should default input file be used? */
Line 5,059 ⟶ 5,737:
if reps\==0 & #reps<=reps then return /*so far, so good, no reps.*/
say '"Wireworld" repeated itself' reps "times, the program is stopping."
signal done /*jump to this pgm's "exit".*/</
Programming note: the '''hood''' subroutine (above) could be optimized for speed by setting some short-circuit values <code>('''r-1''', '''c-1''', '''r+1''', and '''c+1''') </code> and using those values in the subsequent expressions.
Line 5,155 ⟶ 5,833:
═══════════════════════════════════════════════════════════════════════════════════════13
"Wireworld" repeated itself 2 times, the program is stopping.
</pre>
=={{header|RISC-V Assembly}}==
<syntaxhighlight lang="risc-v">
/* gnu assembler syntax */
wireworld:
/* unsigned int width (a0) */
/* unsigned int height (a1) */
/* char* grid (a2) */
mv a4,a2
li t4,'. /* conductor */
li t5,'H /* head */
li t6,'t /* tail */
addi t2,a0,-1
addi t3,a1,-1
mv t1,zero
.yloop: /* outer loop (y) */
mv t0,zero
.xloop: /* inner loop (x) */
lb a5,0(a4)
bgt a5,t4,.torh
blt a5,t4,.empty
/* conductor: */
/* unsigned int head_count (a3) */
/* char* test_ptr (a6) */
/* char test (a7) */
mv a3,zero
sub a6,a4,a0
addi a6,a6,-1
0: beq t1,zero,1f /* bounds up */
beq t0,zero,0f /* bounds left */
lb a7,0(a6)
bne a7,t6,0f
addi a3,a3,1
0: lb a7,1(a6)
bne a7,t6,0f
addi a3,a3,1
0: beq t0,t2,0f /* bounds right */
lb a7,2(a6)
bne a7,t6,0f
addi a3,a3,1
0:1: add a6,a6,a0
beq t0,zero,0f /* bounds left */
lb a7,0(a6)
bne a7,t6,0f
addi a3,a3,1
0: beq t0,t2,0f /* bounds right */
lb a7,2(a6)
bne a7,t5,0f
addi a3,a3,1
0: add a6,a6,a0
beq t1,t3,1f /* bounds down */
beq t0,zero,0f /* bounds left */
lb a7,0(a6)
bne a7,t5,0f
addi a3,a3,1
0: lb a7,1(a6)
bne a7,t5,0f
addi a3,a3,1
0: beq t0,t2,0f /* bounds right */
lb a7,2(a6)
bne a7,t5,0f
addi a3,a3,1
0:1: beq a3,zero,.empty
addi a3,a3,-2
bgt a3,zero,.empty
mv a5,t5 /* convert conductor to electron head */
j .save
.torh: beq a5,t6,.tail
.head: mv a5,t6
j .save
.tail: mv a5,t4
.save: sb a5,0(a4)
.empty: /* do nothing */
/* end x-loop */
addi a4,a4,1
addi t0,t0,1
bne t0,a0,.xloop
/* end y-loop */
addi t1,t1,1
bne t1,a1,.yloop
ret
</syntaxhighlight>
For output, compile the above to an object file and link against the following program:
<syntaxhighlight lang="c">
#include <stdio.h>
#include <string.h>
char init[] = " tH....tH "
" . ...... "
" ........ . "
" .. .... .. .. .. "
".. ... . ..tH....tH... .tH..... ....tH.. .tH."
" .. .... .. .. .. "
" tH...... . "
" . ....tH "
" ...Ht... ";
int width = 60;
int height = 9;
void wireworld(unsigned int, unsigned int, char *);
int main() {
char tmp[width + 1] = {};
do {
for (int i = 0; i < height; i++) {
strncpy(tmp, init + i * width, width);
puts(tmp);
}
wireworld(width, height, init);
} while (getchar());
return 0;
}
</syntaxhighlight>
Example output:
<pre style="height:60ex">
tH....tH
. ......
........ .
.. .... .. .. ..
.. ... . ..tH....tH... .tH..... ....tH.. .tH.
.. .... .. .. ..
tH...... .
. ....tH
...Ht...
.tH....t
. H.....
........ .
.. .... .. .. ..
.. ... . ...tH....tH.. ..tH.... .....tH. ..tH
.. .... .. .. ..
.tH..... H
. .....t
..Ht....
..tH....
. tH....
.......H .
.. .... .. .. H.
.. ... . ....tH....tH. ...tH... ......tH ...t
.. HHH. .. .. H.
..tH.... t
. ......
.Ht.....
...tH...
. .tH...
......Ht .
.. .... H. .. tH
.. ... H H....tH....tH ....tH.. .......t ....
.. tttH H. .. tH
...tH... .
. ......
Ht......
....tH..
. ..tH..
.....Ht. .
.. HHHH tH .. .t
.. ... t tH....tH....t .....tH. ........ H...
.. ...t tH .. .t
....tH.. .
H ......
t.......
.....tH.
. ...tH.
....Ht.. .
.. tttt .t H. ..
.. ... . .tH....tH.... H.....tH ........ tH..
.. .... .t H. ..
H....tH. .
t ......
........
......tH
. ....tH
...Ht... .
.. .... .. tH ..
.. ... . ..tH....tH... tH.....t ........ .tH.
.. .... .. tH ..
tH....tH .
. ......
........
.......t
. H....t
..Ht.... H
.. .... .. .t ..
.. ... . ...tH....tH.. .tH..... H....... ..tH
.. .... .. .t ..
.tH....t .
. H.....
........
........
. tH....
.Ht....H t
.. HHH. .. .. ..
.. ... . ....tH....tH. ..tH.... tH...... ...t
.. .... .. .. ..
..tH.... .
. tH....
.......H
........
. .tH...
Ht....Ht .
.. tttH H. .. ..
.. ... H H....tH....tH ...tH... .tH..... ....
.. .... H. .. ..
...tH... .
. .tH...
......Ht
........
H ..tH..
t....Ht. .
.. ...t tH .. ..
.. ... t tH....tH....t ....tH.. ..tH.... ....
.. HHHH tH .. ..
....tH.. .
. ..tH..
.....Ht.
H.......
t ...tH.
....Ht.. .
.. .... .t .. ..
.. ... . .tH....tH.... H....tH. ...tH... ....
.. tttt .t .. ..
.....tH. .
. ...tH.
....Ht..
tH......
. ....tH
...Ht... .
.. .... .. H. ..
.. ... . ..tH....tH... tH....tH ....tH.. ....
.. .... .. H. ..
......tH .
. ....tH
...Ht...
.tH.....
. .....t
..Ht.... H
.. .... .. tH ..
.. ... . ...tH....tH.. .tH....t .....tH. ....
.. .... .. tH ..
.......t H
. H....t
..Ht....
..tH....
. ......
.Ht..... t
.. HHH. .. .t H.
.. ... . ....tH....tH. ..tH.... H.....tH ....
.. HHH. .. .t H.
........ t
. tH....
.Ht....H
...tH...
. ......
Ht...... .
.. tttH H. .. tH
.. ... . H....tH....tH ...tH... tH.....t ....
.. tttH H. .. tH
........ .
. .tH...
Ht....Ht
....tH..
H ......
t....... .
.. ...t tH .. .t
.. ... . t.....tH....t ....tH.. .tH..... H...
.. ...t tH .. .t
........ .
H ..tH..
t....Ht.
H....tH.
t ......
........ .
.. .... .t .. ..
.. ... . .......tH.... H....tH. ..tH.... tH..
.. .... .t .. ..
H....... .
t ...tH.
....Ht..
tH....tH
. ......
........ .
.. .... .. H. ..
.. ... . ........tH... tH....tH ...tH... .tH.
.. .... .. H. ..
tH...... .
. ....tH
...Ht...
.tH....t
. H.....
........ .
.. .... .. tH ..
.. ... . .........tH.. .tH....t ....tH.. ..tH
.. .... .. tH ..
.tH..... H
. .....t
..Ht....
</pre>
Line 5,161 ⟶ 6,183:
=={{header|Rust}}==
<
#[derive(Debug, Copy, Clone, PartialEq)]
Line 5,258 ⟶ 6,280:
Ok(world)
}
}</
graphical output using winit 0.24 and pixels 0.2
<
use winit::event::*;
use winit::event_loop::{ControlFlow, EventLoop};
Line 5,332 ⟶ 6,354:
}
});
}</
=={{header|Sidef}}==
{{trans|Perl}}
<
10.times {
say f.map { .join(" ") + "\n" }.join
var a = [[]]
for y in (1 ..^ f.end
var r = f[y]
var rr = ['']
for x in (1 ..^ r.end
var c = r[x]
rr << (
given(c) {
when('H') { 't' }
when('t') { '.' }
when('.') { <. H>[[f
default { c }
}
)
}
rr << ''
a << rr
}
f = [a..., []]
}
Line 5,366 ⟶ 6,388:
...
. .
Ht.. ......</
=={{header|Smalltalk}}==
Line 5,372 ⟶ 6,394:
=={{header|Standard ML}}==
<
type world = char vector vector
Line 5,417 ⟶ 6,439:
" ... ",
". . ",
"Ht.. ......"]</
=={{header|Tcl}}==
Line 5,427 ⟶ 6,449:
to construct a two dimensional 3 by 3 sliding window.
The rule function maps a pair (cell,neighborhood) to a new cell.
<
rule = case~&l\~&l {`H: `t!, `t: `.!,`.: @r ==`H*~; {'H','HH'}?</`H! `.!}
Line 5,433 ⟶ 6,455:
neighborhoods = ~&thth3hthhttPCPthPTPTX**K7S+ swin3**+ swin3@hNSPiCihNCT+ --<0>*+ 0-*
evolve "n" = @iNC ~&x+ rep"n" ^C\~& rule**+ neighborhoods@h</
test program:
<
<
Line 5,444 ⟶ 6,466:
#show+
example = mat0 evolve13 diode</
{{out}}
<pre style="height:15ex;overflow:scroll">
Line 5,502 ⟶ 6,524:
......... ...
..
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|wren-fmt}}
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./ioutil" for FileUtil, Stdin
var rows = 0 // extent of input configuration
var cols = 0 // """
var rx = 0 // grid extent (includes border)
var cx = 0 // """
var mn = [] // offsets of Moore neighborhood
var print = Fn.new { |grid|
System.print("__" * cols)
System.print()
for (r in 1..rows) {
for (c in 1..cols) Fmt.write(" $s", grid[r*cx+c])
System.print()
}
}
var step = Fn.new { |dst, src|
for (r in 1..rows) {
for (c in 1..cols) {
var x = r*cx + c
dst[x] = src[x]
if (dst[x] == "H") {
dst[x] = "t"
} else if (dst[x] == "t") {
dst[x] = "."
} else if (dst[x] == ".") {
var nn = 0
for (n in mn) {
if (src[x+n] == "H") nn = nn + 1
}
if (nn == 1 || nn == 2) dst[x] = "H"
}
}
}
}
var srcRows = FileUtil.readLines("ww.config")
rows = srcRows.count
for (r in srcRows) {
if (r.count > cols) cols = r.count
}
rx = rows + 2
cx = cols + 2
mn = [-cx-1, -cx, -cx+1, -1, 1, cx-1, cx, cx+1]
// allocate two grids and copy input into first grid
var odd = List.filled(rx*cx, " ")
var even = List.filled(rx*cx, " ")
var ri = 0
for (r in srcRows) {
for (i in 0...r.count) {
odd[(ri+1)*cx+1+i] = r[i]
}
ri = ri + 1
}
// run
while (true) {
print.call(odd)
step.call(even, odd)
Stdin.readLine() // wait for enter to be pressed
print.call(even)
step.call(odd, even)
Stdin.readLine() // ditto
}</syntaxhighlight>
{{out}}
<pre>
Although not shown, same as Go output.
</pre>
=={{header|XPL0}}==
[[File:WireXPL0.gif|right]]
<
char New(53,40), Old(53,40);
Line 5,553 ⟶ 6,654:
until KeyHit; \keystroke terminates program
SetVid(3); \restore normal text mode
]</
=={{header|Yabasic}}==
<
backcolor 0,0,0
clear window
Line 5,617 ⟶ 6,718:
wait .5
loop
</syntaxhighlight>
|