/*
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;

/**
 */
public
class weight extends Applet implements MouseListener {
    /**
     * Dubble Buffering
     */
     private Image dbImage;
     private Graphics dbg;


    /**
     * Kugel vorhanden
     */
    int exkug[][];

    /**
     * Anzahl der Kugeln in der Waage
     */
    int height[] = {12,0,0,0,0,0,0};

    /**
     * Welche wägung ist aktuell
     */
    int round;
    
    /**
     * falsche Kugel
     */
    int gewicht[] = {0,3,3,3,3,3,3,3,3,3,3,3,3};
    int wert;
    int zahl;

    /**
     * Sieg oder Niederlage
     */
    int status;
    int won;
    int lost;

    /**
     * Ergebnisse der Wägungen
     */
    static final int OPEN = 0;
    static final int LEFT = 1;
    static final int EQUAL = 2;
    static final int RIGHT = 3;
    int ergebnis[] = {OPEN, OPEN, OPEN};

    /**
     * Die Bilder für die Kugeln
     */
    Image kug[];

    /**
     * Initialize the applet. Resize and load images.
     */
    public void init() {
        kug = new Image[13];
        kug[1] = getImage(getCodeBase(), "images/k1.gif");
        kug[2] = getImage(getCodeBase(), "images/k2.gif");
        kug[3] = getImage(getCodeBase(), "images/k3.gif");
        kug[4] = getImage(getCodeBase(), "images/k4.gif");
        kug[5] = getImage(getCodeBase(), "images/k5.gif");
        kug[6] = getImage(getCodeBase(), "images/k6.gif");
        kug[7] = getImage(getCodeBase(), "images/k7.gif");
        kug[8] = getImage(getCodeBase(), "images/k8.gif");
        kug[9] = getImage(getCodeBase(), "images/k9.gif");
        kug[10] = getImage(getCodeBase(), "images/k10.gif");
        kug[11] = getImage(getCodeBase(), "images/k11.gif");
        kug[12] = getImage(getCodeBase(), "images/k12.gif");
 
        exkug = new int[7][13];
        for (int i=1;i<13;i++) {
            exkug[0][i]=1;
            for (int j=1;j<7;j++) {
              exkug[j][i]=0;
            }
        }

        //zufallszahl
        zahl = (int)(Math.random()*12)+1;;
        wert = (int)(Math.random()*2);
        gewicht[zahl]=2+2*wert;
        

        round=0;

        ergebnis[1]=OPEN;
        ergebnis[2]=OPEN;
        ergebnis[0]=OPEN;

	height[0]=12;
        height[1]=0;
        height[2]=0;
        height[3]=0;
        height[4]=0;
        height[5]=0;
        height[6]=0;

	addMouseListener(this);
    }

    public void myinit() {
        for (int i=1;i<13;i++) {
            exkug[0][i]=1;
            for (int j=1;j<7;j++) {
              exkug[j][i]=0;
            }
            gewicht[i]=3;
        }

        //zufallszahl
        zahl = (int)(Math.random()*12)+1;;
        wert = (int)(Math.random()*2);
        gewicht[zahl]=2+2*wert;
        

        round=0;

	status=0;

        ergebnis[1]=OPEN;
        ergebnis[2]=OPEN;
        ergebnis[0]=OPEN;

	height[0]=12;
        height[1]=0;
        height[2]=0;
        height[3]=0;
        height[4]=0;
        height[5]=0;
        height[6]=0;

    }

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

    /**
     * Paint it.
     */
    public void paint(Graphics g) {
	Dimension d = getSize();
          g.setColor(Color.blue);
          g.fill3DRect(75, 20, 60, 20, true);  //RESTART
          g.fill3DRect(155,20, 60, 20, true);  //Wiegen
          g.setColor(Color.red);
          g.drawString("RESTART", 77, 38);
          g.drawString("Wiegen", 157, 38);
	  //g.drawString(""+zahl+" "+wert+" "+round, 230,38);
          g.drawString("Gewonnen "+won+"  Verloren "+lost, 77, 65);
	if (round == 3) {
	  g.drawString("Bitte die falsche Kugel waehlen",100,80);
	}
	if (status == 1) {
          g.drawString("Gewonnen", 100,80);
        } else if (status == 2) {
          g.drawString("Leider falsch", 100,80);
        }


	g.setColor(Color.black);
        g.drawLine(25,20,25,380);
        g.drawLine(25,380,55,380);
        g.drawLine(55,380,55,20);
	
        //Ergebnisse der letzten Wägungen
        for (int i=0; i<3; i++) {
          switch(ergebnis[i]) {
            case OPEN:
              break;
            case LEFT:
              g.drawString(">",i*100+110,200);
              break;
            case RIGHT:
              g.drawString("<",i*100+110,200);
              break;
	    case EQUAL:
              g.drawString("=",i*100+110,200);
              break;
           }
        }

        for (int r=1; r<7; r++) {
          g.drawLine(r*50+25,200,r*50+25,380);
          g.drawLine(r*50+25,380,r*50+55,380);
          g.drawLine(r*50+55,380,r*50+55,200);
        }

        
        int h[] = {0,0,0,0,0,0,0};
        for (int r = 1 ; r < 13; r++) {
            for (int c = 0 ; c < 7; c++) {
               if (exkug[c][r] == 1) {
                  g.drawImage(kug[r], 25+c*50+1, 350-h[c]*30, this);
                  h[c]=h[c]+1;
               }
            }
        }
    }

