Langton's ant: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|APL}}: Correct coordinate chirality)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(19 intermediate revisions by 8 users not shown)
Line 28:
 
;Related task:
*   Rosetta Code:   [[Conway%27s_Game_of_Life|Conway's Game of Life]].
*   [[Elementary_cellular_automaton|Elementary cellular automaton]]
<br><br>
 
Line 502 ⟶ 503:
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">langton ← {⍺←⍵ ⋄ grid←⍺ ⍵⍴0 ⋄ ant←2÷⍨⍺ ⍵ ⋄ dir←?4 ⋄ grid ant dir}
⍝ initialize a Langton's Ant setup with a grid of size left x right (square by default)
step ← {
langton ← {
grid ant dir←⍵
⍝ If rows not specified, set equal to columns
dir←1+4|dir+2×grid[⊂ant]
⍺ ← ⍵
grid[⊂ant]←1-grid[⊂ant]
 
⍝ coordinates are matrix order: row,col. So up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
⍝ 0=white, 1=black. Start with all white
ant+←(4 2⍴¯1 0 0 1 1 0 0 ¯1)[dir;]
grid grid ant dir0
 
}
to watch it run, openStart the variable picant in the monitor before executing this stepmiddle
ant ← 2 ÷⍨ ⍺ ⍵
{} { state ∘← ⍵ ⋄ pic ∘← '.⌺'[1+⊃1⌷⍵] ⋄ _←⎕dl ÷500 ⋄ step ⍵} ⍣≡ langton 100</syntaxhighlight>
 
⍝ Aimed in a random direction
dir ← ?4
 
⍝ return everything in a tuple
grid ant dir
}
 
⍝ iterate one step: takes and returns state as created by langton function
step ← {
grid ant dir ← ⍵
 
⍝ Turn left or right based on grid cell
dir ← 1 + 4|dir+2×grid[⊂ant]
 
⍝ Toggle cell color
grid[⊂ant] ← 1 - grid[⊂ant]
 
⍝ Advance along dir. Since coordinates are matrix order (row,col),
⍝ up is -1 0, right is 0 1, down is 1 0, and left is 0 -1
ant +← (4 2 ⍴ ¯1 0, 0 1, 1 0, 0 ¯1)[dir;]
 
grid ant dir
}
 
⍝ to watch it run, open the variable pic in the monitor before executing this step
{} { state ∘← ⍵ ⋄ pic ∘← '.⌺'[1+⊃1⌷⍵] ⋄ _←⎕dl ÷200 ⋄ step ⍵} ⍣≡ langton 100</syntaxhighlight>
 
{{Out}}
Line 595 ⟶ 623:
.............................⌺⌺.....................................................................
..............................⌺⌺....................................................................</pre>
 
=={{header|Applesoft BASIC}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="gwbasic"> 0 IF T THEN FOR Q = 0 TO T STEP 0: XDRAW T AT X * S,H - Y * S:D = FN M(D + D( PEEK (234)) + F):X = X + X(D):Y = Y + Y(D):Q = X > M OR X < 0 OR Y > M OR Y < 0: NEXT Q: END : DATA 100,50,50,3,220,1,4,-1,1,1,1,-1,-1
1 HGR : SCALE= 1: ROT= 0
2 LET S$ = CHR$ (1) + CHR$ (0) + CHR$ (4) + CHR$ (0) + "5'" + CHR$ (0)
3 POKE 236, PEEK (131): POKE 237, PEEK (132)
4 LET S = PEEK (236) + PEEK (237) * 256 + 1
5 POKE 232, PEEK (S)
6 POKE 233, PEEK (S + 1)
7 READ M,X,Y,S,H,T,F,D(0),D(4),Y(0),X(1),Y(2),X(3)
8 DEF FN M(N) = N - INT (N / F) * F
9 GOTO</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 2,228 ⟶ 2,269:
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=bZHLDoIwEEX3/Yq7VAlNa0wMifgjhIXhEUiUmopA/94ZKILBJn3k9M70zvReNCiTFFopHHgVT2syvCrTQ0IKAKWxcIih0BpEESNPhw2lUZeU0E3JEJAmgE5JqBcJjYfpCrpzP9AWWQu9Vkqx2qWQkzv7bjgWeW1nk/mQ8CMJ+dE0Q410xM7jUPubEfdVfefnr+z/1uR0vIx++ewW7CbsTXSUaVva3I+/NSNE5wVsNsaOtwBH0nZ76kKOE4u9hgJjLoVEc143IvdF3ASu/6xwUlCCf0p8AA== Run it]
[https://easylang.online/ide/#run=len%20f%5B%5D%20100%2A100%0Afunc%20show%20.%20.%0Afor%20y%20range%20100%0Afor%20x%20range%20100%0Aif%20f%5By%2A100%2Bx%5D%3D1%0Amove%20x%20y%0Arect%201%201%0A.%0A.%0A.%0A.%0Afunc%20run%20x%20y%20dir%20.%20.%0Adx%5B%5D%3D%5B%200%201%200%20-1%20%5D%0Ady%5B%5D%3D%5B%20-1%200%201%200%20%5D%0Awhile%20x%20%3E%3D%200%20and%20x%20%3C%20100%20and%20y%20%3E%3D%200%20and%20y%20%3C%20100%0Av%3Df%5By%2A100%2Bx%5D%0Af%5By%2A100%2Bx%5D%3D1%20-%20v%0Adir%3D%28dir%2B1%2B2%2Av%29%20mod%204%0Ax%2B%3Ddx%5Bdir%5D%0Ay%2B%3Ddy%5Bdir%5D%0A.%0A.%0Acall%20run%2070%2040%200%0Acall%20show Run it]
 
<syntaxhighlight lang="text">len f[] 100 * 100
len f[] 100 * 100
func show . .
proc show . .
for y range 100
for xy = 0 rangeto 10099
iffor f[yx *= 1000 + x] =to 199
move xif f[y * 100 + x + 1] = 1
rect 1 1 move x y
rect 1 1
.
.
.
.
.
funcproc run x y dir . .
dx[] = [ 0 1 0 -1 ]
dy[] = [ -1 0 1 0 ]
while x >= 0 and x < 100 and y >= 0 and y < 100
v = f[y * 100 + x + 1]
f[y * 100 + x + 1] = 1 - v
dir = (dir + 1 + 2 * v) mod 4 + 1
x += dx[dir]
y += dy[dir]
.
.
call run 70 40 0
show
call show</syntaxhighlight>
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 3,819 ⟶ 3,862:
{ „direction” }
 
</syntaxhighlight>
 
=={{header|Peri}}==
<syntaxhighlight lang="peri">
###sysinclude standard.uh
###sysinclude system.uh
###sysinclude str.uh
###sysinclude X.uh
#g
$ff0000 sto szin1
$ffffff sto szin2
10 sto pausetime
//maxypixel 100 - sto YRES
//maxypixel 20 - sto YRES
//maxypixel 7 - sto YRES
//maxypixel 13 - sto YRES
maxypixel 20 - sto YRES
maxxpixel sto XRES
zero ant
// Az ant iránykódjai:
// 0 : fel
// 1 : le
// 2 : jobbra
// 3 : balra
@XRES 2 / sto antx // Az ant kezdeti koordinátái
@YRES 2 / sto anty
myscreen "Furor monitor" @YRES @XRES graphic // Create the graphic screen
."Kilépés: ESC\n"
 
{.. // infinite loop begins
myscreen @anty @antx getpixel // A pixel színe amin az ant ül épp
@szin2 == {
myscreen @anty @antx @szin1 setpixel // másik színre átállítjuk a pixelt
2 // Jobbra fog fordulni
}{
myscreen @anty @antx @szin2 setpixel // másik színre átállítjuk a pixelt
3 // Balra fog fordulni
}
// Elvégezzük az új koordináta beállítását:
sto direction
@ant 0 == @direction 2 == & { ++() antx @antx @XRES == { zero antx } 2 sto ant goto §beolvas }
@ant 0 == @direction 3 == & { @antx inv { @XRES -- sto antx }{ --() antx } 3 sto ant goto §beolvas }
@ant 1 == @direction 3 == & { ++() antx @antx @XRES == { zero antx } 2 sto ant goto §beolvas }
@ant 1 == @direction 2 == & { @antx inv { @XRES -- sto antx }{ --() antx } 3 sto ant goto §beolvas }
@ant 2 == @direction 2 == & { @anty inv { @YRES -- sto anty }{ --() anty } 1 sto ant goto §beolvas }
@ant 2 == @direction 3 == & { ++() anty @anty @YRES == { zero anty } 0 sto ant goto §beolvas }
@ant 3 == @direction 2 == & { ++() anty @anty @YRES == { zero anty } 0 sto ant goto §beolvas }
@ant 3 == @direction 3 == & { @anty inv { @YRES -- sto anty }{ --() anty } 1 sto ant goto §beolvas }
 
beolvas:
myscreen key !sto billkód @pausetime inv sleep $1b == {
."Made " {..} print ." generations.\n" {.>.} }
..}
myscreen inv graphic
end
{ „myscreen” }
{ „billkód” }
{ „pausetime” }
{ „XRES” }
{ „YRES” }
{ „szin1” }
{ „szin2” }
{ „ant” }
{ „antx” }
{ „anty” }
{ „direction” }
</syntaxhighlight>
 
Line 4,610 ⟶ 4,719:
| ($grid|length) as $height
| ($grid[0]|length) as $width
| reduce range(0;$height) as $i ("\u001BHu001B[H"; # ANSI code
. + reduce range(0;$width) as $j ("\n";
. + if $grid[$i][$j] then " " else "#" end ) );
Line 5,969 ⟶ 6,078:
 
</pre >
 
=={{header|make}}==
[[File:make_langton_ant.png|right]]
<syntaxhighlight lang="make"># Langton's ant Makefile
# netpbm is an ancient collection of picture file formats
# convert and display are from imagemagick
.PHONY: display
display: ant.png
display $<
ant.png: ant.pbm
convert $< $@
 
n9:=1 2 3 4 5 6 7 8 9
n100:=$(n9) $(foreach i,$(n9),$(foreach j,0 $(n9),$i$j)) 100
ndec:=0 $(n100)
ninc:=$(wordlist 2,99,$(n100))
$(foreach i,$(n100),$(eval row$i:=$(foreach j,$(n100),0)))
 
.PHONY: $(foreach i,$(ndec),row$i)
row0:
@echo >ant.pbm P1
@echo >>ant.pbm '#' Langton"'"s ant
@echo >>ant.pbm 100 100
rowrule=row$i: row$(word $i,$(ndec)); @echo >>ant.pbm $$($$@)
$(foreach i,$(n100),$(eval $(rowrule)))
ant.pbm: Makefile row100
@:
 
x:=50
y:=50
direction:=1
 
turn=$(eval direction:=$(t$(xy)$(direction)))
xy=$(word $x,$(row$y))
t01:=4
t02:=1
t03:=2
t04:=3
t11:=2
t12:=3
t13:=4
t14:=1
 
flip=$(eval row$y:=$(start) $(not$(xy)) $(end))
start=$(wordlist 1,$(word $x,$(ndec)),$(row$y))
not0:=1
not1:=0
end=$(wordlist $(word $x,$(ninc) 100),100,$(row$y))
 
step=$(eval $(step$(direction)))
step1=y:=$(word $y,exit $(n100))
step2=x:=$(word $x,$(ninc) exit)
step3=y:=$(word $y,$(ninc) exit)
step4=x:=$(word $x,exit $(n100))
 
iteration=$(if $(filter exit,$x $y),,$(turn)$(flip)$(step))
$(foreach i,$(n100) $(n100),$(foreach j,$(n100),$(iteration)))
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 6,936 ⟶ 7,103:
</syntaxhighlight>
The output is similar to the basic D version.
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ stack 50 ] is xpos ( --> s )
[ stack 50 ] is ypos ( --> s )
 
[ xpos share 0 100 within
ypos share 0 100 within
and ] is inside ( --> b )
 
[ -1 ypos ] is north ( --> n s )
[ 1 xpos ] is east ( --> n s )
[ 1 ypos ] is south ( --> n s )
[ -1 xpos ] is west ( --> n s )
 
[ stack 0 ] is heading ( --> s )
 
[ 1 ] is right ( --> n )
[ -1 ] is left ( --> n )
 
[ heading take
+ 4 mod
heading put ] is turn ( --> )
 
[ heading share
[ table
north east south west ]
do tally ] is move ( --> )
 
[ ypos share peek
xpos share bit & 0 > ] is black? ( [ --> b )
 
[ ypos share
2dup peek
xpos share bit ~ &
unrot poke ] is white ( [ --> [ )
 
[ ypos share
2dup peek
xpos share bit |
unrot poke ] is black ( [ --> [ )
 
[ 50 xpos replace
50 ypos replace
0 heading replace ] is reset ( --> )
 
[ witheach
[ 100 times
[ dup i^ bit &
iff say "[]"
else say " " ]
cr
drop ] ] is draw ( [ --> )
 
[ reset
0 100 of
[ inside while
dup black? iff
[ white left ]
else
[ black right ]
turn
move
again ]
draw ] is ant ( --> )</syntaxhighlight>
 
{{out}}
 
Surplus whitespace trimmed. Shown at <sup>'''2'''</sup>/<sub>'''3'''</sub> size.
 
<pre style="font-size:67%">
[][] [][][][][][][][][][][][] [][]
[] [][][][] [] [][]
[][][] [][] [][] []
[] [] [] [] [] []
[][] [][] [] [] [][][] []
[][][] [] [] [] [] [][] [][] [][][]
[] [] [][][] [][] [][][][] [][] [] [] [] [][] [][]
[] [][][] [][] [] [][] [][][] [] [] [][][] [][][]
[] [] [][][][][] [] [] [][][][] [] [][][] [] [] []
[][][] [][] [] [][][][] [][] [][] [][][][][][] [] [][][] [] []
[] [][][] [] [][] [] [] [][] [][] [][] [] [][][][][] [][][] [][]
[] [] [] [][] [][][] [] [] [] [][][][] [] [][]
[] [] [][] [][] [] [][] [][] [] [][]
[][][] [] [] [][] [][][] [] [][] [] [][][] [][] [][] []
[] [][][] [][] [][] [][] [][][] [] [] [][] [][][][] []
[][][] [] [] [] [] [] [][][][] [][] [] [][] [][][] [] []
[] [][][] [] [][] [] [] [][][] [] [][][] [][] [] [] [][]
[][][] [] [] [][] [] [][] [][] [][][][][] [][][][] [][][][] [][] []
[] [][][] [] [] [] [] [][][] [] [] [][] [][] [] [] [] [] []
[][][] [] [][] [][][] [][] [] [][] [][][][] [][][][] [] []
[] [][][] [] [] [] [][] [][][][][][][][][][][] [] [][][][] [] [] []
[][][] [] [][] [] [][][][] [][] [][][][][][][][][] [] [][] [] [][]
[] [][][] [] [] [][] [] [][] [][] [][] [][][] [][][] [] [] [][] [][][][] []
[][][] [] [][] [] [] [][][][][][] [][] [] [][] [] [] [][][] [][][] [][] []
[] [][][] [] [] [] [][][][][] [] [][][][][] [] [] [][] [] [][] []
[][][] [] [][] [] [] [][] [][][][][] [][] [] [] [] [] [][] [] [] []
[] [][][] [] [] [] [] [][][][] [] [][][][][] [][] [][][][][][][][][][] [][]
[][][] [] [][] [] [][] [][] [] [] [][][][] [] [][] [][][][] [][]
[] [][][] [] [] [][][][][] [] [][] [][] [] [] [] [] [] [] [] []
[][][] [] [][] [][] [][] [] [] [] [][] [][] [] [] [][] [] [][] [][]
[] [][][] [] [] [] [] [] [][][][][][][][] [] [] [][] [][][][] []
[][][] [] [][] [] [] [] [][] [][] [] [] [][] []
[] [][][] [] [] [] [] [] [] [][] [][] [][] [][][][]
[][][] [] [][] [][] [] [][] [][] [] [] [][][]
[] [][][] [] [] [] [][] [][][][] [][][][] [][][] [][][][]
[][][] [] [][] [][] [][][][] [][] [] [][] [] [] []
[] [][][] [] [] [][] [][] [][] [][][] [][] [][][][][]
[][][] [] [][] [] [][] [] [][][][]
[] [][][] [] [] [][] [][] [][]
[][][] [] [][] [][]
[] [][][] [] [] [] [][] [][][][] []
[][][] [] [][] [] [] [][][] [][][]
[] [][][] [] [] [] [][] [] [] []
[][][] [] [][] [][] [][]
[] [][][] [] [] [][]
[][][] [] [][]
[] [] [] [] []
[][][][] [][]
[] [][] []
[][][][]
[][]</pre>
 
=={{header|R}}==
Line 9,021 ⟶ 9,310:
{{trans|D}}
The textual version only.
<syntaxhighlight lang="ecmascriptwren">var width = 75
var height = 52
var maxSteps = 12000
9,476

edits