import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.io.*;


public class FirstApplet extends Applet {

    private Button          Spin;
    private TextField       Prizes;
	private Font            myFont;
	private Button          ConsonnantButton;
	private TextField       ConsonnantInput;
	private Button          VowelButton;
	private TextField       VowelInput;
	private TextField       Score;
	private Font            myFont2;
	private TextField       CommentBox;
	private TextField       Solve;
	private Button          SolveButton;
	private TextField       Category;
	private Button          Quit;
	private Button          Puzz;
	private Frame           frame;
	private TextField[]     statusPane = new TextField[24];

	static final int WIDTH = 680;
    static final int HEIGHT = 360;

		public int jj;
		public int l;
		public int timeCount = 0;
		int rightcounter=0;
		int p=0;
		int money = 0;
		boolean SpinFlag = false;
		boolean ConsonnantButtonFlag = false;
        int ii;

	    String Prices[] ={"$100",
	                      "$250",
	                      "$400",
	                      "$550",
	                      "$700",
	                      "$850",
	                      "$1000",
	                      "Bankrupt"};

	    int Price1[] ={100,
	                   250,
	                   400,
	                   550,
	                   700,
	                   850,
	                   1000,
	                   0};

	    String Puzzle[][] ={{"PARIS","LONDON","OTTAWA","TORONTO","WASHINGTON"},
	                        {"PORSCHE","MERCEDES","HONDA","CHRYSLER","PONTIAC"},
	                        {"GUITAR","VIOLIN","PIANO","FLUTE","CLARINETTE"},
	                        {"APPLE","ORANGE","STRAWBERRY","KIWI","TANGERINE"}
	                        };

		String Categories[] = {"Places",
							   "Car Brands",
							   "Musical Instruments",
							   "Fruits",
							   };

	    String Consonnants[] ={" BCDFGHJKLMNPQRSTVWXYZ"};

	    String Vowels[] ={" AEIOU"};



      private void addComponents()
	  {
	    setLayout(null);

		double x;
		double yy;
		double z;

		String cc;
		int p;
		int i;
		int j = 0;
		int k = 0;
		int kk;
		int index;
		int indexarray[] = new int[10];

 		x = Math.random();
	    jj = (int)(x*5);
        yy = Math.random();
        ii = (int)(yy*4);
		l = Puzzle[ii][jj].length();
        int B = (WIDTH - l*50)/2;

        for (i=0; i<l; i++)
        {
			statusPane[i] = new TextField();
			statusPane[i].setEditable(false);
			statusPane[i].setBackground(Color.white);
			statusPane[i].setLocation(B+(i*50),20);
			statusPane[i].setSize(35, 40);
			myFont = new Font("Serif", Font.BOLD, 30);
			statusPane[i].setFont(myFont);
			statusPane[i].setBackground(Color.blue);
			add(statusPane[i]);
        }

			Prizes = new TextField();
			Prizes.setEditable(false);
			Prizes.setBackground(Color.white);
			Prizes.setForeground(Color.blue);
			Prizes.setLocation(30,100);
			Prizes.setSize(70,30);
			Prizes.setText("$$$$$");
			add(Prizes);

			Spin = new Button("Spin");
			Spin.setLocation(105,100);
			Spin.setSize(60,30);
			Spin.addActionListener(new SpinButtonHandler());
			add(Spin);

	        ConsonnantButton = new Button("OK");
		    ConsonnantButton.setLocation(105,140);
		    ConsonnantButton.setSize(60,30);
			ConsonnantButton.addActionListener(new ConsonnantButtonButtonHandler());
			add(ConsonnantButton);

			ConsonnantInput = new TextField(1);
			ConsonnantInput.setEditable(true);
			ConsonnantInput.setBackground(Color.white);
			ConsonnantInput.setForeground(Color.blue);
			ConsonnantInput.setLocation(30,140);
			ConsonnantInput.setSize(70,30);
			add(ConsonnantInput);

			VowelInput = new TextField(1);
			VowelInput.setEditable(true);
			VowelInput.setBackground(Color.white);
			VowelInput.setForeground(Color.blue);
			VowelInput.setLocation(30,180);
			VowelInput.setSize(70,30);
			add(VowelInput);

			VowelButton = new Button("Buy");
			VowelButton.setLocation(105,180);
			VowelButton.setSize(60,30);
			VowelButton.addActionListener(new VowelButtonButtonHandler());
			add(VowelButton);

			myFont2 = new Font("Serif", Font.BOLD, 16);
			Score = new TextField();
			Score.setLocation(545,100);
			Score.setSize(130,30);
			Score.setBackground(SystemColor.control);
			Score.setForeground(Color.black);
			Score.setText("Score:$ " + Integer.toString(money));
			Score.setEditable(false);
			Score.setFont(myFont2);
			add(Score);

			CommentBox = new TextField("Good Luck!");
			CommentBox.setEditable(false);
			CommentBox.setLocation(210,290);
			CommentBox.setSize(280,30);
			CommentBox.setBackground(Color.white);
			CommentBox.setForeground(Color.blue);
			add(CommentBox);

			Solve = new TextField();
			Solve.setLocation(210,250);
			Solve.setSize(207,30);
			Solve.setEditable(true);
			Solve.setBackground(Color.white);
			Solve.setForeground(Color.blue);
			add(Solve);

			SolveButton = new Button("Solve");
			SolveButton.setLocation(420, 250);
			SolveButton.setSize(70,30);
			SolveButton.addActionListener(new SolveButtonButtonHandler());
			add(SolveButton);

			Category = new TextField("Category: "+ Categories[ii]);
			Category.setLocation(247,210);
			Category.setSize(207,30);
			Category.setBackground(SystemColor.control);
			Category.setForeground(Color.blue);
			add(Category);
   }

