Wireworld: Difference between revisions

Content deleted Content added
MaiconSoft (talk | contribs)
Added Delphi example
Alextretyak (talk | contribs)
Added 11l
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}}
 
<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.. ......’
 
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 (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 (newworld, width, height)
 
F world2string(ww)
R ww[0][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[1] - 4))"\n")
print(world2string(ww))
ww = nextgen(ww)</lang>
 
{{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}}==