function [ winner, games ] = jai_alai_match ( strength )

%*****************************************************************************80
%
%% jai_alai_match() simulates one match of jai_alai.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    02 July 2019
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Steven Skiena,
%    Calculated Bets,
%    Computers, Gambling, and Mathematical Modeling to Win,
%    Cambridge University Press, 2001,
%    ISBN13: 978-0521009621.
%
%  Input:
%
%    real strength(8), the strengths of each player.
%
%  Output:
%
%    integer winner, the winner of the match.
%
%    integer games, the number of games played before the match was won.
%
  first = 1;
  second = 2;
  waiting = 3:8;

  score = zeros ( 8, 1 );

  games = 0;

  while ( true )

   games = games + 1;
   if ( games <= 7 )
     points = 1;
   else 
     points = 2;
   end
 
   p1 = strength(first) / ( strength(first) + strength(second) );
   p2 = strength(second) / ( strength(first) + strength(second) );

   r = rand ( );

   if ( r < p1 )
     score(first) = score(first) + points;
     if ( 7 <= score(first) )
       winner = first;
       break;
     else
       next = waiting(1);
       waiting(1:5) = waiting(2:6);
       waiting(6) = second;
       second = next;
%      second = i4_wrap ( second + 1, 1, 8 );
%      if ( second == first )
%        second = i4_wrap ( second + 1, 1, 8 );
%      end
     end
   else
     score(second) = score(second) + points;
     if ( 7 <= score(second) )
       winner = second;
       break;
     else
       next = waiting(1);
       waiting(1:5) = waiting(2:6);
       waiting(6) = first;
       first = next;
%      first = i4_wrap ( first + 1, 1, 8 );
%      if ( first == second )
%        first = i4_wrap ( first + 1, 1, 8 );
%      end
     end
   end

  end

  return
end

