/[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 1 by dpavlin, Sun Aug 26 02:53:41 2007 UTC revision 3 by dpavlin, Sun Aug 26 10:26:13 2007 UTC
# Line 6  Line 6 
6    
7  use strict;  use strict;
8    
 use Data::Dump qw/dump/;  
   
9  my $board = << '_BOARD_';  my $board = << '_BOARD_';
10  +-+-+-+-+-+-+-+-+  +-+-+-+-+-+-+-+-+
11  | |   |o| |     |  | |   |o| |     |
# Line 29  my $board = << '_BOARD_'; Line 27  my $board = << '_BOARD_';
27  _BOARD_  _BOARD_
28    
29  my @board = map { split(//) } split(/\n/, $board);  my @board = map { split(//) } split(/\n/, $board);
30    my @trace;
31    
32  # line length  # line length
33  my $ll = 8 * 2 + 1;  my $ll = 8 * 2 + 1;
34    
35    my @step_name = ( qw/right down left up/ );
36    my @move_by = ( 1, $ll, -1, -$ll );
37    my $step = 0;   # right
38    
39    # offset 0, top-left corner
40    my $pos = 0;
41    
42    # unknown trace position
43    my $unknown = ' ';
44    
45  sub draw {  sub draw {
         my @board = @_;  
46          my $o = 0;          my $o = 0;
47          my $out;          my $out;
48          while ( $o < $#board ) {          while ( $o < $#board ) {
49                  $out .= join('', @board[ $o .. $o + $ll - 1 ]) . "\n";                  $out .= join('', @board[ $o .. $o + $ll - 1 ]);
50                    $out .= '   ';
51                    $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
52                    $out .= "\n";
53                  $o += $ll;                  $o += $ll;
54          }          }
55          return $out;          return $out;
56  }  }
57    
 print $board, draw( @board );  
   
 my @step_name = ( qw/right down left up/ );  
 my @move_by = ( 1, $ll, -1, -$ll );  
 my $step = 0;   # right  
   
 # offset 0, top-left corner  
 my $pos = 0;  
 $pos = 2;  
 $pos = 6;  
   
 my @trace = ('x') x ( $#board + 1 );  
58  sub trace {  sub trace {
59          warn "trace $pos\n";          warn "## trace $pos\n";
60          $trace[ $pos ] = $board[ $pos ];          $trace[ $pos ] = $board[ $pos ];
61  }  }
62    
 warn draw( @trace );  
 trace;  
   
63  sub move {  sub move {
64          warn "move $step $step_name[$step]\n";          warn "move $step $step_name[$step]\n";
65          $pos += $move_by[ $step ];          $pos += $move_by[ $step ];
# Line 82  sub follow { Line 78  sub follow {
78  my $ok_path = qr/[\|\-]/;  my $ok_path = qr/[\|\-]/;
79    
80  sub can_turn {  sub can_turn {
81          my $step = shift;          my $try_step = shift;
82          die "no step?" unless defined $step;          die "no step?" unless defined $try_step;
83    
84            $try_step %= 4;
85    
86          my $turn_pos = $pos + $move_by[ $step % 4 ];          my $turn_pos = $pos + $move_by[$try_step];
87    
88          my $old = $trace[ $turn_pos ];          my $old = $trace[ $turn_pos ];
89          $trace[ $turn_pos ] = '?';          $trace[ $turn_pos ] = '?';
# Line 93  sub can_turn { Line 91  sub can_turn {
91          $trace[ $turn_pos ] = $old;          $trace[ $turn_pos ] = $old;
92    
93          if ( $board[ $turn_pos ] =~ $ok_path ) {          if ( $board[ $turn_pos ] =~ $ok_path ) {
94                  warn "OK can_turn $step_name[$step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";                  warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";
95                    $step = $try_step;
96                  return 1;                  return 1;
97          } else {          } else {
98                  warn "NOPE can_turn $step_name[$step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";                  warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])";
99                  return 0;                  return 0;
100          }          }
101  }  }
102    
103  while( 1 ) {  sub shape {
104    
105          my $next_pos = $pos + $move_by[ $step ];          my ($x,$y) = @_;
         warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n";  
106    
107          if ( $trace[ $next_pos ] ne 'x' ) {          $pos = $y * $ll * 2 + $x * 2;
108                  warn "position $next_pos re-visited, exiting\n";  
109                  last;          warn "<<< shape from $x,$y pos: $pos\n";
110          } elsif ( $board[ $next_pos ] =~ $ok_path ) {          @trace = ($unknown) x ( $#board + 1 );
111                  warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";          trace;
112                  move;  
113                  follow( $step+1 ) if can_turn( $step+1 );          my $len = 0;
114          } else {  
115                  warn "find line continuation from $step $step_name[$step]...\n";          while( 1 ) {
116                  foreach my $o ( -1, 1 ) {  
117                          if ( can_turn( $step + $o ) ) {                  my $next_pos = $pos + $move_by[ $step ];
118                                  $step = $step+$o;                  warn "in loop - pos = $pos next_pos = $next_pos step = $step $step_name[$step]\n";
119                                  warn "new direction: $step $step_name[$step]\n";  
120                                  follow( $step );                  if ( $trace[ $next_pos ] ne $unknown ) {
121                                  last;                          warn "position $next_pos re-visited, exiting\n";
122                          }                          last;
123                    } elsif ( $board[ $next_pos ] =~ $ok_path ) {
124                            warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";
125                            move;
126                            $len++;
127                            can_turn( $step + 1 );
128                    } else {
129                            warn "find line continuation from $step $step_name[$step]...\n";
130                            can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
131                  }                  }
132                    warn draw( @trace );
133    
134                    print "WAIT> press enter"; my $foo = <STDIN>;
135          }          }
         warn draw( @trace );  
136    
137            warn ">>> ended at $pos, line length: $len\n";
138            return $len;
139  }  }
140    
141  warn "ended at $pos\n";  my $shapes = '0,0 1,0';
142    
143    foreach my $start ( split(/\s/,$shapes) ) {
144            my $len = shape( split(/,/,$start) );
145            warn "## $start has $len elements\n";
146    }

Legend:
Removed from v.1  
changed lines
  Added in v.3

  ViewVC Help
Powered by ViewVC 1.1.26