Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Cyclic_cellular_automaton with a few changes.

Adjust $COLS and $ROWS for terminal size...

YuckFoo

#!/usr/bin/perl use strict; my $COLS = 80; my $ROWS = 60; my $SLEEP = .04; my $ITERS = 120; my @SCHEMES = ( ' ..::oo0088@@', ' .:o08@@80o:. ', '-<##>--<##>- ', ' .o@@@@@@@@o.', ' . o@o . o@o', '#### ++++ ', ); while(1) { my @chars = split('', $SCHEMES[rand(@SCHEMES)]); my $num = @chars; my $screen = new_screen($ROWS, $COLS, $num); my $backup = int(rand(2)); my $i; while ($i++ < $ITERS) { $screen = update($screen, $ROWS, $COLS, $num, $backup); show($screen, \@chars); select(undef, undef, undef, $SLEEP); } } #----------------------------------------------------------- sub update { my $s = shift; my $rows = shift; my $cols = shift; my $num = shift; my $back = shift; my $new; for my $r (0..$rows-1) { for my $c (0..$cols-1) { if (forward($s, $r, $c, $rows, $cols, $num)) { $new->[$r][$c] = ($s->[$r][$c] + 1) % $num; } elsif ($back) { $new->[$r][$c] = (($s->[$r][$c] - 1) + $num) % $num; } else { $new->[$r][$c] = $s->[$r][$c]; } } } return $new; } #----------------------------------------------------------- sub forward { my $s = shift; my $row = shift; my $col = shift; my $rows = shift; my $cols = shift; my $num = shift; for my $ri (-1..1) { my $r = ($row + $ri + $rows) % $rows; for my $ci (-1..1) { my $c = ($col + $ci + $cols) % $cols; ((($s->[$r][$c] + $num) - $s->[$row][$col]) % $num == 1) and return 1; } } return; } #----------------------------------------------------------- sub show { my $s = shift; my $chars = shift; for my $line (@$s) { print join('', map { $chars->[$_] } @$line), "\n"; } } #----------------------------------------------------------- sub new_screen { my $rows = shift; my $cols = shift; my $num = shift; my $s; for my $r (0..$rows-1) { for my $c (0..$cols-1) { $s->[$r][$c] = int(rand($num)); } } return $s; }


In reply to Some Silly Cyclic Cellular Automaton by YuckFoo

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 goofing around in the Monastery: (4)
As of 2024-04-19 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found