/**
 * troisportes.java - jpq - 03/10/00 - 04/10/00 - 12/10/00
 * modification et ajout de main() le 19/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class troisportes extends Applet implements MouseListener, ActionListener {
  static final long serialVersionUID = 180219L;
  Button raz, np;
  Font f;
  Random rnd;
  Image img;
  Graphics g1;
  int niveau, npg, npp, numpc, numpo, numpg;
  int x1, y1, x1s3, x1s3m20, y1m60;

//----------------------------------------------------------------------------
  public void init ()
  { addMouseListener (this);
    f = new Font ("Arial", Font.PLAIN, 10);
    add (raz = new Button ("Remise à zéro"));
    raz.setFont (f);
    raz.addActionListener (this);
    add (np = new Button ("Nouvelle partie"));
    np.setFont (f);
    np.addActionListener (this);
    rnd = new Random ();
    niveau = npg = npp = 0;
  }

  private void paintporte (int x, int y, int l, int h, boolean b)
  { g1.setColor (Color.orange);
    g1.fillRect (x, y, l, h);
    g1.setColor (Color.blue);
    g1.drawRect (x, y, l, h);
    g1.drawRect (x + 5, y + 5, l - 10, h / 2 - 10);
    int yphs2 = y + h / 2;
    g1.drawRect (x + 5, yphs2 + 5, l - 10, h / 2 - 10);
    if (b) g1.fillRect (x + l - 13, yphs2 - 1, 10, 3);
    else
    { g1.drawLine (x + l - 7, yphs2 + 6, x + l - 3, yphs2 - 1);
      g1.drawLine (x + l - 7, yphs2 + 7, x + l - 3, yphs2);
      g1.drawLine (x + l - 7, yphs2 + 8, x + l - 3, yphs2 + 1); }
//    g1.drawLine (x + l - 13, yphs2, x + l - 3, yphs2);
  }


  public void update (Graphics g)
  { paint (g); }

  public void paint (Graphics g) {
	if (img == null || x1 != getSize().width || y1 != getSize().height) {
	  x1 = getSize().width;
	  y1 = getSize().height;
	  img = createImage (x1, y1);
      g1 = img.getGraphics();
      x1s3 = x1 / 3;
      x1s3m20 = x1s3 - 20;
      y1m60 = y1 - 60;
	}
    g1.setColor (Color.white);
    g1.fillRect (0, 0, x1, y1);
    g1.setColor (Color.black);
    g1.drawRect (0, 0, x1 - 1, y1 - 1);
    paintporte (10, 30, x1s3m20, y1m60, true);
    paintporte (x1s3 + 10, 30, x1s3m20, y1m60, true);
    paintporte (x1s3 * 2 + 10, 30, x1s3m20, y1m60, true);
    switch (niveau)
    { case 0 : numpg = (int)(rnd.nextDouble () * 3.0) + 1;
               break;
      case 2 : if (numpc == numpg)
               { npg ++;
                 g1.setColor (Color.white);
                 g1.fillRect (10 + (numpc - 1) * x1s3, 30, x1s3m20, y1m60);
                 g1.setColor (Color.blue);
                 g1.drawRect (10 + (numpc - 1) * x1s3, 30, x1s3m20, y1m60);
                 g1.setColor (Color.black);
                 g1.drawString ("gagné", 15 + (numpc - 1) * x1s3, y1 / 4);
                 int diametre = Math.min (x1s3 -15, y1m60 / 2);
                 int rayon = diametre / 2;
                 int rs2 = rayon / 2;
                 g1.setColor (Color.yellow);
                 g1.fillOval (15 + (numpc - 1) * x1s3, y1 / 2 - 15, diametre, diametre);
                 g1.setColor (Color.black);
                 g1.drawArc (35 + (numpc - 1) * x1s3, y1 / 2 + 5, rayon, rayon, 80, 195);
                 g1.drawLine (30 + (numpc - 1) * x1s3, y1 / 2 + rs2,
                              25 + (numpc - 1) * x1s3 + rayon, y1 / 2 + rs2);
                 g1.drawLine (30 + (numpc - 1) * x1s3, y1 / 2 + 6 + rs2,
                              25 + (numpc - 1) * x1s3 + rayon, y1 / 2 + 6 + rs2);
               }
               else
               { npp ++;
                 g1.setColor (Color.black);
                 g1.fillRect (10 + (numpc - 1) * x1s3, 30, x1s3m20, y1m60);
                 g1.setColor (Color.blue);
                 g1.drawRect (10 + (numpc - 1) * x1s3, 30, x1s3m20, y1m60);
                 g1.setColor (Color.white);
                 g1.drawString ("perdu", 15 + (numpc - 1) * x1s3, y1 / 2);
               }
               niveau ++;
			   break;
      case 1 : if (niveau == 1) paintporte (x1s3 * (numpc - 1) + 10, 30, x1s3m20, y1m60, false);
               g1.setColor (Color.blue);
               g1.drawRect (10 + (numpo - 1) * x1s3, 30, x1s3m20, y1m60);
               g1.setColor (Color.black);
               g1.fillRect (10 + (numpo - 1) * x1s3, 30, x1s3m20, y1m60);
    }
    g1.setColor (Color.blue);
   g1.drawString ("parties gagnées : " + Integer.toString (npg) + "; parties perdues : " + Integer.toString (npp), 10 , y1 - 10);
// pour tester
// g1.drawString (Integer.toString (numpc) + " " + Integer.toString (numpg)+ " " + Integer.toString (numpo), 10 , y1 - 10);
    g.drawImage (img, 0, 0, this);
  }

//

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == raz) npg = npp = 0;
    if ((e.getSource () == raz) || (e.getSource () == np))
    { niveau = 0;
      repaint ();
    }
  }

  public void mousePressed (MouseEvent e)
  { e.consume ();
    int x = e.getX ();
    int y = e.getY ();
    numpc = 0;
    if ((y > 30) && (y < y1m60))
     if ((x > 10) && (x < x1s3m20)) numpc = 1;
     else if ((x > x1s3 + 10) && (x < x1s3 + x1s3m20)) numpc = 2;
     else if ((x > x1s3 * 2 + 10) && (x < x1s3 * 2 + x1s3m20)) numpc = 3;
    if (numpc != 0)
    { if (niveau == 0)
      { niveau ++;
        do numpo = (int)(rnd.nextDouble () * 3.0) + 1;
        while ((numpo == numpc) || (numpo == numpg));
        repaint ();
      }
      else if ((niveau == 1) && (numpc != numpo))
           { niveau ++;
             repaint ();
           }    
    }
  }

  public void mouseReleased (MouseEvent e) { }
  public void mouseClicked (MouseEvent e) { }
  public void mouseEntered (MouseEvent e) { }
  public void mouseExited (MouseEvent e) { }

  public String getAppletInfo ()
  { return "troisportes par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        troisportes a = new troisportes();
        a.init ();
        a.start ();

        Frame f = new Frame ("troisportes");
        f.addWindowListener (new fermer ());
        f.add (a);
        f.setSize (300, 200);
        f.setVisible (true);
    }

// Permet la fermeture de la fenêtre contenant l'applet
    protected static final class fermer extends WindowAdapter {
        public void windowClosing (WindowEvent e) {
            System.exit (0);
        }
    }

}
