From 1bd35eaa1a03023fe7d6fa822a4ee0778dd01998 Mon Sep 17 00:00:00 2001 From: Mikael Nordin Date: Fri, 28 Oct 2016 13:27:46 +0200 Subject: [PATCH] Something is not working, probably with boundary conditions --- 2d.pl | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/2d.pl b/2d.pl index 715b4df..4bf523e 100755 --- a/2d.pl +++ b/2d.pl @@ -72,8 +72,8 @@ sub print_arr { } -print "\033[2J"; #clear the screen -print "\033[0;0H"; #jump to 0,0 +#print "\033[2J"; #clear the screen +#print "\033[0;0H"; #jump to 0,0 my @initial = init; foreach my $elem (values @initial) { @@ -86,47 +86,56 @@ for (my $gen = 0; $gen < $gens; $gen++) { my @row; for (my $y = 0; $y < $max; $y++) { my $state; + # This is the upper column boundary if($x == 0) { + # This is the left boundary of the row if($y == 0 ) { $state = $initial[$max -1][$max -1] . $initial[$max -1][$y] . $initial[$max -1][$y +1 ]; - $state .= $initial[$x][$max -1] . $initial[$x][$y] . $initial[$x][$y +1 ]; - $state .= $initial[$x +1][$max -1] . $initial[$x +1][$y] . $initial[$x +1][$y +1 ]; + $state .= $initial[$x][$max -1] . $initial[$x][$y] . $initial[$x][$y +1 ]; + $state .= $initial[$x +1][$max -1] . $initial[$x +1][$y] . $initial[$x +1][$y +1 ]; + # This is the right boundary of the row } elsif ($y == $max -1 ) { $state = $initial[$max -1][$y-1] . $initial[$max -1][$y] . $initial[$max -1][0]; - $state .= $initial[$x][$y-1] . $initial[$x][$y] . $initial[$x][0]; - $state .= $initial[$x +1][$y-1] . $initial[$x +1][$y] . $initial[$x +1][0]; - # Normal, we are not on the boundary + $state .= $initial[$x][$y-1] . $initial[$x][$y] . $initial[$x][0]; + $state .= $initial[$x +1][$y-1] . $initial[$x +1][$y] . $initial[$x +1][0]; + # Normal, we are not on the boundary of the row } else { $state = $initial[$max -1][$y - 1] . $initial[$max -1][$y] . $initial[$max -1][$y +1]; - $state .= $initial[$x][$y - 1] . $initial[$x][$y] . $initial[$x][$y +1]; - $state .= $initial[$x +1][$y - 1] . $initial[$x +1][$y] . $initial[$x +1][$y +1]; + $state .= $initial[$x][$y - 1] . $initial[$x][$y] . $initial[$x][$y +1]; + $state .= $initial[$x +1][$y - 1] . $initial[$x +1][$y] . $initial[$x +1][$y +1]; } + # This is bottom boundary of the column } elsif ($x = $max -1) { + # This is the left boundary of the row if($y == 0 ) { $state = $initial[$x -1][$max -1] . $initial[$x -1][$y] . $initial[$x -1][$y +1 ]; $state .= $initial[$x][$max -1] . $initial[$x][$y] . $initial[$x][$y +1 ]; - $state .= $initial[0][$max -1] . $initial[0][$y] . $initial[0][$y +1 ]; + $state .= $initial[0][$max -1] . $initial[0][$y] . $initial[0][$y +1 ]; + # This is the right boundary of the row } elsif ($y == $max -1 ) { $state = $initial[$x -1][$y-1] . $initial[$x -1][$y] . $initial[$x -1][0]; $state .= $initial[$x][$y-1] . $initial[$x][$y] . $initial[$x][0]; - $state .= $initial[0][$y-1] . $initial[0][$y] . $initial[0][0]; - # Normal, we are not on the boundary + $state .= $initial[0][$y-1] . $initial[0][$y] . $initial[0][0]; + # Normal, we are not on the boundary of the row } else { $state = $initial[$x -1][$y - 1] . $initial[$x -1][$y] . $initial[$x -1][$y +1]; $state .= $initial[$x][$y - 1] . $initial[$x][$y] . $initial[$x][$y +1]; - $state .= $initial[0][$y - 1] . $initial[0][$y] . $initial[0][$y +1]; + $state .= $initial[0][$y - 1] . $initial[0][$y] . $initial[0][$y +1]; } + # Normal, we are not on the boundary of the column } else { + # This is the left boundary of the row if($y == 0 ) { $state = $initial[$x -1][$max -1] . $initial[$x -1][$y] . $initial[$x -1][$y +1 ]; $state .= $initial[$x][$max -1] . $initial[$x][$y] . $initial[$x][$y +1 ]; $state .= $initial[$x +1][$max -1] . $initial[$x +1][$y] . $initial[$x +1][$y +1 ]; + # This is the right boundary of the row } elsif ($y == $max -1 ) { $state = $initial[$x -1][$y-1] . $initial[$x -1][$y] . $initial[$x -1][0]; $state .= $initial[$x][$y-1] . $initial[$x][$y] . $initial[$x][0]; $state .= $initial[$x +1][$y-1] . $initial[$x +1][$y] . $initial[$x +1][0]; - # Normal, we are not on the boundary + # Normal, we are not on the boundary of anything } else { $state = $initial[$x -1][$y - 1] . $initial[$x -1][$y] . $initial[$x -1][$y +1]; $state .= $initial[$x][$y - 1] . $initial[$x][$y] . $initial[$x][$y +1]; @@ -140,9 +149,7 @@ for (my $gen = 0; $gen < $gens; $gen++) { } #print "\033[2J"; #clear the screen #print "\033[0;0H"; #jump to 0,0 - @initial = @column; - #print Dumper @initial; #foreach my $elem (values @initial) { # print_arr $elem; # }