   public void delay(){
   	   int delay1 = 1000;
   	   double z1, z=88;
   		for(int bb=0; bb<delay1; bb++)
   		{
   			for(int dd=0; dd<delay1; dd++)
   			{
   				 z1=z*z*z;

   			}

   			z1=z*z*z;
   		}
      }

   public class SpinButtonHandler implements ActionListener {
    	public void actionPerformed (ActionEvent event)
    	{
			if (SpinFlag == false)
			{
				double z=Math.random();
				p=(int)(z*8);
				SpinFlag = true;
				ConsonnantButtonFlag= true;
				for(int s=0; s<5; s++)
				{
					   if(s<4)
					   {
							 for(int b=0; b<8; b++)
							 {
								 Prizes.setText(Prices[b]);
								 delay();

							 }
						}
						else
						{

							 for(int b=0; b<=p; b++)
							 {
								 Prizes.setText(Prices[b]);
								 delay();
							 }


							}
           		 }
           		 if(Prices[p] == "Bankrupt")
				 {
				   money=0;
				   Score.setText("Score:$ " +Integer.toString(money));
				   SpinFlag = false;
				 }

            }
        	else
        	{
				CommentBox.setText("You cannot spin twice!");
		    }
   		}
	}

	public class VowelButtonButtonHandler implements ActionListener {
				public void actionPerformed (ActionEvent event)
				{
					int l=Puzzle[ii][jj].length();
				    int index;
				    int indexarray[]=new int[10];
				    String cc;
				    if (money >= 250)
				    {
						cc = VowelInput.getText();
						SpinFlag = false;

						int index1=-1;
						index1= Vowels[0].indexOf(cc);

						if ((index1 != -1) && (index1 != 0))
						{
						   index=-1;
						   int i=0;
						   do{
							  index=Puzzle[ii][jj].indexOf(cc,index+1);
							   if(index != -1){
								  indexarray[i]=index;
								  i++;
								  rightcounter++;
							   }
						   }while(index != -1);

						   for(int kk=0; kk<i; kk++){
								statusPane[indexarray[kk]].setText(cc);
								statusPane[indexarray[kk]].setForeground(Color.white);
							}

						   if (i != 0)
						   {
							  String tempmessage="Yes, we have " + Integer.toString(i) + " " + cc;
							  CommentBox.setText(tempmessage);
							  money = money - 250;
							  Score.setText("Score:$ " +Integer.toString(money));
							  Score.setEditable(false);
							  Score.setFont(myFont2);
							  if(rightcounter >= l)
							  {
								CommentBox.setText("You have solved the puzzle! You won $" + Integer.toString(money));
						      }
						   }
						   else
						   {
							   CommentBox.setText("Sorry, there is no " + cc);
						   }
					   }
					   else
					   {
						  CommentBox.setText("You have not entered a vowel!");
					   }
				   }
				   else
				   {
					   CommentBox.setText("You cannot buy a vowel now!");
				   }

			       VowelInput.setText("");

			    }
		}

