Wireworld: Difference between revisions

m
m (→‎{{header|REXX}}: added/changed comments, statements, and whitespace, used a template for the output section.)
Line 3,181:
 
To run: jq -n -r -f wireworld.rc
 
=={{header|Julia}}==
<lang julia>function surround2D(b, i, j)
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)]
end
 
surroundhas1or2(b, i, j) = 0 < sum(map(x->Char(x)=='H', surround2D(b, i, j))) <= 2 ? 'H' : '.'
function boardstep!(currentboard, nextboard)
x, y = size(currentboard)
for j in 1:y, i in 1:x
ch = Char(currentboard[i, j])
if ch == ' '
continue
else
nextboard[i, j] = (ch == 'H') ? 't' : (ch == 't' ? '.' :
surroundhas1or2(currentboard, i, j))
end
end
end
 
const b1 = " " *
" tH " *
" . .... " *
" .. " *
" "
const mat = reshape(map(x->UInt8(x[1]), split(b1, "")), (9, 5))'
const mat2 = copy(mat)
 
function printboard(mat)
for i in 1:size(mat)[1]
println("\t", join([Char(c) for c in mat[i,:]], ""))
end
end
 
println("Starting Wireworld board:")
printboard(mat)
for step in 1:8
boardstep!(mat, mat2)
println(" Step $step:")
printboard(mat2)
mat .= mat2
end
</lang>{{output}}<pre>
Starting Wireworld board:
 
tH
. ....
..
 
Step 1:
 
.t
. H...
..
 
Step 2:
 
..
. tH..
.H
 
Step 3:
 
..
. .tH.
Ht
 
Step 4:
 
..
H ..tH
t.
 
Step 5:
 
H.
t ...t
..
 
Step 6:
 
tH
. ....
..
 
Step 7:
 
.t
. H...
..
 
Step 8:
 
..
. tH..
.H
</pre>
 
 
=={{header|Liberty BASIC}}==
Line 3,315 ⟶ 3,415:
wait
</lang>
 
=={{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.
4,105

edits