//package ruinpkg;

public class Ruin {
   int initialCash;
   int cash;

   public Ruin(int initCash) {
      initialCash = initCash;
      cash = initialCash;
   }

   private boolean oneTime() {
      int bet = 1;
      boolean tappedOut = false;
      while(true) {
         cash = cash - bet;
         int spin = (int)(38*Math.random()) + 1;
         if (spin <= 18) { // win
            cash = cash + 2*bet;
            return tappedOut;
         }
         else {
            // try to double bet
            if (2*bet > cash) { // tapped out, lose
               tappedOut = true;
               return tappedOut;
            }
            else {
               // double bet
               bet = bet*2;
            }
         } // if-else
      } // while
   } // oneTime

   public int playGame() {
      while(true) {
         boolean tappedOut = oneTime();
         if (tappedOut) return cash;
         // System.out.print(cash + " ");
         if (cash >= 2 * initialCash) return cash;
      }
   }
}


