import java.util.ArrayList;

/**
 * Class name: SimulateBets
 * This class represents similation of gambler's ruin.
 *
 * @author Robert Sedgewick
 * @author Kevin Wayne
 * @version 2.0, 10/21/26, modified by Takunari Miyazaki
 */

public class SimulateBets {

  private int stake;                    // gambler's starting bankroll
  private int goal;                     // gambler's desired bankroll
  private int trials;                   // the number of trials
  private ArrayList<BetResult> results; // array list of results

  /**
   * A constructor to instantiate a SimulateBets object.
   *
   * @param s - initial stake
   * @param g - goal
   * @param t - the number of trials
   */
  public SimulateBets(int s, int g, int t) {
    stake = s;
    goal = g;
    trials = t;
    results = new ArrayList<BetResult>(trials);      
  }

  /**
   * The accessor for results.
   *
   * @return a reference to results
   */
  public ArrayList<BetResult> getResults() {
    return results;
  }

  /**
   * Perform a single series of simulation and store the result in the 
   * array list results at a given index i.
   *
   * @param i - index
   */
  public void simulate(int i) {
    int bets = 0;
    int cash = stake;
    while (cash > 0 && cash < goal) {
      bets++;
      if (Math.random() < 0.5) cash++; // win $1
      else cash--;                     // lose $1
    }
    if (cash == goal) results.add(i, new BetResult(true, bets));
    else results.add(i, new BetResult(false, bets));
  }

  /**
   * Repeat a series of simulation for the number of trials.
   */
  public void simulate() {
    for (int i = 0; i < trials; i++)
      simulate(i);
  }

}
