/***************************************************
 * MarbleDrawTester.java
 * Author: Sarah House and Jake Dreier
 * Course: CSU231, Honors Freshman Seminar
 * Instructor: Dr. Richard Rasala
 * Date Created: October 13, 2004
 * Date Modified: November 29, 2004
 */

/*Idea Sentence: "You put 3 blue and 2 purple marbles in a bag and draw 2 out.
 * How many draws will you have to make to ensure that 
 * you have drawn a purple marble?"*/

/* Useful imports */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;

import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;

// import java.text.*;

public class MarbleDrawTester extends JPF 
{
    
    public static void main(String[] args) { 
        // To optionally adjust the look and feel,
        // remove the comments from one of the two statements below.
        
        // LookAndFeelTools.showSelectLookAndFeelDialog();
        // LookAndFeelTools.adjustAllDefaultFontSizes(2);
        
        new MarbleDrawTester();
    }
    /** assign marble stuff NOT in an automatically generated GUI. */
    public String[] marbleArray(int x) {
    	String[] marble = new String[8];
    	
    	if (x < 0)
    	    x = 0;
    	
    	if (x > 8)
    	    x = 8;
    	
    	for(int i = 0; i < x; i++)
			marble[i] = "red";
		
		for(int i = x; i < 8; i++)
			marble[i] = "blue";
		return marble;
	}
	
    public String testMarbleArray(int x){
        String[] data = marbleArray(x);
        
        String s = "";
        
        for (int i = 0; i < 8; i++)
            s += data[i] + " ";
        
        return s;
    }
    
    public void showSelectorPanel() {
        MarbleGUI.showSelectorPanel();
    }
    
    public void showLiveSelectorPanel() {
        MarbleGUI.showLiveSelectorPanel();
    }
    
    public void showRandomLiveSelectorPanel() {
        RandomOrderMarbleGUI.showRandomLiveSelectorPanel();
    }
    public void showRandomNumberLiveSelectorPanel() {
        RandomNumberMarbleGUI.showRandomLiveSelectorPanel();
    }
    
    public int combination(int n, int r){
    	return Combinatorics.combination(n,r);
    }
    
    public int permutation(int n, int r){
    	return Combinatorics.permutation(n,r);
    }
}




class MarbleGUI implements JPTConstants {
    TextFieldView count1 =
        new TextFieldView("4", 40);
    
    TextFieldView count2 =
        new TextFieldView("4", 40);
    
    ColorView color1 =
        new ColorView(Colors.red, true);
    
    ColorView color2 =
        new ColorView(Colors.blue, true);
    
    
    TablePanel selectorPanel =
        new TablePanel(
            new Object[][] {
                    { "Ball 1", new Halo(count1), color1 },
                    { "Ball 2", new Halo(count2), color2 }
           },
           10, 10, CENTER
        );
    
    public static void showSelectorPanel() {
        MarbleGUI gui = new MarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 1", gui.selectorPanel);
    }
    
    public PaintBar makePaintBar() {
        int n1 = count1.demandInt();
        int n2 = count2.demandInt();
        int n = n1 + n2;
        
        Color c1 = color1.getColor();
        Color c2 = color2.getColor();
        
        Paint[] array = new Paint[n];
        
        for (int i = 0; i < n; i++)
            if (i < n1)
                array[i] = c1;
            else
                array[i] = c2;
        
        return new PaintBar(array, 15, 5, HORIZONTAL);
    }
    
    
    public void showPaintBar() {
        PaintBar paintbar = makePaintBar();
        
        PaintableComponent component =
            new PaintableComponent(paintbar);
        
        JPTFrame.createQuickJPTFrame("test 1", component, NORTH);
    }
    
    
    SimpleAction showPB =
        new SimpleAction("Show Paint Bar!") {
        	public void perform() { showPaintBar(); }
    };
    
    
    TablePanel liveSelectorPanel =
        new TablePanel(
                new Object[] { selectorPanel, showPB },
                VERTICAL, 10, 10, CENTER);
    
    
    public static void showLiveSelectorPanel() {
        MarbleGUI gui = new MarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 1", gui.liveSelectorPanel);
    }
    

}
   
    
    
class RandomOrderMarbleGUI implements JPTConstants {
    TextFieldView countR1 =
        new TextFieldView("4", 40);
    
    TextFieldView countR2 =
        new TextFieldView("4", 40);
    
    ColorView colorR1 =
        new ColorView(Colors.red, true);
    
