Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Cellular Automata :: Langton's Ant

by Velaki (Chaplain)
on Aug 21, 2004 at 02:46 UTC ( [id://384751]=note: print w/replies, xml ) Need Help??


in reply to Cellular Automata :: Langton's Ant

Thank for a cool program! Over lunch I whipped up a very quick and dirty Tk version.

Enjoy!

#!/usr/bin/perl -w use strict; use Tk; ######################################################## # Constants my $width = 30; # number of boxes wide my $height = 30; # number of boxes high my $scale = 15; # size of boxes in pixels my $freq = 50; # update every 50 milliseconds ######################################################## # Variables my @board; my $dir; my $x; my $y; my $stop; ######################################################## # Set up board my $m = MainWindow->new; $m->repeat($freq => \&run); my $c = $m->Canvas(-width => $width*$scale, -height => $height*$scale) +; &initBoard; $m->MainLoop; # <-- never exits ######################################################## # board utilities sub loc { my ($x, $y) = @_; return ($x*$scale+1, $y*$scale+1, $x*$scale+$scale-1, $y*$scale+$sca +le-1); } sub on { my ($x, $y) = @_; box($x,$y,"black"); $board[$x][$y] = 1; } sub off { my ($x, $y) = @_; box($x,$y,"white"); $board[$x][$y] = 0; } sub box { my ($x, $y, $color) = @_; $c->create('rectangle', loc($x,$y), -fill => $color); } sub initBoard { print "Initializing board..."; for my $i (0 .. $width) { for my $j (0 .. $height) { off($i, $j); print "."; } print "x"; } $c->pack; print "...done\n"; } ######################################################## # main callback code my $initFlag; sub initVars { $dir = 0; $stop = 0; $initFlag++; $x = int($width/2); $y = int($height/2); $board[$x][$y]=1; on($x,$y); print "Initialized\n"; } sub move { for ("$dir") { /0/ && do { if(($y-1)>=0) { $y--; &turn; } else { $stop = 1; } last; }; /1/ && do { if(($x+1)<=$width) { $x++; &turn; } else { $stop = 1; } last; }; /2/ && do { if(($y+1)<=$height) { $y++; &turn; } else { $stop = 1; } last; }; /3/ && do { if(($x-1)>=0) { $x--; &turn; } else { $stop = 1; }; last; } } } sub turn { if($board[$x][$y]==0) { $dir+=3; $dir%=4; on($x,$y); } else { $dir+=1; $dir%=4; off($x,$y); } } sub run { return if $stop; &initVars unless $initFlag; &move; }
-v
"Perl. There is no substitute."

Replies are listed 'Best First'.
Re^2: Cellular Automata :: Langton's Ant
by BrowserUk (Patriarch) on Aug 21, 2004 at 03:25 UTC

    Fun.

    It struck me (having just just read LW's "State of the Onion 2004"), that if you sat and watched that in a room full of Freudian psychoanalysts, calling out all the things you saw as the Ant crawls around, you could very quickly raise them to near orgasm as you satisfied their every, "If I had a patient that displayed X form of neurosis, I'd write a paper and present it to...", wet dreams.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://384751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found