function ash ( N )

%*****************************************************************************80
%
%% ash() is a special case of chess() using P = Q = 1/3.
%
%  Discussion:
%
%    This M file is chess.m modified to implement Ash's special
%    values of p = q = 1/3.
%
%    A champ plays a chump at N games of chess.
%    P is the probability that an individual game is won by the champ.
%    Q is the probability that an individual game ends in a tie.
%
%  Example:
%
%    For N = 100, the probablity of a tied match is 0.04877.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer N, the number of games in the match.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ash():\n' );
  fprintf ( 1, '  A champ plays a chump in a match of N games of chess.\n' );
  fprintf ( 1, '  P is the probability an individual game is won by the champ.\n' );
  fprintf ( 1, '  Q is the probability an individual game ends in a tie.\n' );
  fprintf ( 1, '  For this code, P = Q = 1/3.\n' );
  fprintf ( 1, '  What are the probabilities that the champ wins, ties, or loses the match?\n' );

  q = 1.0 / 3.0;
  p = 1.0 / 3.0;
  g = ( 1.0 - p - q ) * p;
  rz = zeros ( 1, N );
  C = zeros ( N, N );
  for k = 1 : N
    C(k,k) = p ^ k;
  end

  for n = 1 : N

    if ( 2 * round ( n / 2 ) == n )
      for d = 0 : 2 : n
        top = ( n + d ) / 2;
        bottom = ( n - d ) / 2;
        I = binomial ( n, bottom ) * binomial ( top, bottom );
        I = I * ( g ^ bottom ) * ( q ^ d );
        rz(n) = rz(n) + I;
      end
    else
      for d = 1 : 2 : n
        top = ( n + d ) / 2;
        bottom = ( n - d ) / 2;
        I = binomial ( n, bottom ) * binomial ( top, bottom );
        I = I * ( g ^ bottom ) * ( q ^ d );
        rz(n) = rz(n) + I;
      end
    end

  end

  g = 1.0 - p - q;

  for c = 2 : N
    k = 1;
    n = c;
    while ( n <= N )
      C(k,n) = C(k,n-1) * q + C(k+1,n-1) * g;
      if ( k == 1 )
        C(k,n) = C(k,n) + rz(n-1) * p;
      else
        C(k,n) = C(k,n) + C(k-1,n-1) * p;
      end
      k = k + 1;
      n = n + 1;
    end
  end

  total = sum ( C(1:N,N) )

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Probability of a tie is            %g\n', rz(N) );
  fprintf ( 1, '  Probability the champ wins is      %g\n', total );
  fprintf ( 1, '  Probability the challenger wins is %g\n', 1 - rz(N) - total );

  return
end
 