    /** Update - Method, implements double buffering */
    public void update (Graphics g)
    {
    // initialize buffer
    if (dbImage == null)
    {
    dbImage = createImage (this.getSize().width, this.getSize().height);
    dbg = dbImage.getGraphics ();
    
    }
    
    // clear screen in background
    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
    
    // draw elements in background
    dbg.setColor (getForeground());
    paint (dbg);
    
    // draw image on the screen
    g.drawImage (dbImage, 0, 0, this);
    
    }
    
    /**
     * The user has clicked in the applet. Figure out where
     * and see if a legal move is possible. If it is a legal
     * move, respond with a legal move (if possible).
     */
    public void mouseReleased(MouseEvent e) {
	int x = e.getX();
	int y = e.getY();

        //unverteilt
        if ((round < 3) && (x > 25) && (x < 55) && (y > 20) && (y < 380)) {
	  int pos=(380-y)/30+1;
          for (int i=1; i<13; i++) {
            pos=pos-exkug[0][i];
            if (pos==0) {
             if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
		if (height[round*2+1] < 6) {
                  exkug[0][i]=0; 
                  exkug[round*2+1][i]=1;
                  height[0]--;
                  height[round*2+1]++;
		}
              } else {
		if (height[round*2+2] < 6) {
                  exkug[0][i]=0; 
                  exkug[round*2+2][i]=1;
                  height[0]--;
                  height[round*2+2]++;
		}
              }
              repaint();
              break;
            }
          }
        } else if ((round < 3) && (x > 2*round*50+75) && (x < 2*round*50+105) && (y < 380) && (y > 200)) {
          // linke Waagschale
          int pos=(380-y)/30+1;
          for (int i=1; i<13; i++) {
            pos=pos-exkug[2*round+1][i];
            if (pos==0) {
             if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
                exkug[2*round+1][i]=0;
                exkug[0][i]=1;
                height[0]++;
                height[round*2+1]--;
              } else {
		if (height[round*2+2] < 6) {
                  exkug[2*round+1][i]=0;
                  exkug[round*2+2][i]=1;
                  height[round*2+1]--;
                  height[round*2+2]++;
                }
              }
              repaint();
              break;
            }
          }
        } else if ((round < 3) && (x > 2*round*50+125) && (x < 2*round*50+155) && (y < 380) && (y > 200)) {
          // rechte Waagschale
          int pos=(380-y)/30+1;
          for (int i=1; i<13; i++) {
            pos=pos-exkug[2*round+2][i];
            if (pos==0) {
             if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
	        if (height[round*2+1] < 6) {
                  exkug[2*round+2][i]=0;
                  exkug[round*2+1][i]=1;
                  height[round*2+1]++;
                  height[round*2+2]--;
		}
              } else {
                exkug[2*round+2][i]=0;
                exkug[0][i]=1;
                height[round*2+2]--;
                height[0]++;
              }
              repaint();
              break;
            }
          }
        } else if ((round < 3) && (x > 155) && (x < 215) && (y > 20) && (y < 40)) {
          // wiegen
          int gesamt=0;
          for (int i=1;i<13; i++) {
            gesamt+=(exkug[round*2+2][i]-exkug[round*2+1][i])*gewicht[i];
          }
          if (gesamt < 0) { 
             ergebnis[round]=LEFT;
          }
          if (gesamt > 0) {
             ergebnis[round]=RIGHT;
          }
          if (gesamt == 0) {
             ergebnis[round]=EQUAL;
          }
          round++;
          for (int i=1;i<13; i++) {
            exkug[0][i]=1;
          }
          repaint();
        } else if ((round > 2 ) && (x > 25) && (x < 55) && (y > 20) && (y < 380)) {
          // tippen
          int pos=(380-y)/30+1;
          if (pos == zahl) {
             status=1;
             if (round == 3) {
               round++;
               won++;
             }
          } else {
             status=2;
             if (round == 3) {
               round++;
               lost++;
             }

          }
          repaint();
        } else if ((x > 75) && (x < 135) && (y > 20) && (y < 40)) {
          myinit();
          repaint();
        }

    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public String getAppletInfo() {
	return "TicTacToe by Arthur van Hoff";
    }
}

