Langton's ant: Difference between revisions
Content added Content deleted
(→{{header|PicoLisp}}: Add Perl 5.) |
(→{{header|Perl}}: Add Perl 6.) |
||
Line 1,352: | Line 1,352: | ||
for (my $x=0; $x<$size; ++$x) { |
for (my $x=0; $x<$size; ++$x) { |
||
print $plane[$x][$y] ? '#' : '.'; |
print $plane[$x][$y] ? '#' : '.'; |
||
} |
|||
print "\n"; |
|||
}</lang> |
|||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2012.02}} |
|||
{{trans|Perl}} |
|||
This is just a straightforward translation of the Perl 5, helped only slightly by some Perl6isms: |
|||
<lang perl6>my @dirs = [1,0], [0,-1], [-1,0], [0,1]; |
|||
my constant $size = 100; |
|||
my @plane; |
|||
for ^$size -> $x { @plane[$x] = [ 0 xx 100 ] }; |
|||
my ($x, $y) = $size/2, $size/2; |
|||
my $dir = floor rand +@dirs; |
|||
loop (my $move = 0; 0 <= $x < $size && 0 <= $y < $size; $move++) { |
|||
if @plane[$x][$y] = 1 - @plane[$x][$y] { |
|||
$dir = ($dir + 1) % @dirs; |
|||
} else { |
|||
$dir = ($dir - 1) % @dirs; |
|||
} |
|||
$x = $x + @dirs[$dir][0]; |
|||
$y = $y + @dirs[$dir][1]; |
|||
} |
|||
say "Out of bounds after $move moves at ($x, $y)"; |
|||
for ^$size -> $y { |
|||
for ^$size -> $x { |
|||
print @plane[$x][$y] ?? '#' !! '.'; |
|||
} |
} |
||
print "\n"; |
print "\n"; |