function [ p, q ] = squash ( )

%*****************************************************************************80
%
%% squash computes probabilities a player will win the game of squash.
%
%  Discussion:
%
%    Players P and Q can each score a point only by winning a rally in
%    which they served.  If a player wins the rally, he keeps the serve;
%    otherwise, the serve goes to the opposing player.  The winner is the
%    first to reach 9 points.
%
%    Here, we suppose both players P and Q have 1/2 chance of winning
%    any given rally.
%
%    We compute the probability that player P will win, depending on
%    whether P or Q serves first.
%
%  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
%
%  Parameters:
%
%    Output, real p, q, the probabilities that P wins, given that 
%    P serves first, or that Q serves first.
%    Expected values are 0.534863456 and 0.465136543.
%
  r = 2.0 / 3.0;

  ps = zeros ( 9, 9 );
  qs = zeros ( 9, 9 );

  for i = 1 : 9
    ps(i,1) = r^i;
    qs(i,1) = ps(i,1) / 2.0;
    qs(1,i) = 1.0 - ps(i,1);
    ps(1,i) = 1.0 - qs(i,1);
  end

  for i = 2 : 9
    for j = 2 : 9
      ps(i,j) = r * ( ps(i-1,j) + 0.5 * qs(i,j-1) );
      qs(i,j) = 0.5 * ( ps(i,j) + qs(i,j-1) );
    end
  end

  p = ps(9,9);
  q = qs(9,9);

  return
end
