% duel.m
%  Model a duel, where A and B take turns shooting at each other
%  until one is hit.  A goes first, and have 30% accuracy.  B has 40%.
%  Who survives?
%
a_acc = input ( 'Enter accuracy between 0 and 1 for A: ' );
b_acc = input ( '                           and for B: ' );
n = input ( 'Enter number of duels to try: ' );

a_won = 0;
b_won = 0;

for i = 1 : n

  shots = 0;

  while ( true )
%
%  A's turn.
%
    shots = shots + 1;
    x = rand ( );
    if ( x <= a_acc )
      a_won = a_won + 1;
      break;
    end
%
%  B's turn.
%
    shots = shots + 1;
    x =rand ( );
    if ( x <= b_acc )
      b_won = b_won + 1;
      break;
    end

  end

end

fprintf ( '  A wins %d times, with probability %g\n', a_won, a_won / n );
fprintf ( '  B wins %d times, with probability %g\n', b_won, b_won / n );
