Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-03-28 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found