import java.util.Scanner;

public class CoinFlipping {
    
    public static final int HEADS = 0;
    public static final int TAILS = 1;


    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("This program will simulate flipping a coin and report statistics on the flips");
        System.out.print("Enter the number of times to flip the coin: ");
        int numFlips = keyboard.nextInt();
        System.out.println("Simulating flipping a coin " + numFlips + " times ...");
        
        runFlips(numFlips);
        runFlipsCalcStreaksAfterFlips(numFlips);
    }
    
    /*
     * This method performs the flips and tracks the statistics about
     * th flips.
     */
    public static void runFlips(int num){
        int numHeads = 0;
        int numTails = 0;
        int maxRun = 0;
        int currentRun = 1;
        int currentFlipType = -1;
        int currentRunType = -1;
        int maxRunType = -1;
        
        for(int i = 0; i < num; i++){
            if( Math.random() < 0.5 ){
                numHeads++;
                currentFlipType = HEADS;
            }
            else{
                numTails++;
                currentFlipType = TAILS;
            }
            
            //same as before?
            if( currentFlipType == currentRunType){
                currentRun++;
            }
            else{
                // current flip different than previous run
                if( currentRun > maxRun){
                    maxRun = currentRun;
                    maxRunType = currentRunType;
                }
                currentRun = 1;
                currentRunType = currentFlipType;
            }
        }
        
        //after done need to check the last streak to see if it is the longest
        if( currentRun > maxRun){
            maxRun = currentRun;
            maxRunType = currentRunType;
        }

        showResults(numHeads, numTails, maxRun, maxRunType);
    }
    
    /*
     * This method prints out the results of the flipping simulation
     */
    public static void showResults(int numHeads, int numTails, int maxRun, int maxRunType){
        System.out.println("\n\nSimulated flipping a coin " + (numHeads + numTails) + " times.");
        System.out.println("Number of heads: " + numHeads);
        System.out.println("Percentage of heads: " + (double)numHeads / (numHeads + numTails));
        System.out.println("Number of heads: " + numTails);
        System.out.println("Percentage of tails: " + (double)numTails / (numHeads + numTails));
        System.out.print("Maximum run was " + maxRun);
        if( maxRunType == HEADS)
            System.out.println(" heads in a row.");
        else
            System.out.println(" tails in a row.");
    }
    
    /*
     * find largest streak in a different way
     */
    public static void runFlipsCalcStreaksAfterFlips(int num){
        int numHeads = 0;
        int numTails = 0;
        //store the result of all flips in a string
        String flips = "";
        
        for(int i = 0; i < num; i++){
            if( Math.random() < 0.5 ){
                numHeads++;
                flips += 'h';
            }
            else{
                numTails++;
                flips += 't';
            }
        }
        
        //add a character to the beginning and end of the flips String to remove fencepost problems
        flips = " " + flips + " ";
        int maxRun = 0;
        int currentRun = 0;
        char maxType = ' ';
        
        for(int i = 1; i < flips.length(); i++){
            if( flips.charAt(i) == flips.charAt(i-1))
                //streak still going on
                currentRun++;
            else{
                //streak has ended
                if( currentRun > maxRun){
                    maxRun = currentRun;
                    maxType = flips.charAt(i-1);
                }
                currentRun = 1;
            }
        }
        
        //System.out.println("\n\n" + flips + "\n"); //testing code
        int maxRunType = TAILS;
        if( maxType == 'h')
            maxRunType = HEADS;

        showResults(numHeads, numTails, maxRun, maxRunType);
    }

}
