http://qs321.pair.com?node_id=384360

One of the things that has always amazed me is the ability of systems with extremely simple rules to exhibit "intelligent" or "chaotically ordered" behaviour. One such example is Langton's Ant, which follows such extremely simple rules and initially appears to behave chaotically, however after a certain number of steps a recurring pattern emerges.

The rules are as follows; An ant sits on a bit of graph paper where all the squares are initially empty. It moves into a neighbouring square and does one of two things, based on the colour of the square;

  1. If the square is white, it turns to the left and colours the square black.
  2. If the square is black, it turns to the right and colours the square white.

...and so on, ad infinitum. The interesting thing about this is that after a fixed number of steps, the ant builds a highway and hotfoots it into infinity. There are, in fact, any number of "ants" with different rules, however it has been proven that all of them which do not actually reverse their direction in a single move build highways and do not return to their starting position.

I got bored and wrote a program which models this behaviour. Feel free to play, although you'll probably need to build a bigger board to see the highway form. Trivial but very fun!

#! /usr/bin/perl -w # Use bondage and discipline use strict; use POSIX qw( ceil ); # Set up vars my $xmagnitude = 20; my $ymagnitude = 20; my @board; my %dir = ("x" => 0, "y" => -1); my $iterations = 1000; my $pause = 1; # Initialise board for (my $y = 0; $y < $ymagnitude; ++$y) { for (my $x = 0; $x < $xmagnitude; ++$x) { $board[$y][$x] = " "; } } # Find centre of board and X marks the spot my %pos; $pos{"x"} = ceil(($xmagnitude / 2) - 1); $pos{"y"} = ceil(($ymagnitude / 2) - 1); $board[$pos{"y"}][$pos{"x"}] = "X"; # start the loop my $count = 0; while ($count < $iterations) { system(($^O eq 'MSWin32') ? 'cls' : 'clear'); # Thx QandA Edit +ors map {print join ("", @$_)."\n"} @board; $pos{"x"} += $dir{"x"}; $pos{"y"} += $dir{"y"};; # Exit if out of bounds if($pos{"x"} >= $ymagnitude || $pos{"x"} < 0 || $pos{"y"} >= $ +xmagnitude || $pos{"y"} < 0) { print "\nAnt has reached the edge of the board!\n\n"; exit; } if ($board[$pos{"y"}][$pos{"x"}] eq "X") { # Turn left and make an O $board[$pos{"y"}][$pos{"x"}] = " "; # Remember your trig calculus my $tempvar = - $dir{"x"}; $dir{"x"} = $dir{"y"}; $dir{"y"} = $tempvar; } else { # Turn right and make an X $board[$pos{"y"}][$pos{"x"}] = "X"; # Remember your trig calculus my $tempvar = - $dir{"y"}; $dir{"y"} = $dir{"x"}; $dir{"x"} = $tempvar; } ++$count; sleep $pause; } # ##### End of Script ##### #

Update: This won't work on Win32 (yet.)

Update^2: This should work on Win32 now. Could someone test it please to check? Thx...

Update^3:Thanks to roju for testing on Win32. roju++

"Stercus! Dixit Pooh. Eeyore, missilis lux navigii heffalumporum iaculas. Piglet, mecum ad cellae migratae secundae concurras."