import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.text.*;
 
public class Kings extends Applet implements MouseListener
{
	// Screen constants	
	static final int CARDW = 60; 
	static final int CARDH = 80; 
	static final int TWOSIZE = 50; 
	static final int MOVEW = 40; 
	static final int MOVEH = 40; 
	
	// the cards
	boolean Deck[] = new boolean[52]; 
	MediaTracker tracker = new MediaTracker(this);
	int HoldCard;
	Image Twos[] = new Image[4];
  
	// the Board
	Place Board[] = new Place[52];
	
	int Moves = 0;
	int InPlace;

	Random rand = new Random();

      Font smallFont = new Font("SansSerif", Font.BOLD, 12);
      Font BigFont = new Font("SansSerif", Font.BOLD, 24);

	// Set up clicking areas
	Rectangle TwoC = new Rectangle(130, 5, TWOSIZE, TWOSIZE);
	Rectangle TwoD = new Rectangle(185, 5, TWOSIZE, TWOSIZE);
	Rectangle TwoH = new Rectangle(240, 5, TWOSIZE, TWOSIZE);
	Rectangle TwoS = new Rectangle(295, 5, TWOSIZE, TWOSIZE);

    public void init() 
    {
       Twos[0] = getImage(getCodeBase(),"Images/2C.gif"); 
       Twos[1] = getImage(getCodeBase(),"Images/2D.gif"); 
       Twos[2] = getImage(getCodeBase(),"Images/2H.gif"); 
       Twos[3] = getImage(getCodeBase(),"Images/2S.gif"); 

	 setBackground(Color.white);
	 addMouseListener(this);
	 DealHand();
    }

   public void update(Graphics g)
   {
	paint(g);
   }
	
   public void paint(Graphics g)
   {
	MoveDisplay();
	PaintCards();
   }

   private void PaintCards()
   {
	Graphics g = getGraphics();
	try
	{
	   tracker.waitForID(0);
	}	
      catch(InterruptedException e){}

	int TempX = 2;
	int TempY = 60;
	g.clearRect(0, 59, 800, 327);
	int Card = 0;	
	for(int i = 0; i < 4; i++)
	{
	   for(int x = 0; x < 13; x++, Card++)
	   {
		if(Board[Card].Occupied)
		{
 		   g.drawImage(Board[Card].pic, TempX, TempY, TempX + 80, TempY + 80, 0, 0, 100, 100, this);
		   tracker.addImage(Board[Card].pic, 0);
		}
       	TempX += CARDW;
	   }			
	   TempX = 2;
	   TempY += CARDH;
	}
   }	

   private void MoveDisplay()
   {
	int MoveX = 430;
	String Display = null;
	Graphics g = getGraphics();
      g.clearRect(MoveX, 0, 130, 50);
	CalculateInPlace(); 
	for(int x = 0; x < 2; x++)
	{
	   g.setColor(Color.red);
	   g.fillRect(MoveX, 2, MOVEW, MOVEH);
	   g.setColor(Color.black);
	   g.fillRect(MoveX, 2, MOVEW, 3);
	   g.fillRect(MoveX, 2, 3, MOVEH);
	   g.fillRect(MoveX + MOVEW, 2, 3, MOVEH);
	   g.fillRect(MoveX, MOVEH + 2, MOVEW + 3, 3);
	   g.setFont(smallFont);
	   if(x == 0)
	   {	
		Display = String.valueOf(Moves);
       	g.drawString("Moves", MoveX + 2, 56);
	   }
	   else
	   {	
		Display = String.valueOf(InPlace);	
       	g.drawString("In Place", MoveX + 2, 56);
	   }
  	   g.setFont(BigFont);
	   g.drawString(Display, MoveX + 7, 35);
	   g.setColor(Color.white);
    	   g.drawString(Display, MoveX + 6, 34);
	   MoveX += 70;
	}
   }	 	

   private void CalculateInPlace()
   {
	InPlace = 0;
	for(int x = 0; x < 40; x += 13)
	{
	   if((Board[x].Value == 2)&&(Board[x].Occupied))
	   {
		int suit = Board[x].Suit;
		InPlace += 1;
		for(int i = 1; i < 12; i++)
		{
		   if((Board[x+i].Value==i + 2)&&(Board[x+i].Suit == suit))
			 InPlace += 1;
		}
	   }
	}
   }
					
   private void ShowTwos()
   {
	Graphics g = getGraphics();
	int TempX = 130;
	int TempY = 5;
	g.setFont(smallFont);
	g.drawString("Please Select One:", 10, 35);
	g.fillRect(125, 0, 220, 57);
	for(int x = 0; x < 4; x++)
      {
 	   g.drawImage(Twos[x], TempX, TempY, TempX + TWOSIZE, TempY + TWOSIZE, 0, 0, 100, 100, this);
	   TempX += 55;
	}
	repaint();
   }
			
