% double_or_nothing.m
%
%  It costs $10 for each round of this game.
%  If you play, a random number between 0 and 20 is your reward.
%  You want to keep playing until you double your initial stake,
%  or can't afford to keep playing.
%
stake = input ( '  How much money do you want to gamble? ' );
goal = 2 * stake;
cost = 10.0;
round = 0;
while ( cost <= stake && stake < goal )
  round = round + 1;
  stake = stake - cost;  % Pay for this round.
  stake = stake + 20.0 * rand ( );  % Win between $0 and $20.
end

fprintf ( '\n' );
fprintf ( '  You played %d rounds of double or nothing.\n', round );
fprintf ( '  You finished with $%10.2f\n', stake );
if ( stake < cost )
  fprintf ( '  You could not afford to pay $%10.2f for another round.\n', cost );
else
  fprintf ( '  You doubled your money.\n' );
end

