/**
 * File          : Pit.java
 * Desc          : The accountancy and display mechanism
 * Hist
 *   2004-02-22  : @author Douglas Reay
 */

public abstract class Pit {

    /** Should we display a progress marker? */
    public abstract boolean showProgress();
    /** How many trials to run? (1,000,000 takes a minute) */
    public abstract int getTrials();
    /** Is this a 2D pit, a 3D pit, or worse? */
    public abstract int getDimensions();
    /** Are there 4 or more pebbles */
    public abstract int getPebbles();
    /** Is it a round pit, square, or other distribution? */
    public abstract Space getSpace();
    /** The pebbles themselves */
    public abstract Points getPoints();


    // Run the program and deal with errors
    public void start() {
        try {
            output("Starting.\n");
            runSimulation();
            output("Finished.\n");
        } catch(Exception e) {
            output("Failed.\n" + e.toString() + "\n");
        }
    }


    // Run the program
    public void runSimulation() throws Exception {
        int success = 0;
        int repetitions = getTrials();
        for(int loop = 0 ; loop < repetitions ; loop++) {
            Points mypoints = getPoints();
            if( mypoints.isMatchPebbleInCenter() ) {
                success++; 
                reportProgress( loop, repetitions, true );
            } else {
                reportProgress( loop, repetitions, false );
            }
        }
        output( "\nI threw " + getPebbles() + " pebbles into a " + 
                getSpace().toString(getDimensions()) + " " +
                getDimensions() + "D pit, " + repetitions + " times.\n" +
                "The pebble in the center ratio was: " + 
                ( 1.0 * success / repetitions ) + "\n" );
    }
    

    // Print a "." for every 1% progress
    private void reportProgress( int progress, int trials, boolean worked) {
        if( ! showProgress() ) { return; }
        int divisor = trials / 100;
        if( ( 1.0 * progress / divisor ) > ( progress / divisor ) ) {
            return;
        }
        output(".");
    } 


    // Display a string to the screen
    private void output(String str) {
        System.out.print(str);
    }
}

// End of File