// Michael Tartaglia
// TossInterface: displays functionality of Dice class in a game of Craps

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class TossInterface extends Applet implements ActionListener
{
   private Dice die1 = new Dice();
   private Dice die2 = new Dice();
	private Button button;		// PLAY BUTTON
	private int point,			// POINTS
					toss,				// TOSS VALUE
					tossTimes;		// TIMES DICE WERE TOSSED
	private boolean pointSet;	// SET POINTS BOOLEAN

   public void init() {
		setBackground(new Color(0,160,0));
	
		// INITIALIZE VARIABLES
		toss = 0;
		tossTimes = 0;
		pointSet = false;
		
		// PLACE BUTTON ON SCREEN
		button = new Button("Click to play!");
		button.addActionListener(this);
		add(button);
	}
	
	public void actionPerformed(ActionEvent E) {
		die1.toss(); die2.toss();						// TOSS DICE
      toss = die1.getValue() + die2.getValue();	// GET TOTAL TOSS VALUE
		tossTimes++;				// REGISTER TOSS

		if (!pointSet) {			// IF POINT IS NOT ALREADY SET
			point = toss;			// SET POINT
			pointSet = true;		// REGISTER AS SO
		}

		repaint();
	}

   public void paint(Graphics g) {
		if (tossTimes > 0) {							// AS LONG AS THERE WAS A TOSS
			drawDice(g,die1.getValue(),60,40);	// DRAW WHAT DICE 1 LOOKS LIKE
			drawDice(g,die2.getValue(),90,40);	// DRAW DICE 2

			// SET FONT AND TELL USER INFO
			g.setColor(Color.white);
			g.setFont(new Font("Arial",Font.BOLD,12));
	      g.drawString("Your point is " + point + ".", 15,80);
			g.drawString("You tossed " + die1.getValue() + " and "
 												+ die2.getValue() + " for " + toss + ".\n",15,94);
		}
	
		// PRINT OUT CURRENT GAME RESULTS
      if (point == toss && tossTimes > 1) {		// ALERT OF WIN AND RESET
			g.drawString("You win!", 15,108);
			tossTimes = 0;
			pointSet = false;
			button.setLabel("Play again!");
		} else if (toss == 7 && tossTimes > 1) {	// ALERT OF LOSS AND RESET
			g.drawString("You lost!", 15,108);
			tossTimes = 0;
			pointSet = false;
			button.setLabel("Play again!");
		} else if (tossTimes > 0) {					// TELL USER TO KEEP GOING
			g.drawString("Toss again!", 15,108);
			button.setLabel("Toss");
		}
   }


	public void drawDice(Graphics g, int value, int x, int y)
	// FUNCTION: uses passed value to draw dice at point (x,y) using g
	{
		g.setColor(Color.white);
		g.fillRoundRect(x,y,19,19,3,3);
	   g.setColor(Color.black);
		g.drawRoundRect(x,y,19,19,3,3);
	
		switch (value) {
			case 1:	g.fillOval(x+8,y+8,4,4);
						break;
			case 2:  g.fillOval(x+5,y+5,4,4);
						g.fillOval(x+11,y+11,4,4);
						break;
			case 3:	g.fillOval(x+2,y+2,4,4);
						g.fillOval(x+8,y+8,4,4);
						g.fillOval(x+14,y+14,4,4);
						break;
			case 4:	g.fillOval(x+5,y+5,4,4);
						g.fillOval(x+5,y+11,4,4);
						g.fillOval(x+11,y+5,4,4);
						g.fillOval(x+11,y+11,4,4);
						break;
			case 5:	g.fillOval(x+2,y+2,4,4);
						g.fillOval(x+2,y+14,4,4);
						g.fillOval(x+14,y+2,4,4);
						g.fillOval(x+14,y+14,4,4);
						g.fillOval(x+8,y+8,4,4);
						break;
			case 6:	g.fillOval(x+2,y+2,4,4);
						g.fillOval(x+2,y+8,4,4);
						g.fillOval(x+2,y+14,4,4);
						g.fillOval(x+14,y+2,4,4);
						g.fillOval(x+14,y+8,4,4);
						g.fillOval(x+14,y+14,4,4);
						break;
		}
	}

}
