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

public class Slot extends Applet {
	int aa = 5;
	int bb = 5;
	int cc = 5;
	int dd = 6;
	int ee = 6;
	int ff = 6;
	int gg = 4;
	int hh = 4;
	int ii = 4;
	int money = 1000;
	int x1, y1;
	int bet = 0;
	int width = getSize().width;
	int height = getSize().height;
	boolean firstStopButton, secondStopButton, thirdStopButton, coinButton, startButton;
	boolean isGameOver = false;
	private MouseListener mouseListener = new MouseAdapter() {
	
		public void mousePressed( MouseEvent event ) {
			x1 = event.getX();
			y1 = event.getY();
				if ((x1 >= 110 && x1 <= 160) && (y1 >= 240 && y1 <= 270)) {
					firstStopButton = true;
					repaint();
				}
				if ((x1 >= 175 && x1 <= 225) && (y1 >= 240 && y1 <= 270)) {
					secondStopButton = true;
					repaint();
				}
				if ((x1 >= 240 && x1 <= 290) && (y1 >= 240 && y1 <= 270)) {
					thirdStopButton = true;	
					repaint();
				}
				if ((x1 >= 320 && x1 <= 370) && (y1 >= 240 && y1 <= 270)) {
					startButton = true;
					repaint();
				}
				if (bet < 30) {
					if ((x1 >= 320 && x1 <= 371) && (y1 >= 78 && y1 <= 129)) {
						coinButton = true;
						if (money > 0) {
							money = money - 10;
							bet = bet + 10;
							repaint();
						}
					}
				}
			}
		public void mouseReleased( MouseEvent event ) {
			if(firstStopButton || secondStopButton || thirdStopButton || coinButton || startButton) {
				firstStopButton = false; 
				secondStopButton = false; 
				thirdStopButton = false;
				startButton = false;
				coinButton = false;
				repaint();
			}
		}
		
	};
		
