/[symmetry-circle]/symmetry.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /symmetry.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by dpavlin, Sun Aug 26 12:17:00 2007 UTC revision 6 by dpavlin, Sun Aug 26 14:15:26 2007 UTC
# Line 30  my $debug = shift @ARGV || 0; Line 30  my $debug = shift @ARGV || 0;
30    
31  my @board = map { split(//) } split(/\n/, $board);  my @board = map { split(//) } split(/\n/, $board);
32  my @trace;  my @trace;
33    my @visited = (' ') x ($#board + 1);
34    
35  # line length  # line length
36  my $ll = 8 * 2 + 1;  my $ll = 8 * 2 + 1;
# Line 53  my @shapes_start = ( '0,0' ); #( qw/0,0 Line 54  my @shapes_start = ( '0,0' ); #( qw/0,0
54    
55  sub draw {  sub draw {
56          my $o = 0;          my $o = 0;
57          my $out;          my $out = "\n  " . join('',
58                    ('0 1 2 3 4 5 6 7 8 | ') x 3
59            ) . "\n";
60          while ( $o < $#board ) {          while ( $o < $#board ) {
61                    my $l = '|';
62                    if ( $o % ($ll*2) == 0) {
63                            my $y = int($o / ($ll*2));
64                            $out .= "$y ";
65                            $l = $y;
66                    } else {
67                            $out .= "  ";
68                    }
69    
70                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);
71                  $out .= '   ';                  $out .= " $l ";
72                  $out .= join('', @trace[ $o .. $o + $ll - 1 ]);                  $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
73                  $out .= "\n";                  $out .= " $l ";
74                    $out .= join('', @visited[ $o .. $o + $ll - 1 ]);
75                    $out .= " $l\n";
76                  $o += $ll;                  $o += $ll;
77          }          }
78    
79            $out .= "\n";
80    
81          return $out;          return $out;
82  }  }
83    
84  sub trace {  sub trace {
85          warn "## trace $pos\n";          warn "## trace $pos\n";
86          $trace[ $pos ] = $board[ $pos ];          $trace[ $pos ] = $board[ $pos ];
87            $visited[$pos]++;
88  }  }
89    
90  my ( $tr_x, $tr_y );  my ( $tr_x, $tr_y );
91    my ( $bl_x, $bl_y );
92    
93    sub x_y {
94            my $p = shift;
95    
96  sub pos_x_y {          my $update = 0;
97          my $y = int($pos / ($ll*2));          if ( ! defined( $p ) ) {
98          my $x = int(($pos % $ll) / 2);                  $p = $pos;
99                    $update = 1;
100            }
101    
102            my $y = int($p / ($ll*2));
103            my $x = int(($p % $ll) / 2);
104    
105            warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
106    
107          $tr_x = $x unless defined $tr_x;          if ( $update ) {
         $tr_y = $y unless defined $tr_y;  
108    
109          $tr_x = $x if $x > $tr_x && $y == $tr_y;  #               $tr_x = $x if $x > $tr_x && $y == $tr_y;
110          $tr_y = $y if $y < $tr_y && $x == $tr_x;  #               $tr_y = $y if $y < $tr_y && $x == $tr_x;
111    
112                    if (
113                            $y < $tr_y
114                            ||
115                            $y <= $tr_y && $x > $tr_x
116                    ) {
117                            ( $tr_x, $tr_y ) = ( $x, $y );
118                            warn "## UPDATED tr: $tr_x,$tr_y\n";
119                    }
120    
121          warn "## pos_x_y $pos -> $x,$y\n";                  if (
122                            $x < $bl_x
123                            ||
124                            $y > $bl_y
125                    ) {
126                            ( $bl_x, $bl_y ) = ( $x, $y );
127                            warn "## UPDATED bl: $bl_x,$bl_y\n";
128                    }
129    
130          return ($x,$y) if wantarray;  #               $bl_x = $x if $x < $bl_x; # && $y == $bl_y;
131    #               $bl_y = $y if $y > $bl_y; # && $x == $bl_x;
132            
133            }
134    
135            warn "## x_y($p) -> $x,$y ",
136                    $update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '',
137                    "\n";
138    
139    #       return ($x,$y) if wantarray;
140          return "$x,$y";          return "$x,$y";
141  }  }
142    
# Line 93  sub move { Line 146  sub move {
146          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
147          trace;          trace;
148          push @directions, $step;          push @directions, $step;
149          warn "move $step $step_name[$step] to ", scalar pos_x_y, "\n";          warn "move $step $step_name[$step] to ", x_y, "\n";
150  }  }
151    
152  sub follow {  sub follow {
# Line 114  sub can_turn { Line 167  sub can_turn {
167    
168          my $old = $trace[ $turn_pos ];          my $old = $trace[ $turn_pos ];
169          $trace[ $turn_pos ] = '?';          $trace[ $turn_pos ] = '?';
170          warn "TEST\n",draw( @trace );          $trace[ $pos ] = '*';
171            warn "TEST ", x_y($turn_pos), "\n",draw;
172          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
173    
174          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
# Line 143  sub shape { Line 197  sub shape {
197    
198          $pos = $y * $ll * 2 + $x * 2;          $pos = $y * $ll * 2 + $x * 2;
199    
         warn "<<< shape from $x,$y pos: $pos\n";  
200          @trace = ($unknown) x ( $#board + 1 );          @trace = ($unknown) x ( $#board + 1 );
201          @directions = ();          @directions = ();
202          trace;          trace;
203    
204          my $len = 0;          my $len = 0;
205            ( $tr_x, $tr_y ) = ( $x,$y );
206            ( $bl_x, $bl_y ) = ( $x,$y );
207            $step = 0;
208    
209            if ( $visited[$pos] > 3 ) {
210                    warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n";
211                    return;
212            }
213    
214            warn "<<< shape from $x,$y pos: $pos\n";
215    
216          while( 1 ) {          while( 1 ) {
217    
# Line 167  sub shape { Line 230  sub shape {
230                          warn "find line continuation from $step $step_name[$step]...\n";                          warn "find line continuation from $step $step_name[$step]...\n";
231                          can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";                          can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
232                  }                  }
233                  warn draw( @trace );                  warn draw;
234    
235                    warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
236    
237                  if ( $debug ) {                  if ( $debug ) {
                         warn "## tr: $tr_x,$tr_y\n";  
238                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;                          print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
239                  }                  }
240          }          }
241    
242          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };          push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
243    
         warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions,"\n";  
   
244          my $tr = "$tr_x,$tr_y";          my $tr = "$tr_x,$tr_y";
245            my $bl = "$bl_x,$bl_y";
246    
247            warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n";
248    
249          if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {          if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {
250                  print "INFO: added top-right $tr\n";                  warn "INFO: added $tr top-right\n";
251                  push @shapes_start, $tr;                  push @shapes_start, $tr;
252          }          }
253            if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) {
254                    warn "INFO: added $bl bottom-left\n";
255                    push @shapes_start, $bl;
256            }
257    
258          print "WAIT> press enter"; my $foo = <STDIN>;          print "WAIT> press enter"; my $foo = <STDIN>;
259    

Legend:
Removed from v.5  
changed lines
  Added in v.6

  ViewVC Help
Powered by ViewVC 1.1.26