function prob = ping_pong ( p )

%*****************************************************************************80
%
%% ping_pong() computes the likelihood of winning at ping pong.
%
%  Discussion:
%
%    A score of 11 wins in ping-pong...except that the winner must be
%    ahead by at least 2 points.  We ignore that exception here.
%    
%    We assume player 1 has a probability p of winning any single point.
%
%    We construct a table T such that T(i,j) is the probability that
%    player 1 will win, given that player 1 needs i more points to win,
%    and player 2 needs j more points to win.  Then T(11,11) is the
%    probability that player 1 wins.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    08 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input:
%
%    real p: the probability that player 1 wins a single point.
%
%  Output:
%
%    real prob: the probability that player 1 wins the game.
%
  t = zeros ( 11, 11 );

  q = 1.0 - p;
%
%  t(1,j) is the probability that player 1 will win, 
%  assuming player 1 needs 1 more point to win, 
%  and player 2 needs j more points to win.
%
  for j = 1 : 11
    t(1,j) = 1.0 - q^j;
  end
%
%  t(j,1) is the probability that player 1 will win, 
%  assuming player 1 needs j more points to win, 
%  and player 2 needs 1 more point to win.
%
%  In each case, player 1 must make j points in a row.
%
  for j = 1 : 11
    t(j,1) = p^j;
  end
%
%  t(i,j) is the probability that player 1 will win,
%  assuming player 1 needs i more points, and player 2 j more points.
%
%  This can happen in two ways:
%    player 1 had i-1 points, player 2 had j points, and player 1 made the next point.
%  OR
%    player 1 had i points, player 2 had j-1 points, and player 2 made the next point.
%
  for i = 2 : 11
    for j = 2 : 11
      t(i,j) = p * t(i-1,j) + q * t(i,j-1);
    end
  end
%
%  Our answer is the probability that player 1 will win, starting
%  from the beginning, when both players needed 11 points to win.
%
  prob = t(11,11);

  return
end
