// DieTest.java

// Written by Julian Devlin, 8/97, for the text book
// "Introduction to Probability," by Charles M. Grinstead & J. Laurie Snell

import java.applet.Applet;
import java.awt.*;

public class DieTest
	extends java.applet.Applet
{
	Float[] xCoords;		// Variables for simulation
	Float[] yCoords;
	
	ChiSquareGraph abg;				// AWT elements
	
	Panel dispArea;
	Panel controls;		// Panel for user controls
	
	Label numl1, numl2;			// Controls
	TextField num1, num2;
	Button go;
	
	GridBagLayout gbl;
	GridBagConstraints cc;
	
	JRandom myRand;
		
	// Initialize applet
	public void init()
	{	
		numl1 = new Label("Number of rolls =");			// Create controls
		num1 = new TextField("60", 4);
		numl2 = new Label("Number of trials");			// Create controls
		num2 = new TextField("100", 4);
		go = new Button("Go");
		
		abg = new ChiSquareGraph(); // initialize a graphing space
		
		dispArea = new Panel();				// Set up window
		controls = new Panel();
		setLayout(new BorderLayout(5, 5));
		
		add("South", controls);
		add("Center", dispArea);
		
		dispArea.setLayout(new GridLayout(1, 1));
		dispArea.add(abg);
		
		gbl = new GridBagLayout();
		controls.setLayout(gbl);
		
		cc = new GridBagConstraints();
		
		cc.gridx = 0;
		cc.gridy = 0;
		gbl.setConstraints(numl1, cc);
		controls.add(numl1);
		
		cc.gridx = 1;
		gbl.setConstraints(num1, cc);
		controls.add(num1);
		
		cc.gridx = 0;
		cc.gridy = 1;
		gbl.setConstraints(numl2, cc);
		controls.add(numl2);
		
		cc.gridx = 1;
		gbl.setConstraints(num2, cc);
		controls.add(num2);
		
		cc.gridx = 0;
		cc.gridy = 2;
		cc.gridwidth = 2;
		gbl.setConstraints(go, cc);
		controls.add(go);
		
		validate();
		
		myRand = new JRandom();
	}
	
	// Handle events
	public boolean handleEvent(Event evt)
	{
		if (evt.target instanceof Button)
		{
			if (evt.target == go && evt.id == Event.ACTION_EVENT)	// When button is clicked
			{
        		simulate(Integer.valueOf(num1.getText()).intValue(),
        			Integer.valueOf(num2.getText()).intValue());
        		return true;					// Generate correct number of tosses
			}
		}
		return super.handleEvent(evt);	// Handle other events as usual
	}
	
	// Calculate probabilities
    public void simulate(int rolls, int num)
    {	
    	xCoords = new Float[31];
    	yCoords = new Float[31];
    	int[] temp = new int[31];
    	int[] dice = new int[6];
    	int rnd;
    	float e = (float) rolls / 6f;
    	float v;
    	
    	yCoords[0] = new Float(0);
    	for (int i = 0; i < xCoords.length; i++) {
    		xCoords[i] = new Float(i);
    		temp[i] = 0;
    	}
    	for (int i = 0; i < num; i++) {
    		for (int j = 0; j < 6; j++) {
    			dice[j] = 0;
    		}
    		v = 0;
    		for (int j = 0; j < rolls; j++) {
    			rnd = myRand.nextInt(0, 5);
    			dice[rnd]++;
    		}
    		for (int j = 0; j < 6; j++) {
    			v += (float) Math.pow((double) dice[j] - (double) e, 2.0) / e;
    		}
    		v = (float) Math.ceil((double) v);
    		if (v < 31)
    			temp[(int) v]++;
    	}
    	
    	for (int i = 0; i < yCoords.length; i++) {
    		yCoords[i] = new Float((float) temp[i] / (float) num);
    	}
    	
		dispArea.remove(abg);
		abg = new ChiSquareGraph(xCoords, yCoords);	// Create new SpikeGraph
		dispArea.add(abg);							// Put up the graph
		validate();
	}
}