    ColorView colorR2 =
        new ColorView(Colors.blue, true);
    
    
    TablePanel randomSelectorPanel =
        new TablePanel(
            new Object[][] {
                    { "Ball 1", new Halo(countR1), colorR1 },
                    { "Ball 2", new Halo(countR2), colorR2 }
           },
           10, 10, CENTER
        );
    
    public static void showRandomSelectorPanel() {
        RandomOrderMarbleGUI gui = new RandomOrderMarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 2", gui.randomSelectorPanel);
    }
    

    
    public PaintBar makeRandomPaintBar() {
        int n1 = countR1.demandInt();
        int n2 = countR2.demandInt();
        int n = n1 + n2;
        
        Color c1 = colorR1.getColor();
        Color c2 = colorR2.getColor();
        
        int[] array = new int[n];
        
        for (int i = 0; i < n; i++)
            if (i < n1)
                array[i] = 1;
            else
                array[i] = 2;
        
        array = ProbStatTools.randomPermutation(array);
        Paint[] paintAr = new Paint[n];
        for (int j = 0; j < n; j++)
        	if (array[j] == 1)
        		paintAr[j] = c1;
        	else 
        		paintAr[j] = c2;
          return new PaintBar(paintAr, 15, 5, HORIZONTAL);
    }
    

    public void showRandomPaintBar() {
        PaintBar paintbar = makeRandomPaintBar();
        
        PaintableComponent component =
            new PaintableComponent(paintbar);
        
        JPTFrame.createQuickJPTFrame("test 1", component, NORTH);
    }
    
    
    SimpleAction showRPB =
        new SimpleAction("Show Random Paint Bar!") {
        	public void perform() { showRandomPaintBar(); }
    };
    
    
    TablePanel liveRandomSelectorPanel =
        new TablePanel(
                new Object[] { randomSelectorPanel, showRPB },
                VERTICAL, 10, 10, CENTER);
    
    
    public static void showRandomLiveSelectorPanel() {
        RandomOrderMarbleGUI gui = new RandomOrderMarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 2", gui.liveRandomSelectorPanel);
    }
}
class RandomNumberMarbleGUI implements JPTConstants {
// takes in a number n and two colors,
// and produces a random number less than n
// of marbles of one color and the remainder 
// of n of the other color 
    TextFieldView countR1 =
        new TextFieldView("4", 40);
    
    ColorView colorR1 =
        new ColorView(Colors.red, true);
    
    ColorView colorR2 =
        new ColorView(Colors.blue, true);
    
    
    TablePanel randomSelectorPanel =
        new TablePanel(
            new Object[][] {
                    { "Number of Marbles", new Halo(countR1), colorR1 },
           },
           10, 10, CENTER
        );
    
    public static void showRandomSelectorPanel() {
        RandomNumberMarbleGUI gui = new RandomNumberMarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 2", gui.randomSelectorPanel);
    }
    

    
    public PaintBar makeRandomPaintBar() {
        int n = countR1.demandInt();
        
        Color c1 = colorR1.getColor();
        Color c2 = colorR2.getColor();
        
        Paint[] array = new Paint[n];
        int k = MathUtilities.randomInt(0, n - 1);
        
        for (int i = 0; i < n; i++)
            if (i < k)
                array[i] = c1;
            else
                array[i] = c2;
        
        return new PaintBar(array, 15, 5, HORIZONTAL);
    }
    

    public void showRandomPaintBar() {
        PaintBar paintbar = makeRandomPaintBar();
        
        PaintableComponent component =
            new PaintableComponent(paintbar);
        
        JPTFrame.createQuickJPTFrame("test 1", component, NORTH);
    }
    
    
    SimpleAction showRPB =
        new SimpleAction("Show Random Paint Bar!") {
        	public void perform() { showRandomPaintBar(); }
    };
    
    
    TablePanel liveRandomSelectorPanel =
        new TablePanel(
                new Object[] { randomSelectorPanel, showRPB },
                VERTICAL, 10, 10, CENTER);
    
    
    public static void showRandomLiveSelectorPanel() {
        RandomNumberMarbleGUI gui = new RandomNumberMarbleGUI();
        
        JPTFrame.createQuickJPTFrame("test 2", gui.liveRandomSelectorPanel);
    }
}

class Combinatorics implements JPTConstants {
	
    public static int fact(int x) {
    	int y = 1;
    	while( x > 0){
    		y = (x * y);
    		x--;
    		}
    	return y;
	}
	
	public static int combination(int n, int r){
		// n!/r!(n - r)!
		return fact(n)/(fact(r) * ((fact((n - r)))));
	}
	public static int permutation(int n, int r){
		// n!/(n - r)!
		return fact(n)/(fact((n - r)));
		}	
}
