 
function duel ( )

%*****************************************************************************80
%
%% duel() tests duel_simulation().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 November 2018
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'duel():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Test duel_simulation().\n' );

  p = [ 0.40, 0.30 ];
  duel_num = 1000;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Player 1 accuracy: %g\n', p(1) );
  fprintf ( 1, '  Player 2 accuracy: %g\n', p(2) );
  fprintf ( 1, '  Number of duels simulated = %d\n', duel_num );
  [ s, turn_average ] = duel_simulation ( p, duel_num );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Probability player 1 wins: %g\n', s(1) );
  fprintf ( 1, '  Probability player 2 wins: %g\n', s(2) );
  fprintf ( 1, '  Average number of shots fired = %g\n', turn_average );

  p = [ 0.30, 0.40 ];
  duel_num = 1000;
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Player 1 accuracy: %g\n', p(1) );
  fprintf ( 1, '  Player 2 accuracy: %g\n', p(2) );
  fprintf ( 1, '  Number of duels simulated = %d\n', duel_num );
  [ s, turn_average ] = duel_simulation ( p, duel_num );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Probability player 1 wins: %g\n', s(1) );
  fprintf ( 1, '  Probability player 2 wins: %g\n', s(2) );
  fprintf ( 1, '  Average number of shots fired = %g\n', turn_average );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'dueltest():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end
function [ s, turn_average ] = duel_simulation ( p, duel_num )

%*****************************************************************************80
%
%% duel_simulation() simulates a duel.
%
%  Discussion:
%
%    Player 1 fires at player 2, and hits with a probability of P(1).
%    If Player 2 misses, then Player 2 fires at Player 1, hitting with
%    a probability of P(2).
%
%    The duel continues with alternating shots until only one player 
%    survives.
%
%    The simulation is intended to estimate the probabilities that a
%    player will survive, and the number of turns required.
%
%    The exact probability that player 1 will survive is
%
%      P(1) / ( P(1) + P(2) - P(1) * P(2) )
%
%    Player 2's chance is
%
%     P(2) * ( 1 - P(1) ) / ( P(1) + P(2) - P(1) * P(2) )
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 November 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Paul Nahin,
%    Duelling Idiots and Other Probability Puzzlers,
%    Princeton University Press, 2000,
%    ISBN13: 978-0691009797,
%    LC: QA273.N29.
%
%    Martin Shubik,
%    "Does the Fittest Necessarily Survive?",
%    in Readings in Game Theory and Political Behavior,
%    edited by Martin Shubik,
%    Doubleday, 1954,
%    LC: H61.S53.
%
%  Input:
%
%    real P(2), the probabilities that player I will hit an opponent
%    in a single shot.  Each P(I) should be between 0 and 1.
%
%    integer DUEL_NUM, the number of duels to run.
%
%  Output:
%
%    real S(2), the estimated probablity that player I will survive.
%
%    real TURN_AVERAGE, the average number of turns required to
%    complete the duel.
%
  rng ( 'shuffle' );

  s = zeros(2,1);
  turn_average = 0;

  for duel = 1 : duel_num
    [ survivor, turn_num ] = duel_once ( p );
    s(survivor) = s(survivor) + 1;
    turn_average = turn_average + turn_num;
  end

  s = s / duel_num;
  turn_average = turn_average / duel_num;

  return
end
function [ survivor, turn_num ] = duel_once ( p )

%*****************************************************************************80
%
%% duel_once() returns the outcome of a single duel.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 November 2009
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Martin Shubik,
%    “Does the Fittest Necessarily Survive?”,
%    in Readings in Game Theory and Political Behavior,
%    edited by Martin Shubik,
%    Doubleday, 1954,
%    LC: H61.S53.
%
%  Input:
%
%    real P(2), the probabilities that player I will hit an opponent
%    in a single shot.  Each P(I) should be between 0 and 1.
%
%  Output:
%
%    integer SURVIVOR, the survivor of the duel.
%
  turn_num = 0;
 
  while ( true )
%
%  Player 1 fires.
%
    turn_num = turn_num + 1;
    r = rand ( );
    if ( r <= p(1) )
      survivor = 1;
      break
    end
%
%  Player 2 fires.
%
    turn_num = turn_num + 1;
    r = rand ( );
    if ( r <= p(2) )
      survivor = 2;
      break
    end

  end

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the current YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 February 2003
%
%  Author:
%
%    John Burkardt
%
  t = now;
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end

