// Created by David Bierbaum 9/19/2000
// This program will give the user the option of a target board or // checker board, then allow them to throw darts at it and // computer+display the scores. The position and size of board should
// also be user selectable. I could not get the scores to compute right
// or the darts on the circlular target to throw closely around the // target :(.

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

public class Darts extends Applet implements ActionListener, AdjustmentListener
{
//set all variables and set up screen with buttons and scrolls
 int size = 50;
 int scoreinc = 10;
 int score = 0;
 int darts_left = 10;
 int tempscore = 0;
 int target_selected = 0;
 int dartx;
 int darty;
 int x_scroll = 100;
 int y_scroll = 100;
 boolean thrown = false;
 private Button clear_button;
 private Button target_button;
 private Button throw_button;
 private Button checker_button;
 private Scrollbar xpos_scroll;
 private Scrollbar ypos_scroll;
 private Scrollbar size_scroll;
 private Panel top = new Panel();
 private Panel bottom = new Panel();

public void init()
{
 setLayout(new BorderLayout());
 size_scroll = new Scrollbar(Scrollbar.VERTICAL,50,1,25,60);
 size_scroll.addAdjustmentListener(this);
 xpos_scroll = new Scrollbar(Scrollbar.HORIZONTAL,100,1,40,325);
 top.add(xpos_scroll);
 xpos_scroll.addAdjustmentListener(this);
 ypos_scroll = new Scrollbar(Scrollbar.VERTICAL,100,1,120,225);
 top.add(ypos_scroll);
 ypos_scroll.addAdjustmentListener(this);
 clear_button = new Button("Clear");
 bottom.add(clear_button);
 clear_button.addActionListener(this);
 target_button = new Button("Target Board");
 bottom.add(target_button);
 target_button.setVisible(true);
 target_button.addActionListener(this);
 throw_button = new Button("Throw Dart");
 bottom.add(throw_button);
 throw_button.setVisible(true);
 throw_button.addActionListener(this);
 checker_button = new Button("Checker Board");
 bottom.add(checker_button);
 checker_button.setVisible(true);
 checker_button.addActionListener(this);
 top.add(size_scroll);
 add("North",top);
 add("South",bottom);

}

// get values from scrollbars when user changes them

public void adjustmentValueChanged(AdjustmentEvent event)
{
 x_scroll=xpos_scroll.getValue();
 y_scroll=ypos_scroll.getValue();
 size=size_scroll.getValue();
 repaint();

}

public void actionPerformed(ActionEvent event)
{
if(event.getSource() == checker_button)
{ 
 target_selected=1;
 checker_button.setVisible(false);
 target_button.setVisible(false);
 throw_button.setVisible(true);
}
if(event.getSource() == target_button) 
{
 target_selected=2;
 checker_button.setVisible(false);
 target_button.setVisible(false);
 throw_button.setVisible(true);
}
if(event.getSource() == clear_button)
{ 
 score = 0;
 target_selected=0;
 darts_left=10;
 throw_button.setVisible(false);
 checker_button.setVisible(true);
 target_button.setVisible(true);
}
if(event.getSource() == throw_button)
{
 darts_left=darts_left-1;
 if(darts_left==0) throw_button.setVisible(false);
 if(target_selected==2)
{
  dartx=(int)((Math.random()*(5*size))+(x_scroll-(size)+((size)/6)));
  darty=(int)((Math.random()*(5*size))+(y_scroll-(size)+((size)/6)));
}
 if(target_selected==1)
{
  dartx=(int)((Math.random()*(5*size))+(x_scroll-size));
  darty=(int)((Math.random()*(5*size))+(y_scroll-size));
}
thrown = true;
if(dartx<0) dartx=0;
if(dartx>350) dartx=350;
if(darty<0) darty=0;
if(darty>350) darty=350;
}
scoreinc = 10;
repaint();
}

public void paint (Graphics surface)
{

scoreinc=10;
//print name, title, date, and scores/darts left on screen
  surface.drawString("Game of Darts",20,75);
  surface.drawString("David Bierbaum",10,95);
  surface.drawString("9/30/2000",25,115);
  surface.drawString("X: "+x_scroll,150,80);
  surface.drawString("Y: "+y_scroll,190,80);
  surface.drawString("Size: "+size,230,80);

//draw targets
  if((target_selected==1)&&(darts_left>0))      drawChecker(surface,x_scroll,y_scroll,size);
  if((target_selected==2)&&(darts_left>0))      drawTarget(surface,x_scroll,y_scroll,size*3);
  if(target_selected==0) surface.drawString("Please choose dart board      type.",125,180);
  if(darts_left==0) surface.drawString("GAME OVER, Quit or Press      Clear",120,225);
  if(target_selected==0) throw_button.setVisible(false);
  if(thrown)
{
    surface.setColor(Color.cyan);
    surface.fillOval(dartx,darty,20,20);
    surface.setColor(Color.black);
    int distance = 0;
    if(target_selected==2)
{
//computer score from dart throw
  int centerx;
  int centery;
  centerx=(int)(.5*size)+(x_scroll);
  centery=(int)(.5*size)+(y_scroll);
  distance=(((centerx+dartx)*   (centerx+dartx)+(centery+darty)*(centery+darty))^(1/2));
  tempscore=0;
  if(distance<size) tempscore=10;
  if(distance<(2/3)*size) tempscore=20;
  if(distance<(1/3)*size) tempscore=30;
}
    if((target_selected==1) && ((distance-size)>0)) tempscore=0;
    if((target_selected==1) && ((distance-size/3)<0)) tempscore=10;
    if((target_selected==1) && ((distance-size/2)<0)) tempscore=20;
    tempscore=(int)(Math.random()*(30));
    surface.drawString("Scored:"+tempscore,190,250);
    score=score+tempscore;
    thrown = false;
}

//display results 
 surface.setColor(Color.black);
 surface.drawString("Score: "+score,10,150);
 surface.drawString("Darts Left: "+darts_left,300,150);

}

private void drawChecker (Graphics surface,int xcorner,int ycorner,int size)
{
drawSq(surface,xcorner,ycorner,Color.red,size);
scoreinc=10;
drawSq(surface,xcorner+size,ycorner,Color.black,size);
scoreinc=20;
drawSq(surface,xcorner+(size*2),ycorner,Color.red,size);
scoreinc=10;
drawSq(surface,xcorner,ycorner+size,Color.black,size);
scoreinc=30;
drawSq(surface,xcorner+size,ycorner+size,Color.red,size);
scoreinc=10;
drawSq(surface,xcorner+(size*2),ycorner+size,Color.black,size);
scoreinc=20;
drawSq(surface,xcorner,ycorner+(size*2),Color.red,size);
scoreinc=10;
drawSq(surface,xcorner+size,ycorner+(size*2),Color.black,size);
scoreinc=20;
drawSq(surface,xcorner+(size*2),ycorner+(size*2),Color.red,size);
surface.setColor(Color.black);
}
private void drawSq (Graphics surface,int left,int right,Color  square_color,int size)
{
surface.setColor(square_color);
surface.fillRect(left,right,size,size);
if(square_color==Color.red) surface.setColor(Color.black);
if(square_color==Color.black) surface.setColor(Color.red);
surface.drawString(""+scoreinc,left+((int)(.5*size)),right+((int)(.5*size))); 
}
// declare the drawCircle method
private void drawCircle (Graphics page,int left,int right,int circumf,Color circle_color,int center)
{
// set color based on inputed value
page.setColor(circle_color);
// draws a filled circle based on inputed values
page.fillOval(left,right,circumf,circumf);
// labels the circle with its score and then increments score
page.setColor(Color.black);
page.drawString(""+scoreinc,left+5,center);
scoreinc=scoreinc+10;
}
// declare the drawTarget method
private void drawTarget (Graphics window,int xcenter,int ycenter,int size)
{
// draws 3 concentric filled ovals using drawCircle method
drawCircle(window,xcenter-size/2,ycenter-size/2,size,Color.red,ycenter);
drawCircle(window,(xcenter-size/2)+(size/8),(ycenter-size/2)+(size/8),size-(size/4),Color.green,ycenter);

drawCircle(window,(xcenter-size/2)+(size/4),(ycenter-size/2)+(size/4),size-(size/2),Color.blue,ycenter);
}
} 
