P is for Practical PerlMonks

### Comment on

 ( #3333=superdoc: print w/ replies, xml ) Need Help??

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."

In reply to Cellular Automata :: Langton's Ant by Elgon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2015-11-30 09:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?