/** model a Gambler who can calculate certain values using Pascal's Triangle.
 */
import java.util.Vector;
public class Gambler {
  // store pascalTriangle in a vector
  private Vector pascalTriangle= new Vector();

  /**addRows: add n rows to this Gambler's pascalTriangle
   */
  public void addRows(int n){

    // start is the first row to add
    int start= pascalTriangle.size();
    for (int row= start; row != start + n; row++){

      // build up the current row, its first entry is a 1
      Vector currentRow= new Vector();
      currentRow.addElement(new Integer(1));

      // build the other entries by adding elements from the row above
      for (int col=1; col < row; col++) {
	Vector previousRow= (Vector)pascalTriangle.elementAt(row-1);
	int i= ((Integer)previousRow.elementAt(col-1)).intValue();
	int j=((Integer)previousRow.elementAt(col)).intValue();
	currentRow.addElement(new Integer(i + j));
      }
      // last entry in the current row is 1
      if (row > 0) {
	currentRow.addElement(new Integer(1));
      }
      pascalTriangle.addElement(currentRow);
    }
  }
      

  /** constructor: create a Gambler with a pascalTriangle of
   *  with n rows.
   */
  public Gambler(int n) {
    addRows(n);
  }

  /** choose: return the jth integer of row i
   */
  public int choose(int i, int j) {
    if (pascalTriangle.size()-1 < i) {
      addRows((i+1) - pascalTriangle.size());
    }
    return ((Integer)((Vector)pascalTriangle.elementAt(i)).elementAt(j)).
      intValue();
  }

  /** twoToThe: return the sum of the integers in row n
   */
  public int twoToThe(int n) {
    if (pascalTriangle.size()-1 < n) {
      addRows((n+1) - pascalTriangle.size());
    }
    Vector rowN= (Vector)pascalTriangle.elementAt(n);
    int sum= 0;
    for (int i=0; i != n+1; i++) {
      sum += ((Integer)rowN.elementAt(i)).intValue();
    }
    return sum;
  }
    
}

