% 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: ' );

shots = 0;

while ( true )
%
%  A's turn.
%
  shots = shots + 1;
  x = rand ( );
  if ( x <= a_acc )
    fprintf ( '  Player A won after %d shots were fired!\n', shots );
    break;
  end
%
%  B's turn.
%
  shots = shots + 1;
  x =rand ( );
  if ( x <= b_acc )
    fprintf ( '  Player B won after %d shots were fired!\n', shots );
    break;
  end

end

