Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^5: My Conway's Game Of Life Attempt

by BrowserUk (Patriarch)
on Jan 05, 2010 at 03:24 UTC ( [id://815683]=note: print w/replies, xml ) Need Help??


in reply to Re^4: My Conway's Game Of Life Attempt
in thread My Conway's Game Of Life Attempt

Sticking as close as possible to your methodology, but just a more compact representation, this version works.

Working out what small change in your logic was required is left as an exercise :)

#! perl -slw use strict; $|++; my @board = map [ (' ') x 39 ], 1.. 39; $board[ $_->[ 0 ] ][ $_->[ 1 ] ] = '0' for [1,1],[1,2],[2,1],[3,4],[4,3],[4,4], [9,8],[9,9],[9,10]; #my @board2 = @board; printf "iterations: "; my $z = <stdin>; $z--; my $t = 0; print "t:$t"; $t++; print join '|', @$_ for @board; select(undef, undef, undef, 1); while( $t <= $z ) { my @neighbours; my $i = 0; my @board2; for my $row ( @board ) { my $j = 0; for ( @$row ) { local $^W; my $neighbours = 0; ++$neighbours if $board[$i-1][$j-1] eq '0'; ++$neighbours if $board[$i-1][$j ] eq '0'; ++$neighbours if $board[$i-1][$j+1] eq '0'; ++$neighbours if $board[$i ][$j-1] eq '0'; # ++$neighbours if $board[$i ][$j ] eq '0'; ++$neighbours if $board[$i ][$j+1] eq '0'; ++$neighbours if $board[$i+1][$j-1] eq '0'; ++$neighbours if $board[$i+1][$j ] eq '0'; ++$neighbours if $board[$i+1][$j+1] eq '0'; $board2[$i][$j] = ' '; $board2[$i][$j] = ' ' if $board[$i][$j] eq '0' and $neighb +ours < 2; $board2[$i][$j] = '0' if $board[$i][$j] eq '0' and $neighb +ours == 2; $board2[$i][$j] = '0' if $neighbours == 3; $board2[$i][$j] = ' ' if $board[$i][$j] eq '0' and $neighb +ours > 3; $j++; } $i++; } print "t:$t"; print join '|', @$_ for @board2; $t++; select(undef, undef, undef, 0.1); @board = @board2; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-28 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found