function chess ( N, q )

%*****************************************************************************80
%
%% chess() simulates an N-game chess tournament.
%
%  Discussion:
%
%    This function computes the probabilities, in an N-game chess
%    match, of the match ending in a tie, in a win for the champ, or
%    in a win for the challenger. The probability the champ wins an
%    individual game is p, and the probability an individual
%    game ends in a tie is q.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
%  Input:
%
%    integer N, number of games in the match.
%
%    real Q, probability of a drawn game.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'chess():\n' );
  fprintf ( 1, '  Compute the probabilities, in an N-game chess\n' );
  fprintf ( 1, '  match, of the match ending in a tie, in a win for the champ, or\n' );
  fprintf ( 1, '  in a win for the challenger. The probability the champ wins an\n' );
  fprintf ( 1, '  individual game is p, and the probability an individual\n' );
  fprintf ( 1, '  game ends in a tie is q.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Present the results graphically.\n' );

  ANSWER = zeros(3,round(100*(1-q)));

  for p = 0.01 : 0.01 : 1.0 - q

    g = ( 1 - p - q ) * p;
    RZ = zeros(1,N);         
    C = zeros(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 - 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

    SUM = 0;
    for k = 1 : N
      SUM = SUM + C(k,N);
    end

    Q = round ( 100 * ( 1 - q ) );
    j = round ( 100 * p );
    if ( j <= Q )
      ANSWER(1,j) = RZ(N);
      ANSWER(2,j) = SUM;
      ANSWER(3,j) = 1 - ANSWER(1,j) - ANSWER(2,j);
    end
  end
%
%  Graphics.
%
  clf ( );
  p = 0.01 : 0.01 : 1.0 - q;
  hold ( 'on' );
    plot ( p, ANSWER(1,:), 'h' )
    plot ( p, ANSWER(2,:), '.' )
    plot ( p, ANSWER(3,:), '-', 'linewidth', 3 )
  hold ( 'off' );
  title ( 'Tie is hexagons, Champ wins is dots, Challenger wins is solid' )
  xlabel ( 'probability, p, of the Champ winning a game' )
  ylabel ( 'probability' )
  grid ( 'on' );
  filename = 'chess.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