	public void init() {
		addMouseListener( mouseListener );
		repaint();
	}
	public void GameOver(Graphics g) {
		g.setColor(new Color(100,0,100));
		g.setFont( new Font("TimesRoman", Font.BOLD, 30));
		g.drawString("Game Over", 130, 163);
		removeMouseListener( mouseListener );
	}
	public void nine( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("9",62 + 65 * column,78 + (45 * row));
	}
	public void ten( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("10",50 + 65 * column,78 + (45 * row));
	}
	public void jack( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("J",60 + 65 * column,78 + (45 * row));
	}
	public void queen( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("Q",55 + 65 * column,75 + (45 * row));
	}
	public void king( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("K",55 + (65 * column),78 + (45 * row));
	}
	public void ace( Graphics g, int row, int column ) {
		g.setColor(Color.yellow);
		g.fillRect(45 + (65 * column),45 + (45 * row),50,40);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 38));
		g.drawString("A",57 + 65 * column,77 + (45 * row));
	}
	public void director( Graphics g, int number, int row, int column) {
		if (number == 1) 
			nine(g,row,column);
		else if (number == 2)
			ten(g,row,column);
		else if (number == 3)
			jack(g,row,column);
		else if (number == 4)
			queen(g,row,column);
		else if (number == 5)
			king(g,row,column);
		else if (number == 6)
			ace(g,row,column);
	}
	public void paint( Graphics g ) {
	// Grey BackGround
		g.setColor(new Color(200,200,200));		
		g.fillRect(20,40,360,300);
	// Three Stop Buttons
		g.setColor(Color.red);					
		g.fillRect(110,240,50,30);
		g.fillRect(175,240,50,30);
		g.fillRect(240,240,50,30);
		g.setColor(Color.blue);
		g.drawString("Stop", 123,259);
		g.drawString("Stop", 188,259);
		g.drawString("Stop", 253,259);
	// Start Button
		g.setColor(Color.blue);
		g.fillRect(320,240,50,30);
		g.setColor(Color.red);
		g.setFont( new Font("TimesRoman", Font.BOLD, 15));
		g.drawString("Start",330,260);
	// Money Box
		g.setColor(Color.black);
		g.fillRect(80,280,240,50);
		g.setColor(Color.white);
		g.drawRect(100,290,200,30);
		g.drawLine(80,280,100,290);
		g.drawLine(80,330,100,320);
		g.drawLine(320,330,300,320);
		g.drawLine(320,280,300,290);
	// Money Indicator
		g.setFont( new Font("TimesRoman", Font.PLAIN, 15));
		g.setColor(Color.blue);
		g.fillRect(80,50,240,20);
		g.setColor(Color.green);
		g.drawString("$ " + money, 90, 64);
	// Board BackGround
		g.setColor(new Color(255,30,255));
		g.fillRect(90,80,220,150);
	// Board Lights
		if (bet == 0) {
			g.setColor(new Color(100,100,100));
			g.fillOval(96,106,8,8);
			g.fillOval(96,151,8,8);
			g.fillOval(96,196,8,8);
			g.fillOval(296,106,8,8);
			g.fillOval(296,151,8,8);
			g.fillOval(296,196,8,8);
			g.drawLine(100,110,300,110);
			g.drawLine(100,155,300,155);
			g.drawLine(100,200,300,200);
		}
		else if (bet == 10) {
			g.setColor(new Color(100,100,100));
			g.fillOval(96,106,8,8);
			g.fillOval(96,196,8,8);
			g.fillOval(296,106,8,8);
			g.fillOval(296,196,8,8);
			g.drawLine(100,110,300,110);
			g.drawLine(100,200,300,200);
			g.setColor(Color.green);			
			g.fillOval(96,151,8,8);
			g.fillOval(296,151,8,8);
			g.drawLine(100,155,300,155);
		}
		else if (bet == 20) {
			g.setColor(new Color(100,100,100));
			g.fillOval(96,196,8,8);
			g.fillOval(296,196,8,8);
			g.drawLine(100,200,300,200);
			g.setColor(Color.green);			
			g.fillOval(96,151,8,8);
			g.fillOval(296,151,8,8);
			g.drawLine(100,155,300,155);
			g.fillOval(96,106,8,8);
			g.fillOval(296,106,8,8);
			g.drawLine(100,110,300,110);
		}
		else if (bet == 30){
			g.setColor(Color.green);
			g.fillOval(96,106,8,8);
			g.fillOval(96,151,8,8);
			g.fillOval(96,196,8,8);
			g.fillOval(296,106,8,8);
			g.fillOval(296,151,8,8);
			g.fillOval(296,196,8,8);
			g.drawLine(100,110,300,110);
			g.drawLine(100,155,300,155);
			g.drawLine(100,200,300,200);
		}
	// Main Board
				director(g, aa, 1, 1);			
				director(g, bb, 1, 2);
				director(g, cc, 1, 3);			
				director(g, dd, 2, 1);
				director(g, ee, 2, 2);			
				director(g, ff, 2, 3);
				director(g, gg, 3, 1);			
				director(g, hh, 3, 2);												
				director(g, ii, 3, 3);	
	// Insert Coin Button
		g.setColor(new Color(0,160,0));
		g.fillRect(320,78,51,51);
		g.setColor(Color.yellow);
		g.fillOval(322,80,46,46);
		g.setColor(Color.black);
		g.fillRect(343,90,5,28);
		g.setColor(Color.black);
		g.setFont( new Font("TimesRoman", Font.PLAIN, 10));
		g.drawString("Insert Coin $10",315, 138);
		g.drawString("Max $30",328, 148);
	// Change Color if button is preesed
		g.setFont( new Font("TimesRoman", Font.PLAIN, 15));

		if( money <= 0 && bet == 0)
				GameOver(g);
		else if( firstStopButton ) {
			g.setColor(Color.blue);
			g.fillRect(110,240,50,30);
			g.setColor(Color.red);
			g.drawString("Stop", 123,259);
		}
		else if( secondStopButton ) {
			g.setColor(Color.blue);
			g.fillRect(175,240,50,30);	
			g.setColor(Color.red);	
			g.drawString("Stop", 188,259);
		}
		else if(thirdStopButton ) {
			g.setColor(Color.blue);
			g.fillRect(240,240,50,30);		
			g.setColor(Color.red);
			g.drawString("Stop", 253,259);
		}
		else if( startButton ) {
			g.setColor(Color.yellow);
			g.fillRect(320,240,50,30);
			g.setColor(Color.blue);
			g.setFont( new Font("TimesRoman", Font.BOLD, 15));
			g.drawString("Start",330,260);
			if(bet > 0) {
				aa = (int)(6 * Math.random() + 1 );
				bb = (int)(6 * Math.random() + 1 );
				cc = (int)(6 * Math.random() + 1 );
				dd = (int)(6 * Math.random() + 1 );
				ee = (int)(6 * Math.random() + 1 );
				ff = (int)(6 * Math.random() + 1 );
				gg = (int)(6 * Math.random() + 1 );
				hh = (int)(6 * Math.random() + 1 );	
				ii = (int)(6 * Math.random() + 1 );
				director(g, aa, 1, 1);			
				director(g, bb, 1, 2);
				director(g, cc, 1, 3);			
				director(g, dd, 2, 1);
				director(g, ee, 2, 2);			
				director(g, ff, 2, 3);
				director(g, gg, 3, 1);			
				director(g, hh, 3, 2);												
				director(g, ii, 3, 3);
				if( bet == 30) {
					if(( aa == bb) && (bb == cc))
						money = money + 350;
					if(( dd == ee) && (ee == ff))
						money = money + 350;
					if(( gg == hh) && (hh == ii))
						money = money + 350;
				}
				if( bet == 20) {
					if(( aa == bb) && (bb == cc))
						money = money + 350;
					if(( dd == ee) && (ee == ff))
						money = money + 350;
				}
				if( bet == 10) {
					if(( dd == ee) && (ee == ff))
						money = money + 350;
				}
			}
			bet = 0;
		}
		else if( coinButton ) {
			g.setColor(Color.white);
			g.fillRect(343,90,5,28);
		}
	}
}