	public class ConsonnantButtonButtonHandler implements ActionListener {
				public void actionPerformed (ActionEvent event)
				{
					int l=Puzzle[ii][jj].length();
					int index;
					int indexarray[]=new int[10];
					boolean validcons = false;
					String cc;
					cc=ConsonnantInput.getText();

					int index1=-1;
					index1= Consonnants[0].indexOf(cc);

				if (ConsonnantButtonFlag == true)
				{
					SpinFlag = false;
					if ((index1 != -1) && (index1 != 0))
					{
					   index=-1;
					   int i=0;
					   do{
						  index=Puzzle[ii][jj].indexOf(cc,index+1);
						   if(index != -1){
							  indexarray[i]=index;
							  i++;
							  rightcounter++;
						   }
					   }while(index != -1);

					   for(int kk=0; kk<i; kk++)
					   {
							statusPane[indexarray[kk]].setText(cc);
							statusPane[indexarray[kk]].setForeground(Color.white);
					   }

					   if (i != 0)
					   {
						  String tempmessage="Yes, we have " + Integer.toString(i) + " " + cc;
						  CommentBox.setText(tempmessage);
						  money = money+(i*Price1[p]);
						  Score.setText("Score:$ " + Integer.toString(money));
						  Score.setEditable(false);
						  Score.setFont(myFont2);
						  if(rightcounter >= l)
						  {
							CommentBox.setText("You have solved the puzzle! You won $" + Integer.toString(money));
						  }
					   }
					   else
					   {
						   CommentBox.setText("Sorry, there is no " + cc + ". Spin again.");
						   SpinFlag = false;
						   ConsonnantButtonFlag = false;
					   }
				   }
				   else
				   {
					  CommentBox.setText("You have not entered a consonnant!");
					  ConsonnantInput.setText("");
				   }

					ConsonnantInput.setText("");

			    }
				else
				{
					CommentBox.setText("You must spin before guessing a letter!");
					ConsonnantInput.setText("");
				}
		}
    }

    public class SolveButtonButtonHandler implements ActionListener {
			public void actionPerformed (ActionEvent event)
			{
				String ss = Solve.getText();
				int comp = ss.compareTo(Puzzle[ii][jj]);
				if (comp != 0)
				{
					Solve.setText(" ");
					CommentBox.setText("Sorry, that is incorrect");
				}
				else
				{
				    for(int kk=0; kk<l; kk++){
					     String c = Puzzle[ii][jj].substring(kk, kk+1);
						 statusPane[kk].setText(c);
						 statusPane[kk].setForeground(Color.white);
			       }
				   CommentBox.setText("You have solved the puzzle! You won $" + Integer.toString(money));

				   Solve.setText(" ");
				}
			}

    }



    public void init() {
        System.out.println("Applet initializing");
        addComponents();
    }
    public void start() {
        System.out.println("Applet starting");
    }
    public void stop() {
        System.out.println("Applet stopping");
    }
    public void destroy() {
        System.out.println("Applet destroyed");
    }
}



