import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Scrabble implements ItemListener
{
	JFrame frame = new JFrame("Scrabble");
	JComponent c;
	
	HraciePole hraciePole ;
	Zasobnik zasobnik;
	Stack slovo = new Stack();
	char smer = '?';
	
	
	public  Vector Vrecusko = new Vector();
	
	JButton vymen = new JButton("Vymen");
	JButton zober = new JButton("Slovo");
	JTextField body = new JTextField("   ");
	public Pole[][] pole= new Pole[15][15];
	public JToggleButton[] zasob = new JToggleButton[7];	
	
	public static final String[] PISMENKA = 
	{
		"A","A","A","A","A","A","A","A","A",
		"A'",
		"'A'",
		"B","B",
		"C",
		"C~",
		"D","D","D",
		"D~",
		"E","E","E","E","E","E","E","E",
		"E'",
		"F",
		"G",
		"H",
		"I","I","I","I","I",
		"I'",
		"J","J",
		"K","K","K",	
		"L","L","L",
		"L'",//dlhe L
		"L~",//L s makcenom
		"M","M","M","M",
		"N","N","N","N","N",
		"N~",
		"O","O","O","O","O","O","O","O","O",
		"O'",
		"O~",//o s vokanom
		"P","P","P",
		"R","R","R","R",
		"R'",
		"S","S","S","S",
		"S~",
		"T","T","T","T",
		"T~",
		"U","U",
		"U'",
		"V","V","V","V",
		"X",
		"Y",
		"Y'",
		"Z",
		"Z~",
		"?","?"//zolik
	} ;
	public static final int[] BODY = 
	{
	1,1,1,1,1,1,1,1,1,
	4,
	10,
	4,4,
	4,
	5,
	2,2,2,
	8,
	1,1,1,1,1,1,1,1,
	7,
	8,
	8,
	4,
	1,1,1,1,1,
	1,
	3,3,
	2,2,2,
	2,2,2,
	7,
	10,
	2,2,2,2,
	1,1,1,1,1,
	8,
	1,1,1,1,1,1,1,1,1,
	10,
	8,
	2,2,2,
	1,1,1,1,
	10,
	1,1,1,1,
	5,
	1,1,1,1,
	7,
	3,3,
	7,
	1,1,1,1,
	10,
	4,
	5,
	4,
	5,
	0,0	
	};
	public Scrabble()
	{	//System.out.println("Dlzka="+Scrabble.PISMENKA.length);
		//System.out.println("Dlzka="+Scrabble.BODY.length);
		
		for (int i=0;i<100;i++)
			Vrecusko.addElement(new Kococka( (byte)Scrabble.BODY[i],Scrabble.PISMENKA[i] ));
		String[] zoznam = {"Abcd","Bcde","Cdef","Efgh"};
		hraciePole = new HraciePole();
		zasobnik = new Zasobnik();
		
		JPanel info = new JPanel();
			info.setLayout(new BorderLayout());
			info.add( new JList(zoznam),BorderLayout.NORTH );
		
				JPanel bodyPanel = new JPanel();
					bodyPanel.setLayout(new FlowLayout());
					bodyPanel.add( new Label("BODY= ") );
					bodyPanel.add(body);
					
			info.add(bodyPanel,BorderLayout.CENTER);
			
				JPanel tlacitkaPanel = new JPanel();
					tlacitkaPanel.setLayout(new GridLayout(1,0));					
					tlacitkaPanel.add(zober);		
					tlacitkaPanel.add(vymen);
			info.add(tlacitkaPanel,BorderLayout.SOUTH);		
				JPanel infoPanel = new JPanel();
					infoPanel.setLayout(new BorderLayout());
					infoPanel.add(info,BorderLayout.NORTH);
		
				JPanel hraciePolePanel = new JPanel();
					hraciePolePanel.setLayout(new GridLayout(15,15));
					for (int i=0;i<15;i++)
						for (int j=0;j<15;j++) 
						{
							pole[i][j] = new Pole(i,j,"-");					
							hraciePolePanel.add( pole[i][j] );
							pole[i][j].addItemListener(this);
							pole[i][j].setBackground(Color.white);
						}
				
				JPanel zasobnikPanel = new JPanel();
					for(int i=0;i<7;i++)	
					{
						zasob[i] = new JToggleButton("-");
						zasob[i].setToolTipText(zasobnik.policka[i].label);
						zasobnikPanel.add(zasob[i]);
					}
			
		c =(JComponent) frame.getContentPane();
		c.setLayout(new BorderLayout());
		c.add(hraciePolePanel,BorderLayout.CENTER);
		c.add(zasobnikPanel,BorderLayout.SOUTH);		
		c.add(infoPanel,BorderLayout.EAST);
		
		frame.addWindowListener(new WindowAdapter() 
			{
      		public void windowClosing(WindowEvent e) {System.exit(0);}
    		});		
		frame.setSize(800,600);
		frame.setVisible(true);
	}
	public static byte getPismenkovaPremiaAt(int index,int index2)
	{
		index = index+1;
		index2 = index2+1;
		byte kolko = (byte)1;
		switch (index)
		{
			case 1: if ( (index2 == 4) || (index2 == 12) ) kolko=(byte)2; break;			
			case 2:  if ( (index2 == 6) || (index2 == 10 ) )	kolko = (byte)3;break;
			case 3:	if ( (index2 == 7) || (index2 == 9 ) )	kolko = (byte)2;break;
			case 4:	if ( (index2 == 1) || (index2 == 8) || (index2 == 15) )	kolko = (byte)2;break;
			case 6:	if ( (index2 == 2) || (index2 == 6) || (index2 == 10)|| (index2 == 14) ) kolko = (byte)3;break;
			case 7:	if ( (index2 == 3) || (index2 == 7) || (index2 == 9)|| (index2 == 13) ) kolko = (byte)2;break;
			
			case 8: if ( (index2 == 4) || (index2 == 12) ) kolko=(byte)2; break;
			
			case 9:	if ( (index2 == 3) || (index2 == 7) || (index2 == 9)|| (index2 == 13) ) kolko = (byte)2;break;
			case 10:	if ( (index2 == 2) || (index2 == 6) || (index2 == 10)|| (index2 == 14) ) kolko = (byte)3;break;			
			case 12:	if ( (index2 == 1) || (index2 == 8) || (index2 == 15) )	kolko = (byte)2;break;
			case 13:	if ( (index2 == 7) || (index2 == 9 ) )	kolko = (byte)2;break;
			case 14:	if ( (index2 == 6) || (index2 == 10 ) )	kolko = (byte)3;break;
			case 15: if ( (index2 == 4) || (index2 == 12) ) kolko=(byte)2; break;			
		}
		
		return kolko;
	}
	public static byte getSlovnaPremiaAt(int index,int index2)
	{
		index = index+1;
		index2 = index2+1;
		byte kolko = (byte)1;
		switch (index)
		{
			case 1: if ( (index2 == 1) || (index2 == 8) || (index2 == 15) ) kolko=(byte)3; break;
			case 8: if ( (index2 == 1) ||  (index2 == 15) ) kolko=(byte)3;
					  if ( index2 == 8 )	kolko = (byte)2;
					  break;
			case 2:  if ( (index2 == 2) || (index2 == 14 ) )	kolko = (byte)2;break;
			case 3:	if ( (index2 == 3) || (index2 == 13 ) )	kolko = (byte)2;break;
			case 4:	if ( (index2 == 4) || (index2 == 12) )	kolko = (byte)2;break;
			case 5:	if ( (index2 == 5) || (index2 == 11) )	kolko = (byte)2;break;
			case 11:	if ( (index2 == 11) || (index2 == 5) )	kolko = (byte)2;break;
			case 12:	if ( (index2 == 12) || (index2 == 4) )	kolko = (byte)2;break;
			case 13:	if ( (index2 == 13) || (index2 == 3) )	kolko = (byte)2;break;
			case 14:	if ( (index2 == 14) || (index2 == 2) )	kolko = (byte)2;break;
			case 15: if ( (index2 == 1) || (index2 == 8) || (index2 == 15) ) kolko=(byte)3; break;
		}
		return kolko;
	}
	public void doplnZasobnik()
	{
		for (int i=0;i<7;i++)
		{
			if ( ( zasobnik.isEmpty(i) )&& (!Vrecusko.isEmpty()) )
			{ 
				Kococka koc = getRandomKococka();
				zasobnik.setKocockaAt( (byte)i, koc);
				zasob[i].setToolTipText( ""+koc.body );								
				zasob[i].setLabel(koc.label);
			}				
		}
		
	}
	public void itemStateChanged(ItemEvent e)
	{
		byte countSelected = 0;
		int indexOfSelected =1;
		for (int i=0;i<7;i++)		
			if ( zasob[i].isSelected() )
			{
				countSelected++;
				indexOfSelected = i;
			}			
		if ( 	countSelected == (byte)1	)
		{
			Kococka koc = zasobnik.getANDsetEmptyAt(indexOfSelected);							
			Pole tl = (Pole)e.getSource();			
			if ( hraciePole.isEmpty(tl.x,tl.y) )
			{
				zasob[indexOfSelected].setToolTipText( "");
				zasob[indexOfSelected].setLabel("-");
				zasob[indexOfSelected].setSelected(false);						
			
				tl.setKococka(koc);
				tl.setSelected(false);
				
				hraciePole.setKocockaAt(tl.x,tl.y,koc);				
				/*if (slovo.size()==1) 
					{
						if (dx==0) smer = 'Z';
						else smer = 'V';
					}*/
				slovo.push( new Slovo(tl.x,tl.y,indexOfSelected) );
			}
			tl.setSelected(false);
		}
						
	}
	public void go()
	{
		doplnZasobnik();
		
		vymen.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e) 
				{
					for (int i=0;i<7;i++)
					{
						if ( (zasob[i].isSelected())&&(!zasobnik.isEmpty(i)) )
						{ 
							Vrecusko.addElement(zasobnik.getANDsetEmptyAt(i));
							Kococka koc = getRandomKococka();
							zasobnik.setKocockaAt( (byte)i,koc );
							zasob[i].setToolTipText( ""+koc.body );
							zasob[i].setLabel(koc.label);
							zasob[i].setSelected(false);
						}
					}
				}
			}
		);//end vymen.addActionListener
		
		zober.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e) 
				{
					int slovoPremia = 1;
					int pismenoPremia = 1;
					int bodyZaSlovo = 0;
					for (int i=0;i<slovo.size();i++)
					{
						int xx = ( (Slovo)slovo.elementAt(i) ).hraciePoleX;
						int yy = ( (Slovo)slovo.elementAt(i) ).hraciePoleY;
						slovoPremia = slovoPremia*getSlovnaPremiaAt(xx,yy);
						bodyZaSlovo = bodyZaSlovo+(hraciePole.policka[xx][yy].body*getPismenkovaPremiaAt(xx,yy));
						//System.out.println(i+"_"+pole[xx][yy].toString()+"~"+hraciePole.policka[xx][yy].body+" Pism="+getPismenkovaPremiaAt(xx,yy)+" Slov="+getSlovnaPremiaAt(xx,yy));
						pole[xx][yy].zmenFarbu();
					}
					bodyZaSlovo = bodyZaSlovo * slovoPremia;
					if (slovo.size() == 7) bodyZaSlovo = bodyZaSlovo+50;
					body.setText(""+bodyZaSlovo);
					slovo.removeAllElements();
					doplnZasobnik();
					
				}
			}
		);//end zober.addActionListener		
	}	
	public Kococka getRandomKococka()
	{		
		boolean vyhovuje = true;
		byte rnd = 0;
		do{
				rnd =(byte) (Math.round( Vrecusko.size()*Math.random()) );
				vyhovuje = rnd<Vrecusko.size();
		}while(!vyhovuje);
		Kococka koc = (Kococka) Vrecusko.elementAt(rnd);
		Vrecusko.removeElementAt(rnd);
		if (Vrecusko.isEmpty()) System.exit(0);
			
		return koc;
	}	
	public static void main(String[] vstup)
	{
		Scrabble scrabble = new Scrabble();
		scrabble.go();		
		if (scrabble.Vrecusko.isEmpty()) System.exit(0);		
	}

}

