public class GamblersRuin {

  public static void main(String[] args) {
      
    int stake  = Integer.parseInt(args[0]);  // gambler's stating bankroll
    int goal   = Integer.parseInt(args[1]);  // gambler's desired bankroll
    int trials = Integer.parseInt(args[2]);  // number of trials to perform

    int wins = 0;             // total number of games won
    int losses = 0;           // total number of games lost
    int total_bets = 0;       // total number of bets made, win or lose
    int total_bets_to_win = 0;
    int total_bets_to_lose = 0;
    
    // repeat trials times
    for (int t = 0; t < trials; t++) {
      // do one gambler's ruin simulation
      int bets = 0;
      int cash = stake;
      while (cash > 0 && cash < goal) {
        bets++;
        if (Math.random() < 0.5) cash++; // win $1
        else cash--;                     // lose $1
      }
      total_bets += bets;
      if (cash == goal) wins++;
      else losses++;
    }

    // print results
    System.out.println("It resulted in " + wins + " wins and " + losses
      + " losses out of " + trials + " trials.");
    System.out.println("The percentage of wins:                  "
      + 100.0*wins/trials + "% ");
    System.out.println("The percentage of losses:                "
      + 100.0*losses/trials + "% ");
    System.out.println("The average number of bets, win or lose: "
      + 1.0*(total_bets)/trials);

  }

}
