Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Cellular Automata :: Langton's Ant by Velaki
in thread 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 chanting in the Monastery: (3)
As of 2024-04-18 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found