//**************************************************************************************************
//Program : Lab7
//Name : Ammer Almalki
//**************************************************************************************************
import java.awt.*;                                                                                                     
import java.applet.Applet;                                                                                             
import java.awt.event.*; 
                                                                                                            
                                                                                                                       
public class Game extends Applet implements ActionListener,MouseListener,MouseMotionListener{                                                                                                                      
       Graphics g;
	private final int SIZE = 50;
	int x,y;                                                                                                        
       int width=50;                                                                                                  
       int height=50;                                                                                                 
       int randomint; //random number
       Color mycolor;  // box color                                                       
       public Button newGame; 
       Box Boxes[][]; // each index in array will hold a box                                                                                                            
       Color [ ] colors = {Color.blue,Color.cyan,Color.green,Color.magenta,Color.orange,Color.pink,Color.red,Color.yellow}; // array holds colors to use in game
       int [ ] currColor= new int[8];   //array to store colors value

	public void init()
       { 

	addMouseListener(this);
	newGame = new Button("New Game");
        add(newGame);                        //Button for new game        
        newGame.addActionListener(this);        

	//create boxes
       	Boxes = new Box[4][4];  
           for (int x=0; x< Boxes.length; x++)
             for(int y=0; y< Boxes[x].length; y++)
               Boxes[x][y] = new Box(x*SIZE, y*SIZE, SIZE, SIZE, Color.gray);    

	}
	
	public void paint(Graphics g)
        {
		this.g = g;
          	for(int x=0; x<Boxes.length; x++)
           	{
             	for(int y=0; y<Boxes[x].length; y++)
		 //Draw Each Box
                  Boxes[x][y].display(g);
           	}
             
        }//end paint 

	public Color getRanColor()  
	{        
           randomint = (int)(Math.random() * 8);
 	   while (currColor[randomint] >=2){
        	randomint=(int)(Math.random() * 8);
        	}	
	   currColor[randomint]=currColor[randomint] + 1;
           mycolor = colors[randomint];                                                                       
           return mycolor;                                                                         
        } 

	public void mouseReleased (MouseEvent event) 
       	{
        repaint();  //repaiting the applet
       	}  
     
    //provide empty definition for unused events method 
   public void mouseEntered (MouseEvent event) {}
   public void mouseExited (MouseEvent event)  {}      
   int counter = 0;
   int CurrentX, CurrentY;



	public void mouseClicked (MouseEvent event) 
	{
	   if (counter < 2)
	   {
	   for(int x = 0; x < Boxes.length; x++)
            for(int y = 0 ; y < Boxes[x].length; y++)
              if (Boxes[x][y].ismoved(event.getX(), event.getY()) && Boxes[x][y].Clicked != true)
              {
	        counter++;
                Boxes[x][y].setColor(getRanColor()); //setting the box color to lightgray
		Boxes[x][y].setClicked();
		break;                    //breaking out of the loop
                }
	 if( counter == 2)
	   counter = 0; //Reset Counter
	
	}
     repaint();
     }  
     public void mousePressed (MouseEvent event) {}
     public void mouseDragged (MouseEvent event) {}
   
    //change the color of the box at which the mouse are moving on
     public void mouseMoved (MouseEvent event) {}


    public void actionPerformed(ActionEvent e) {
	   init();
	    repaint();
	}



}//end GGame
 

