import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import markov.*;
import typedonnee.ProbasDate;
 
public class Des extends JApplet implements ActionListener
{
 
    public void init()
    {
        texte = new JTextArea();
        texte.setLineWrap(true);
        texte.setEditable(false);
        view = new JScrollPane(texte);
        lancer = new JButton("Lancer");
        lancer.addActionListener(this);
        JPanel jpanel = new JPanel(new BorderLayout());
        jpanel.add(view, "Center");
        jpanel.add(lancer, "South");
        getContentPane().add(jpanel);
        etat = 0;
        construitModele();
        donnees = new FournisseurHMM(modele);
        reconnaissance = new Viterbi(donnees, modele);
        texte.append("Modèle construit avec succès.\n");
    }
 
    public void start() {}
 
    public void stop()  {}
 
    public void destroy() {}
 
    public void actionPerformed(ActionEvent actionevent)
    {
        int i = (int)(Math.random() * 6D) + 1;
        if(Math.random() >= 0.75D)
            etat = 1 - etat;
        texte.append("Lancement de ");
        texte.append(Integer.toString(etat + 1));
        int j;
        if(etat == 0)
        {
            texte.append(" dé");
            j = i;
        } else
        {
            texte.append(" dés");
            j = (int)(Math.random() * 6D) + 1 + i;
        }
        String s = " : " + j + "\n";
        texte.append(s);
        double ad[] = new double[2];
        switch(j)
        {
        case 1:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.0D;
            break;
 
        case 2:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.027777777777777776D;
            break;
 
        case 3:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.055555555555555552D;
            break;
 
        case 4:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.083333333333333329D;
            break;
 
        case 5:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.1111111111111111D;
            break;
 
        case 6:
            ad[0] = 0.16666666666666666D;
            ad[1] = 0.1388888888888889D;
            break;
 
        case 7:
            ad[0] = 0.0D;
            ad[1] = 0.16666666666666666D;
            break;
 
        case 8:
            ad[0] = 0.0D;
            ad[1] = 0.1388888888888889D;
            break;
 
        case 9:
            ad[0] = 0.0D;
            ad[1] = 0.1111111111111111D;
            break;
 
        case 10:
            ad[0] = 0.0D;
            ad[1] = 0.083333333333333329D;
            break;
 
        case 11:
            ad[0] = 0.0D;
            ad[1] = 0.055555555555555552D;
            break;
 
        case 12:
            ad[0] = 0.0D;
            ad[1] = 0.027777777777777776D;
            break;
        }
        donnees.ajouteObservation(new ProbasDate(ad), modele.getAction(0));
        try
        {
            double ad1[][] = reconnaissance.getAllDistributions();
            double ad2[] = ad1[ad1.length - 1];
            s = "Probas => " + (int)(ad2[0] * 100D) + "% / " + (int)(ad2[1] * 100D) + "%\n";
        }
        catch(Exception exception)
        {
            s = "Erreur imprévue : " + exception.getMessage() + "\n";
            exception.printStackTrace();
        }
        texte.append(s);
        texte.setCaretPosition(texte.getText().length());
    }
 
    public void construitModele()
    {
        String as[] = {"1D6", "2D6"};
        Distribution distribution = new Distribution(as.length);
        IAction aiaction[] = new IAction[1];
        aiaction[0] = new ActionEntiere(0, "Lancer");
        Distribution adistribution[][] = new Distribution[aiaction.length][as.length];
        double ad[] = new double[2];
        ad[0] = 0.75D;
        ad[1] = 0.25D;
        adistribution[0][0] = new Distribution(ad);
        ad = new double[2];
        ad[0] = 0.25D;
        ad[1] = 0.75D;
        adistribution[0][1] = new Distribution(ad);
        modele = new FournisseurMarkov(as, distribution, adistribution, aiaction);
    } // construitModele
 
    protected JScrollPane view;
    protected JTextArea texte;
    protected JButton lancer;
    protected int etat;
    protected Viterbi reconnaissance;
    protected FournisseurMarkov modele;
    protected FournisseurHMM donnees;
} // class Des