function value = boundary_is_legal ( w, p )

%*****************************************************************************80
%
%% boundary_is_legal() determines if a polyhex boundary word is legal.
%
%  Discussion:
%
%    The boundary word describes how to trace the boundary by starting
%    at a base point P=(i,j), and taking a sequence of steps of unit length 
%    in one of 6 directions, as suggested by these diagrams:
%
%          3     2-----*             3     2     *  
%      ^        /                ^    \         /   
%     /        /                /      \       /     
%    J  4-----P     1          J  4     P-----1      
%      /       \                       /             
%     /         \                     /              
%    *     5     6             *-----5     6         
%
%          I-->                      I-->           
%
%    If the current position P has parallelogram coordinates (I,J), then
%    the step of given index moves to
%
%      1  (I+1,J)
%      2  (I,  J+1)
%      3  (I-1,J+1)
%      4  (I-1,J)
%      5  (I,  J-1)
%      6  (I+1,J-1)
%
%    After a step of index K, the next step must be K-1 or K+1 (modulo 6).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.
%
%    integer P(2): the boundary word starting point.
%
%  Output:
%
%    boolean VALUE: is TRUE if the boundary word is legal.
%
  value = false;

  verbose = false;
%
%  Only the characters 1 through 6 can appear.
%
  l = sum ( w == '1' ) + sum ( w == '2' ) + sum ( w == '3' ) ...
    + sum ( w == '4' ) + sum ( w == '5' ) + sum ( w == '6' );

  if ( length ( w ) ~= l )
    if ( verbose )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'boundary_is_legal():\n' );
      fprintf ( 1, '  W contains illegal characters.\n' );
    end
    return;
  end
%
%  Each pair 1/4, 2/5 and 3/6 must match.
%
  if ( sum ( w == '1' ) ~= sum ( w == '4' ) )
    if ( verbose )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'boundary_is_legal():\n' );
      fprintf ( 1, '  1 and 4 characters do not match.\n' );
    end
    return;
  end

  if ( sum ( w == '2' ) ~= sum ( w == '5' ) )
    if ( verbose )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'boundary_is_legal():\n' );
      fprintf ( 1, '  2 and 5 characters do not match.\n' );
    end
    return;
  end

  if ( sum ( w == '3' ) ~= sum ( w == '6' ) )
    if ( verbose )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'boundary_is_legal():\n' );
      fprintf ( 1, '  3 and 6 characters do not match.\n' );
    end
    return;
  end
%
%  The step index must change by exactly 1 unit.
%
  cnew = w(end);
  pnew = ch_to_digit ( cnew );
  for i = 1 : length ( w )
    cold = cnew;
    pold = pnew;
    cnew = w(i);
    pnew = ch_to_digit ( cnew );
    if ( pold == 6 && pnew == 1 )
      continue;
    elseif ( pold == 1 && pnew == 6 )
      continue;
    else
      if ( abs ( pold - pnew ) ~= 1 )
        if ( verbose )
          fprintf ( 1, '\n' );
          fprintf ( 1, 'boundary_is_legal():\n' );
          fprintf ( 1, '  On step %d, illegal step %s to %s.\n', i, cold, cnew  );
        end
        return
      end
    end
  end
%
%  All tests passed.
%
  value = true;

  return
end

