Hey i was reading your post and thought that there might be a clearer way to save that data so i came up with the following. I started with your code then adapted it slowly till it became this. I used strings to hold the rows instead of arrays just because it made printing and inputing a lot easier. It also iterates on key press instead of using a counter, and stops when you enter 'q'.

#!/usr/bin/perl use strict; use warnings; use Clone qw( clone ); $|++; my $width = 29; my $height = 29; my $world = [ #0 1 2 3 #123456789012345678901234567890 ' ',#1 ' 00 ',#2 ' 0 ',#3 ' 0 ',#4 ' 00 ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#10 ' ',#1 ' 000 ',#2 ' ',#3 ' ',#4 ' ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#20 ' ',#1 ' ',#2 ' ',#3 ' ',#4 ' ',#5 ' ',#6 ' ',#7 ' ',#8 ' ',#9 ' ',#30 ] ; my $i = 0; print_world($world); while (<>) { chomp; exit if $_ eq 'q'; print "iteration:", $i++, "\n"; $world = iterate($world); print_world($world); } sub print_world { my $world = shift; for my $row (@$world) { print "$row\n"; } }; sub iterate { my $world = shift; my $new_world = clone($world); for my $x (0 .. $width) { for my $y (0 .. $height) { my $n = 0; for my $xc (-1,0,1) { for my $yc ( -1,0,1) { next if $xc == 0 and $yc == 0; my $tx = $x+$xc; next if $tx > $width or $tx < 0; my $ty = $y + $yc; next if $ty > $height or $ty < 0; #warn "$x + $xc = $tx\t$y + $yc = $ty"; my $tmp = substr($world->[$ty], $tx,1); $n++ if $tmp eq '0'; } } substr($new_world->[$y], $x,1) = ' ' if $n < 2 or $n > 3; substr($new_world->[$y], $x,1) = '0' if $n == 3; } } return $new_world; };

I broke out the printing and iterating out into functions so that they can be called independently and you don't have to repeat the code. Hopefully you can use this to figure out what is wrong with your version, plus it gave me a chance to play with substr which i havn't realy done before.


___________
Eric Hodges

In reply to Re: My Conway's Game Of Life Attempt by eric256
in thread My Conway's Game Of Life Attempt by Anonymous Monk

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.