/* jeu2PF.java - 09/05/2003
 * modification et ajout de main() le 09/02/18
 */

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;

public class jeu2PF extends java.applet.Applet implements ActionListener {
  static final long serialVersionUID = 180209L;
  Button pile, face, raz;
  Random rnd;
  int n;
  String s, s1;
  Panel p;
  Centre c;

  private Button ajoutb (String s)
  { Button b = new Button (s);
    b.addActionListener (this);
    p.add (b);
    return b;
  }


  public void init ()
  { setLayout(new BorderLayout());
    p = new Panel ();
    p.setBackground (Color.lightGray);
    c = new Centre ();
    add (p, BorderLayout.NORTH);
    add (c, BorderLayout.CENTER);
    pile = ajoutb ("Pile");
    face = ajoutb ("Face");
    raz = ajoutb ("Remise à zéro");
    s = "Prêt à jouer ?";
    s1 = "Appuyer sur le bouton \"Pile\" ou le bouton \"Face\"";
    rnd = new Random ();
  }

  public void actionPerformed (ActionEvent e)
  { boolean b = (rnd.nextDouble() < 0.375); // b vrai l'ordi joue "Pile"
    if (e.getSource () == raz) n = 0;
    else if (e.getSource () == pile)
    { if (b)
      { n += 3;
        s = "\"Pile-Pile\" vous venez de gagner 3 euros !";
        c.neuros = 3;
      }
      else
      { n -= 2;
        s = "\"Pile-Face\" vous avez perdu 2 euros !";
        c.neuros = - 2;
      }
    }
    else if (e.getSource () == face)
    { if (b)
      { n -= 2;
        s = "\"Face-Pile\" vous avez perdu 2 euros !";
        c.neuros = - 2;
      }
      else
      { n += 1;
        s = "\"Face-Face\" vous venez de gagner 1 euro !";
        c.neuros = 1;
      }
    }
    if (n > 1) s1 ="Vous possédez maintenant " + n + " euros";
    else if (n == 1) s1 ="Vous possédez maintenant 1 euro";
    else if (n == -1) s1 ="Vous devez à votre adversaire 1 euro";
    else if (n < -1) s1 ="Vous devez à votre adversaire " + (- n) + " euros";
    else s1 = "Vous n'avez rien !";
    c.repaint ();
  }

protected class Centre extends Canvas {
  static final long serialVersionUID = 180209L;
  int neuros;

  public void paint (Graphics g) {
	int w = getSize().width;
    int h = getSize().height;
    g.setColor (Color.white);
    g.fillRect (0, 0, -- w, -- h);
    g.setColor (Color.black);
    g.drawRect (0, 0, w, h);
    g.drawString (s, 20, 40);
    g.drawString (s1, 20, 60);
    Color couleur = Color.yellow;
    if (neuros < 0) couleur = Color.gray;
    int aneuros = Math.abs (neuros);
    for (int i = 0; i < aneuros; i ++) paintEuro (g, 10 + i * 50, 80, couleur);
  }

// dessin euro

  private void paintEuro (Graphics g, int x, int y, Color couleur)
//   int x = 10; int y = 80;
  { int diametre = 40;
    int rayon = diametre / 2;
    int rs2 = rayon / 2;
    g.setColor (couleur);
    g.fillOval (x, y, diametre, diametre);
    g.setColor (Color.black);
    g.drawArc (x + rs2, y + rs2, rayon, rayon, 80, 195);
    g.drawLine (x + rs2 - 3, y + rayon, x + rayon, y + rayon);
    g.drawLine (x + rs2 - 3, y + rayon + 3, x + rayon, y + rayon + 3);

  }

}

////////////////////////////////////////////////////////////////////////////////

	public static void main (String [] args) {
		jeu2PF a = new jeu2PF();
		a.init();
		a.start();

		Frame f = new Frame ("jeu2PF");
		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);
		}
	}
}
