1#!/usr/bin/perl -T -w 2# This is used for moving from human readable life format ot a code format. 3 4local($pl) = shift(@ARGV); 5if (!defined $pl || ("$pl" ne "tr" && "$pl" ne "sq" && "$pl" ne "pn" && "$pl" ne "hx")) { 6 print "\nUsage: $0 [tr | sq | pn | hx]\n"; 7 print "\ttr: triangle format (same as sq)\n"; 8 print "\tsq: square format\n"; 9 print "\tpn: pentagon (cairo tiling) format (same as sq)\n"; 10 print "\thx: hexagon format\n"; 11 exit 1; 12} 13 14&search; 15 16sub search { 17 local ($col, $row); 18 local ($x, $y); 19 local ($NOTUSED); 20 local (@array); 21 22 $HALFMAX = 64; # really 32 but being safe 23 $MINCOL = $MINROW = $HALFMAX; 24 $MAXCOL = $MAXROW = $HALFMAX; 25 $NOTUSED = -127; 26 $col = $row = $NOTUSED; 27 $number = 0; 28 while (<>) { 29 if (!($_ =~ /^#/)) { 30 @chars = split(//); 31 $number = 0; 32 $col = $NOTUSED; 33 $negative = 1; 34 foreach $c (@chars) { 35 if ($c =~ /[-]/) { 36 $negative = -1; 37 } elsif ($c =~ /[0123456789]/) { 38 $number = $number * 10 + ($c - '0'); 39 } elsif ($c =~ /[,]/) { # Last number does not have a "," 40 if ($col > $NOTUSED) { 41 $row = $number * $negative; 42 $col = $col + $HALFMAX; 43 $row = $row + $HALFMAX; 44 $array{$col, $row} = 1; 45 if ($col > $MAXCOL) { 46 $MAXCOL = $col; 47 } elsif ($col < $MINCOL) { 48 $MINCOL = $col; 49 } 50 if ($row > $MAXROW) { 51 $MAXROW = $row; 52 } elsif ($row < $MINROW) { 53 $MINROW = $row; 54 } 55 $col = $NOTUSED; 56 } else { 57 $col = $number * $negative; 58 } 59 $number = 0; 60 $negative = 1; 61 } elsif ($c =~ /[{}\/]/) { # Last number does not have a "," 62 $col = $NOTUSED; 63 $number = 0; 64 $negative = 1; 65 } 66 } 67 } 68 } 69 $x=$MAXCOL - $MINCOL + 1; 70 $y=$MAXROW - $MINROW + 1; 71 print "#x=$x, y=$y\n"; 72 for ($j = $MINROW; $j <= $MAXROW; $j++) { 73 if ("$pl" eq "hx") { 74 for ($j1 = $MINROW; $j1 <= $MAXROW + $MINROW - $j - 1; $j1++) { 75 print " "; 76 } 77 } 78 for ($i = $MINCOL; $i <= $MAXCOL; $i++) { 79 if ($array{$i, $j}) { 80 if ("$pl" eq "hx") { 81 print "O "; 82 } else { 83 print "*"; 84 } 85 } else { 86 if ("$pl" eq "hx") { 87 print ". "; 88 } else { 89 print "."; 90 } 91 } 92 } 93 print "\n"; 94 } 95} 96