   private void MoveCard(int Card)
   {
	int suit = Board[Card-1].Suit;
	int value = Board[Card-1].Value;
	if(value != 13)
	{	
	   for(int x = 0; x < 52; x++)
         {
		if((Board[x].Suit == suit)&&(Board[x].Value == value + 1)&&(Board[x].Occupied))
		{
		   Board[Card]= new Place(suit, value + 1);
		   Board[x].Occupied = false;
		   break;
		}
	   }
	   repaint();
	} 
   }	

   private void MoveTwo(int y)
   {
	int suit = y;
      for(int x = 0; x < 52; x++)
      {
	   if((Board[x].Suit == suit)&&(Board[x].Value == 2)&&(Board[x].Occupied))
	   {
		Board[HoldCard]= new Place(suit, 2);
		Board[x].Occupied = false;
	      Moves += 1;
		break;
	   }
	} 
	repaint();
   }	

   private void DealHand()
   {
	int card;
	for(int x = 0; x < 52; x++)
	   Deck[x] = false;
	for(int i = 0; i < 52; i++)
	{
         while(true)
	   {
		card = (int)(Math.random() * 52);
		if(Deck[card] == false)
		   break;
	   }
         Board[i] = new Place(card);
	   Deck[card] = true;
	} 
	repaint();
   }

// The user has clicked the applet. Figure out where and what they
// want.
   public void mouseReleased(MouseEvent e)
   {
	Graphics g = getGraphics();
	int MEx = e.getX();
	int MEy = e.getY();
	int TempX = 2;
	int TempY = 60;
	int Card = 0;
	for(int i = 0; i < 4; i++)
	{
	   for(int x = 0; x < 13; x++, Card++)
	   {
		if((MEx > TempX) && (MEx < TempX + CARDW) && (MEy > TempY) && (MEy < TempY + CARDH) && (!Board[Card].Occupied))
	      {
		   if((Card % 13) == 0)
		   {
			HoldCard = Card;
			ShowTwos();
		   }
		   else
		   {
			if(!Board[Card - 1].Occupied)
			   break;
		      MoveCard(Card);
		      Moves += 1;
		   }
		   break;
		}
       	TempX += CARDW;
	   }			
	   TempX = 2;
	   TempY += CARDH;
	}

	if(TwoC.contains(MEx, MEy))
	{
  	   g.clearRect(0, 0, 345, 57);
	   MoveTwo(0);
	}
	if(TwoD.contains(MEx, MEy))
	{
  	   g.clearRect(0, 0, 345, 57);
	   MoveTwo(1);
	}
	if(TwoH.contains(MEx, MEy))
	{
  	   g.clearRect(0, 0, 345, 57);
	   MoveTwo(2);
	}
	if(TwoS.contains(MEx, MEy))
	{
  	   g.clearRect(0, 0, 345, 57);
	   MoveTwo(3);
	}
   }

   public void mousePressed(MouseEvent e) {}

   public void mouseClicked(MouseEvent e) {}

   public void mouseEntered(MouseEvent e) {}

   public void mouseExited(MouseEvent e) {}

   public void destroy()
   {
      removeMouseListener(this);
   }

   class Place
   {
      // 0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades
	// 11=Jack, 12=Queen, 13=King 1=Ace 

	public boolean Occupied;
	public int Value;
	public int Suit;
	public String Name;
	public Image pic; 
	private String theSuit;

	public Place(int x)
	{
	   Suit = x % 4;
	   if(Suit == 0)	
	   	theSuit = "C";
	   else if(Suit == 1)	
	   	theSuit = "D";
	   else if(Suit == 2)	
	   	theSuit = "H";
	   else
	   	theSuit = "S";
	   Value = x % 13;
 	   if(Value == 0)
		Value = 13;
 	   Name = String.valueOf(Value) + theSuit;
	   pic = getImage(getCodeBase(),"Images/" + Name + ".gif"); 
	   Occupied	= (Value == 1) ? false : true; 
	}

	public Place(int suit, int value)
	{
	   Suit = suit;
	   Value = value;
	   if(Suit == 0)	
	   	theSuit = "C";
	   else if(Suit == 1)	
	   	theSuit = "D";
	   else if(Suit == 2)	
	   	theSuit = "H";
	   else
	   	theSuit = "S";
 	   Name = String.valueOf(Value) + theSuit;
	   pic = getImage(getCodeBase(),"Images/" + Name + ".gif"); 
	   Occupied	= true; 
	}
   } 		
}
