/*
	Simulates the game described in The Practice of Statistics (by Yates et al.)
	Written 11/09/00 by Chris Thiel <cct@ktb.net>
	
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.lang.Math;

public class discgame extends Applet
{
	//Globals
	Choice daMenu = new Choice();
	String message = "Press Toss to Play";	
	double size = 4.0;	// size of game disc
	double maximum = 10.0; // maximum size of disc
	double tile = 9.0; // size of tiles
	int xDisc, yDisc;	// location of center of disc
	int win  = 0; 	// number of wins
	int loss = 0;	// number of losses
	
	Color buttonColor;
	int buttonWidth=35;
	int buttonHeight=20;
	Rectangle toss,clear, slider;

	boolean sMoving = false;	// whether the slider is in motion
	
	public void init() {
		size = 4.0;
		win = 0;
		loss = 0;
		tile=4;
		buttonColor = new  Color( 200,250, 220);
		toss = new Rectangle ( size().width-buttonWidth-10,size().height-buttonHeight-10,buttonWidth,buttonHeight);
	    clear= new Rectangle ( 460, size().height-buttonHeight-10,buttonWidth,buttonHeight);
	   	slider= new Rectangle ( 475, 68, 100, 10);
	   	daMenu.addItem("4 inch tiles");
	   	daMenu.addItem("6 inch tiles");
		daMenu.addItem("9 inch tiles");
		daMenu.addItem("12 inch tiles");
		daMenu.addItem("16 inch tiles");
	    add(daMenu);
		repaint();
		
	}
	
	
	private boolean onTheCrack (int x, int y) {
		boolean h = false;
		
		
		double half = 2.5*size; //convert size to pixels
		for (int i=0; i< 500; i+=tile*5) if (i>x-half && i<x+half) h=true;
		for (int i=0; i< 350; i+=tile*5) if (i>y-half && i<y+half) h=true;
		return h;
	}
	private void tossDisc() {
		double x = Math.random()*450.0;// between 0 and 450 
		double y = Math.random()*300.0;// between 300 and 0
		xDisc = (int)(Math.round(x));
		yDisc = (int)(Math.round(y));
		boolean hit =false;
		
		
		if (onTheCrack(xDisc,yDisc)) {
			win++;
			message="On the crack! A Winner!";
		 } else {
		 	loss++;
		 	message="Sorry! Not on any cracks!";
		 }
		
	}

	public void paint( Graphics g ) {
		g.setColor(new Color(0xFFFF40));
		g.fillRect ( 450, 0, 145, 300);
		g.setColor(new Color(0x0080FF));
		g.fillRect ( 0, 0, 450, 300);
		
		g.setColor(new Color(0x0060FF)); //lines
	
		for(int i = 0; i< 450; i+=tile*5) g.drawLine( i,0,i ,300); // horizontals
			
		for(int i = 0; i< 300; i+=tile*5) g.drawLine( 0, i, 450,i ); //virticals
		//draw slider for adjustinf the size of the disc
		g.setColor(Color.black);
		g.drawString("Disk Size: "+size+" inches", 480,50);
		g.drawLine( 475, 73, 575, 73);
		if (sMoving) g.setColor(Color.red); else g.setColor(Color.green);
		g.fillOval((int)(100*size/maximum)+475,65, 5,15);
		
		//draw score
		g.setColor(Color.black);
		
		g.drawString("Wins:", 500, 150);
		g.drawString(win+" ", 550, 150);
		g.drawString("Losses:", 495, 175);
		g.drawString(loss+" ", 550, 175);
		g.drawString(message, 470, 200);
		int sz = (int)(5*size);
		g.setColor(new Color(0x2240AA));
		g.fillOval(xDisc-sz/2,yDisc-sz/2,sz,sz);
		g.setColor(Color.blue);
		g.drawOval(xDisc-sz/2,yDisc-sz/2,sz,sz);
		
		
		
		
		// update location of buttons in case window size changed
	   	toss.move(size().width-buttonWidth-10,size().height-buttonHeight-10);
	   	clear.move(460,size().height-buttonHeight-10);
		//draw colorful buttons and show which step we're on
		g.setColor( buttonColor);
		g.fill3DRect(toss.x, toss.y, toss.width, toss.height,true);
		g.fill3DRect(clear.x, clear.y, clear.width, clear.height,true);
		g.setColor( Color.black);
		g.draw3DRect(toss.x, toss.y, toss.width, toss.height,true);
		g.draw3DRect(clear.x, clear.y, clear.width, clear.height,true);
		g.setColor(Color.blue);
		g.drawString("Clear", clear.x+(int)(.15*buttonWidth), clear.y+(int)(.75*buttonHeight));
		g.drawString("Toss", toss.x+(int)(.15*buttonWidth), toss.y+(int)(.75*buttonHeight));
		
	}// end of paint

/*
     * Mouse methods
     */
	 public boolean mouseDown(java.awt.Event evt, int x, int y) {
      // Find out if user is manipulating a slider
     
        if ( sMoving ) {
        	sMoving =false;
        	if (onTheCrack(xDisc,yDisc)) {
			win++;
			message="On the crack! A Winner!";
		 } else {
		 	loss++;
		 	message="Sorry! Not on any cracks!";
		 }
		
        } else {
      
        	if ( slider.inside(x,y) ) { 
        		sMoving =true; 
        		win=0;
        		loss=0;	
        		message="Click to set disc size";
        	}
        	
       }
       if ( toss.inside(x,y) ) tossDisc();
       if ( clear.inside(x,y) ) {
       		win = 0;
       		loss = 0;
       }
       repaint();
        return true;
    }
	
  	
  	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
    	
       
        // transform slider movement in terms of the varible it controls
        // y varies from 50 to 270, a range of 220
         if (sMoving){
         	requestFocus();
        	size = (x-475)*maximum/100;
        	  
        	repaint();
         }
         
         
         
         //check for limits
         if (size > maximum) size=maximum;
         if (size < 0) size=0;
       
      
         //smooth the reals
       
        
     	 // size = Math.round(size*100)/100.0;
        return true;
    }

    public void mouseEnter() {
        repaint();
    }

    public void mouseExit() {
       
        repaint();
    }

// end of Mouse Methods
 /*
     * Event Methods for pop up menu
     */
    public boolean action(Event e, Object arg) {
          if (e.target instanceof Choice) {
          switch (daMenu.getSelectedIndex()) {
			case (0) : 
				tile=4;
				break;
			case (1) : 
				tile=6;
				break;
			case (2) : 
				tile=9;
				break;
			case (3) : 
				tile=12;
				break;
			case (4) : 
				tile=16;
				break;
		}// of switch
         repaint();
        return true;
        } else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }

}// end of discgame
