import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.io.*;

public class LegespielMiniJava extends Applet{ 
  
 public static String readString(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        if (s == null)
            System.exit(0);
        return s;
    }

    public static String readString() {
        return readString("Eingabe:");
    }

    public static int readInt(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        int x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Integer.parseInt(s.trim());
        } catch (NumberFormatException e) {
            x = readInt(text);
        }
        return x;
    }

    public static int readInt() {
        return readInt("Geben Sie eine ganze Zahl ein:");
    }

    public static int read(String text) {
        return readInt(text);
    }

    public static int read() {
        return readInt();
    }

    public static double readDouble(String text) {
        JFrame frame = new JFrame();
        String s = JOptionPane.showInputDialog(frame, text);
        frame.dispose();

        double x = 0;
        if (s == null)
            System.exit(0);
        try {
            x = Double.parseDouble(s.trim());
        } catch (NumberFormatException e) {
            x = readDouble(text);
        }
        return x;
    }

    public static double readDouble() {
        return readDouble("Geben Sie eine Zahl ein:");
    }

    public static void write(String output) {
        JFrame frame = new JFrame();
        JOptionPane.showMessageDialog(frame, output, "Ausgabe", JOptionPane.PLAIN_MESSAGE);
        frame.dispose();
    }

    public static void write(int output) {
        write("" + output);
    }

    public static void write(double output) {
        write("" + output);
    }

  private class Field extends JPanel {
        boolean black = false; 
	Point p;
	int x, y;
	MyMouseListener mml;
        public Field(int x, int y,boolean black){
	    this.black=black;
	    this.x=x;
	    this.y=y;
	    p = getLocation();
	    mml= new MyMouseListener();
	    this.addMouseListener(mml);
        }
	
	public void paint(Graphics g) {
	    super.paint(g);
	    if (black){
		GradientPaint gradient = new GradientPaint(10, 50, Color.GRAY, getWidth(), 0, Color.DARK_GRAY);
		((Graphics2D)g).setPaint(gradient);
	    }else{ 
		GradientPaint gradient = new GradientPaint(0, 50, Color.WHITE, getWidth(), 0, Color.GRAY);
		((Graphics2D)g).setPaint(gradient);
	    }
	    g.fillRect(p.getLocation().x,p.getLocation().y,getWidth()*2,getHeight());
	    if(spielFeld[x][y] ==1){
		GradientPaint gradient = new GradientPaint(15, 0, Color.WHITE, getWidth(), 0, Color.LIGHT_GRAY);
		((Graphics2D)g).setPaint(gradient);
		((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						 RenderingHints.VALUE_ANTIALIAS_ON);
	
		g.fillOval((int)(getWidth()*.3) ,(int)(getHeight()*.3),(int)(getWidth()*.5),(int)(getHeight()*.5));
	    }else if(spielFeld[x][y] == -1){
		GradientPaint gradient = new GradientPaint(0, 0, Color.BLACK, getWidth(), 0, Color.BLACK);
		((Graphics2D)g).setPaint(gradient);
		((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						 RenderingHints.VALUE_ANTIALIAS_ON);
	
		g.fillOval((int)(getWidth()*.3) ,(int)(getHeight()*.3),(int)(getWidth()*.5),(int)(getHeight()*.5));
	   
	    }else{}
	}
	class MyMouseListener extends MouseAdapter{ 
	    public void mousePressed(MouseEvent e){ 
		if (spielFeld[x][y]==0)
		    klick(x,y);
		return;
	    }
	
	}

    }
    private boolean finished = false;
    private JFrame myFrame = new JFrame("Spielfeld");
    private JPanel pan = new JPanel();
    private int[][] spielFeld;
    private LinkedList<int[]> q = new LinkedList<int[]>();
    private Object warteschlange = new Object();
    private void klick(int x, int y){
	int [] xy = new int[2];
	xy[0]=x;
	xy[1]=y;
	synchronized(warteschlange){
	    q.add(xy);
	    warteschlange.notify();
	}
    }
    private int[] getPosition(){
        if (finished) return null;
        synchronized(warteschlange){
            if (q.size()==0) 
                try{
		    warteschlange.wait();
                    if (q.size()==0) return null;
                }catch(InterruptedException ie){
                }
            return q.poll();
        }
    }
    private void start(int[][] spielfeld){
	spielFeld = new int[spielfeld.length][spielfeld[0].length];
	pan.setLayout(new GridLayout(spielFeld.length,spielFeld[0].length));
	Field [][] field = new Field[spielFeld.length][spielFeld[0].length];
	for (int y = 0; y < spielFeld.length; y++)
	    for (int x = 0; x < spielFeld[y].length; x++){
		field[x][y] = new Field(x,y,(x+y)%2 == 1);
		pan.add(field[x][y]);
		spielFeld[x][y] = spielfeld[x][y];
	    }
    
        myFrame.add(pan);
        myFrame.setSize(400,400*spielfeld.length/spielfeld[0].length);
        myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        myFrame.setVisible(true);
	repaint();

    }
    protected LegespielMiniJava(){
    }
    private void updateField(int[][] feld){
	if (feld.length!=spielFeld.length) 
	    System.err.println("Spielfeld darf sich nicht vergroessern/verkleinern...");
	if (feld[0].length!=spielFeld[0].length) 
	    System.err.println("Spielfeld darf sich nicht vergroessern/verkleinern...");

	int punkteC =0;
	int punkteS=0;
	int freie =0;
	for (int x = 0 ; x < spielFeld.length; x ++)
	    for (int y = 0; y < spielFeld[0].length; y++){
		spielFeld[x][y] = feld[x][y];
		if(spielFeld[x][y] == -1) punkteC++;
		if(spielFeld[x][y] == 1) punkteS++;

	    }
	pan.repaint();
	if(punkteC + punkteS == spielFeld.length*spielFeld[0].length){
	    if(punkteC>punkteS)
		JOptionPane.showMessageDialog(null, "Computer hat mit "+punkteC+" Steinen gewonnen!", "Verlierer!", JOptionPane.PLAIN_MESSAGE);
	    else
		JOptionPane.showMessageDialog(null, "Spieler hat mit "+punkteS+" Steinen gewonnen!", "Sieger!", JOptionPane.PLAIN_MESSAGE);
            synchronized(warteschlange){
                finished=true;
                q.clear();
                warteschlange.notify();
            }

	}
    }
    private static LegespielMiniJava r = new LegespielMiniJava();
    public static void initLegespiel(int [][] feld){
	r.start(feld);
    }
    public static void updateLegespiel(int [][]feld){
	r.updateField(feld);
    }
    public static int[] getSpielerZug(){
	return r.getPosition();
    }
}
