// Die.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 Die
	extends java.applet.Applet
{
	TextArea disp;		// Area to display HT
	
	Panel dispArea;
	Panel controls;		// Panel for user controls
	
	Label numl;			// Controls
	TextField num;
	Button go;
	
	GridBagLayout gbl;
	GridBagConstraints cc;
	
	JRandom myRand;
		
	// Initialize applet
	public void init()
	{	
		numl = new Label("n =");			// Create controls
		num = new TextField("100", 4);
		
		go = new Button("Go");
		
		disp = new TextArea(15, 20);		// Create display area
		
		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(disp);
		
		gbl = new GridBagLayout();
		controls.setLayout(gbl);
		
		cc = new GridBagConstraints();
		
		cc.gridx = 0;
		cc.gridy = 0;
		gbl.setConstraints(numl, cc);
		controls.add(numl);
		
		cc.gridx = 1;
		gbl.setConstraints(num, cc);
		controls.add(num);
		
		cc.gridx = 0;
		cc.gridy = 1;
		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
			{
				disp.setText("");			// Reset output window
        		simulate(Integer.valueOf(num.getText()).intValue());
        		return true;					// Generate correct number of tosses
			}
		}
		return super.handleEvent(evt);	// Handle other events as usual
	}
	
	// Calculate probabilities
    public void simulate(int num)
    {	
    	int[] freq = {0, 0, 0, 0, 0, 0};
    	int total = 0;
    	int temp;
    
    	for (int i = 0; i < num; i++) {
    		temp = myRand.nextInt(1, 6);
    		freq[temp - 1]++;
    		if (temp == 2 || temp == 4 || temp == 6)
    			temp *= -1;
    		total += temp;
	    }
    
    	disp.appendText("Winning            Frequency          Relative frequency\n\n");
    	disp.appendText("1                            " + String.valueOf(freq[0]) + 
    		"                      " + String.valueOf((float) freq[0] / (float) num) + "\n");
    	disp.appendText("-2                          " + String.valueOf(freq[1]) + 
    		"                      " + String.valueOf((float) freq[1] / (float) num) + "\n");
    	disp.appendText("3                            " + String.valueOf(freq[2]) + 
    		"                      " + String.valueOf((float) freq[2] / (float) num) + "\n");
    	disp.appendText("-4                          " + String.valueOf(freq[3]) + 
    		"                      " + String.valueOf((float) freq[3] / (float) num) + "\n");
    	disp.appendText("5                            " + String.valueOf(freq[4]) + 
    		"                      " + String.valueOf((float) freq[4] / (float) num) + "\n");
    	disp.appendText("-6                          " + String.valueOf(freq[5]) + 
    		"                      " + String.valueOf((float) freq[5] / (float) num) + "\n");
    	disp.appendText("Average win: " + String.valueOf((float) total / (float) num) + "\n");
    }

}