class HraciePole
{	
	public Kococka[][] policka;
	
	HraciePole ()
	{
		policka = new Kococka[15][15];
		for (int i=0;i<15;i++)
			for (int j=0;j<15;j++) policka[i][j] = new Kococka("",(byte)(-1));
	}
	void setKocockaAt(int index1,int index2,Kococka kococka)
	{
		policka[index1][index2] = kococka;
	}
	void setEmptyAt(int index,int index2)
	{
		policka[index][index2] = new Kococka("",(byte)(-1));
	}
	Kococka getANDsetEmptyAt(int index,int index2)
	{
		Kococka old = policka[index][index2];
		policka[index][index2] = new Kococka("",(byte)(-1));
		return old;
	}
	boolean isEmpty(int index,int index2)
	{
		return policka[index][index2].body == (byte)(-1);
	}
}


class Kococka 
{
	public String label;
	public byte body;
	
	Kococka (String lab,byte prem)	
	{
		label = lab;
		body = prem;
	}	
	Kococka (byte prem,String lab)	
	{
		label = lab;
		body = prem;
	}	
}

class Zasobnik
{
	public static final int VELKOST_ZASOBNIKA = 7;
	Kococka policka[];
	
	Zasobnik ()
 	{		
		policka = new Kococka[VELKOST_ZASOBNIKA];
		for (int i=0;i<VELKOST_ZASOBNIKA;i++) policka[i] = new Kococka("",(byte)(-1));
	}
	void setKocockaAt(int index,Kococka kococka)
	{
		policka[index] = kococka;
	}
	void setEmptyAt(int index)
	{
		policka[index] = new Kococka("",(byte)(-1));
	}
	Kococka getANDsetEmptyAt(int index)
	{
		Kococka old = policka[index];
		policka[index] = new Kococka("",(byte)(-1));
		return old;
	}
	boolean isEmpty(int index)
	{
		return policka[index].body == (byte)(-1);
	}
}
class Slovo
{
	public int hraciePoleX,hraciePoleY,zasobnikIndex;
	public Slovo(int a,int b,int c)	
	{
		hraciePoleX =a;
		hraciePoleY=b;
		zasobnikIndex=c;
	}
	public String toString()
	{
		return "";
	}
}
class Pole extends JToggleButton
{
	public int x,y;
	Pole(int xx,int yy,String label)
	{
		super(label);
		x=xx;y=yy;
	}	
	public void setKococka(Kococka koc)	
	{
		setLabel(koc.label);
		setToolTipText(x+"-"+y+".."+getLabel()+" "+koc.body);
	}
	public String toString()
	{
		return x+"-"+y+".."+getLabel();
	}	
	public void zmenFarbu()
	{
		setBackground(Color.yellow);
	}
	
}

