function A = tennis_matrix ( p )

%*****************************************************************************80
%
%% tennis_matrix() returns a tennis transition matrix.
%
%  Discussion:
%
%    The peculiar tennis scoring system goes 0, 15, 30, 40, Win.
%
%    However, to win, a player must be ahead by two points.  
%
%    There are three "peculiar" states: Deuce, Ahead 1, Behind 1,
%    which arise when one player is near to winning.  
%    'Ahead 1' means the player is 1 point away from winning.
%    'Behind 1' means the player is 1 point away from losing.
%    'Deuce' means either player will win with two unopposed points.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 December 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real p: the probability that the server will make the next point.
%
%  Output:
%
%    real A(17,17): the transition matrix.
%
  q = 1.0 - p;

  A = zeros ( 17, 17 );
%
%  1: 0-0
%
  A(1,2) = p;
  A(1,3) = q;
%
%  2: 15-0
%  3: 0-15
%
  A(2,4) = p;
  A(2,5) = q;
  A(3,5) = p;
  A(3,6) = q;
%
%  4: 30-0
%  5: 15-15
%  6: 0-30
%
  A(4,7) = p;
  A(4,8) = q;
  A(5,8) = p;
  A(5,9) = q;
  A(6,9) = p;
  A(6,10) = q;
%
%  7: 40:0
%  8: 30:15
%  9: 15:30
%  10: 0:40
%
  A(7,11) = p;
  A(7,12) = q;
  A(8,12) = p;
  A(8,13) = q;
  A(9,13) = p;
  A(9,14) = q;
  A(10,14) = p;
  A(10,15) = q;
%
%  11: Win
%  12: 40:15
%  13: Deuce
%  14: 15:40
%  15: Loss
%
  A(11,11) = 1.0;
  A(12,11) = p;
  A(12,16) = q;
  A(13,16) = p;
  A(13,17) = q;
  A(14,11) = p;
  A(14,17) = q;
  A(15,15) = 1.0;
%
%  16: Ahead 1
%  17: Behind 1
%
  A(16,11) = p;
  A(16,13) = q;
  A(17,13) = p;
  A(17,15) = q;

  return
end

