% ruin2.m
%  For $10 a toss, players H and T toss a coin, playing til bankruptcy.
%
h_cash = 100;
t_cash = 80;
flips = 0;

while ( true )

  flips = flips + 1;
  ht = randi ( [ 0, 1 ] );

  if ( ht == 0 )

    t_cash = t_cash + 10;
    h_cash = h_cash - 10;
    if ( h_cash < 10 )
      fprintf ( 'Terry won after %d flips!\n', flips );
      break;
    end

  else

    h_cash = h_cash + 10;
    t_cash = t_cash - 10;
    if ( t_cash < 10 )
      fprintf ( 'Haley won after %d flips!\n', flips );
      break;
    end

  end

end 

