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 GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  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.
#
  import matplotlib.pyplot as plt
  import numpy as np

  print ( '' )
  print ( 'chess():' )
  print ( '  Compute the probabilities, in an N-game chess' )
  print ( '  match, of the match ending in a tie, in a win for the champ, or' )
  print ( '  in a win for the challenger. The probability the champ wins an' )
  print ( '  individual game is p, and the probability an individual' )
  print ( '  game ends in a tie is q.' )
  print ( '' )

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

  for p = .01 : .01 : 1-q

    g = ( 1.0 - p - q ) * p
    RZ = np.zeros ( N )         
    C = np.zeros ( [ N, N ] )
    for k in range ( 1, N + 1 ):
      C[k-1,k-1] = p ** k

    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

      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

    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

        k = k + 1
        n = n + 1

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

    Q = round ( 100 * ( 1.0 - 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)
#
#  Graphics.
#
  p=.01:.01:1-q

  plt.clf ( )
  plt.plot ( p, answer(1,:), 'h' )
  plt.plot ( p, answer(2,:), '.' )
  plt.plot ( p, answer(3,:), '-' )
  plt.title ( 'Tie is hexagrams, Champ wins is dots, Challenger wins is solid' )
  plt.xlabel ( 'probability, p, of the Champ winning a game' )
  plt.ylabel ( 'probability' )
  plt.grid ( True )
  filename = 'chess.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return
end
