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; }
|
|---|