import java.applet.Applet;
import java.awt.*;

public class volpe extends Applet
{

    public void init()
    {
        if(gioco == 2)
        {
            for(h = 1; h < 34; h++)
                ce[h] = ce3[h];

            difficolta = 7;
        }
        if(gioco == 1)
        {
            for(h = 1; h < 34; h++)
                ce[h] = ce1[h];

            difficolta = 15;
        }
        if(gioco == 0)
        {
            for(h = 1; h < 34; h++)
                ce[h] = ce3[h];

            difficolta = 15;
        }
        scacchiera();
        setLayout(null);
        setBackground(Color.white);
        add(are);
        are.reshape(310, 40, 275, 260);
        add(box1);
        add(box2);
        add(box3);
        box1.reshape(225, 15, 70, 20);
        box2.reshape(225, 35, 70, 20);
        box3.reshape(225, 55, 70, 20);
        add(ric);
        ric.reshape(225, 250, 70, 25);
        for(h2 = 1; h2 < 34; h2++)
            for(h1 = 0; h1 < 25; h1++)
                for(h = 1; h < 34; h++)
                {
                    ceno[h2][h1] = 34;
                    e = (h1 - (h1 / 5) * 5) * 2 - 4;
                    r = (h1 / 5) * 2 - 4;
                    if(a[h2] + e != a[h] || b[h2] + r != b[h])
                        continue;
                    ceno[h2][h1] = h;
                    break;
                }



    }

    public boolean mouseDown(Event event, int i, int j)
    {
        x1 = i;
        y1 = j;
        riquadro();
        return false;
    }

    public void paint(Graphics g)
    {
        scacchiera();
    }

    public void riquadro()
    {
        Graphics g = getGraphics();
        for(h = 1; h < 34; h++)
            if(x1 >= (b[h] / 2 - 1) * 38 + 28 && x1 <= (b[h] / 2 - 1) * 38 + 60 && y1 >= (a[h] / 2 - 1) * 38 + 10 && y1 <= (a[h] / 2 - 1) * 38 + 41)
                cella = h;

        libero = false;
        for(h = 1; h < 18; h++)
            if(ce[ceno[cella][h]] == 3 && (h == 7 || h == 11 || h == 17 || h == 13))
                libero = true;

        if(ce[cella] == 1 && libero)
            if(!primo && cella2 == cella)
            {
                primo = true;
                g.setColor(Color.white);
                g.fillRect((b[cella2] / 2 - 1) * 38 + 28, (a[cella2] / 2 - 1) * 38 + 11, 3, 33);
                g.fillRect((b[cella2] / 2 - 1) * 38 + 28, (a[cella2] / 2 - 1) * 38 + 11, 33, 3);
                g.fillRect((b[cella2] / 2 - 1) * 38 + 59, (a[cella2] / 2 - 1) * 38 + 11, 3, 34);
                g.fillRect((b[cella2] / 2 - 1) * 38 + 28, (a[cella2] / 2 - 1) * 38 + 42, 33, 3);
            } else
            if(primo)
            {
                primo = false;
                g.setColor(Color.green);
                g.fillRect((b[cella] / 2 - 1) * 38 + 28, (a[cella] / 2 - 1) * 38 + 11, 3, 33);
                g.fillRect((b[cella] / 2 - 1) * 38 + 28, (a[cella] / 2 - 1) * 38 + 11, 33, 3);
                g.fillRect((b[cella] / 2 - 1) * 38 + 59, (a[cella] / 2 - 1) * 38 + 11, 3, 34);
                g.fillRect((b[cella] / 2 - 1) * 38 + 28, (a[cella] / 2 - 1) * 38 + 42, 33, 3);
                cella2 = cella;
            }
        libero = false;
        for(h = 1; h < 18; h++)
            if(ce[cella] == 3 && (h == 7 || h == 11 || h == 17 || h == 13) && ceno[cella2][h] == cella && ce[cella2] == 1 && !primo)
                libero = true;

        if(libero)
        {
            ce[cella] = 1;
            ce[cella2] = 3;
            scacchiera();
            primo = true;
            for(h = 1; h < 34; h++)
            {
                punti[h] = 0;
                if(ce[h] == 2)
                    cellav = h;
            }

            mossa = false;
            for(h2 = 1; h2 < 34; h2++)
            {
                for(h = 0; h < difficolta; h += 2)
                {
                    if(ce[ceno[h2][mangia[h]]] != 2 || ce[ceno[h2][mangia[h + 1]]] != 1 || ce[h2] != 3)
                        continue;
                    ce[ceno[h2][12]] = 2;
                    ce[ceno[h2][mangia[h]]] = 3;
                    ce[ceno[h2][mangia[h + 1]]] = 3;
                    mossa = true;
                    break;
                }

                if(mossa)
                    break;
                if(ce[h2] == 3)
                {
                    for(h1 = 0; h1 < 8; h1++)
                        if(ce[ceno[h2][direzio[h1]]] == 2)
                            for(h = 0; h < 16; h += 2)
                                if(ce[ceno[h2][mosse2[h]]] == 1 && ce[ceno[h2][mosse2[h + 1]]] == 3)
                                    punti[h2] += 7;


                    for(h1 = 8; h1 < 16; h1++)
                        if(ce[ceno[h2][direzio[h1]]] == 2)
                            for(h = 0; h < 16; h += 2)
                                if(ce[ceno[h2][mosse2[h]]] == 1 && ce[ceno[h2][mosse2[h + 1]]] == 3 && ce[ceno[h2][direzio[h1 - 8]]] == 3)
                                    punti[ceno[h2][direzio[h1 - 8]]] += 3;


                    for(h1 = 16; h1 < 24; h1++)
                        if(ce[ceno[h2][direzio[h1]]] == 2)
                            for(h = 0; h < 16; h += 2)
                                if(ce[ceno[h2][mosse2[h]]] == 1 && ce[ceno[h2][mosse2[h + 1]]] == 3 && ce[ceno[h2][direzio[h1 - 16]]] == 3)
                                    punti[ceno[h2][direzio[h1 - 16]]] += 2;


                    for(h1 = 16; h1 < 23; h1++)
                        if(ce[ceno[h2][direzio[h1]]] == 2)
                            for(h = 0; h < 16; h += 2)
                                if(ce[ceno[h2][mosse2[h]]] == 1 && ce[ceno[h2][mosse2[h + 1]]] == 3 && ce[ceno[h2][direzio[h1 - 15]]] == 3)
                                    punti[ceno[h2][direzio[h1 - 15]]] += 2;


                    for(h1 = 16; h1 < 24; h1++)
                        if(ce[ceno[h2][direzio[h1]]] == 2)
                            for(h = 16; h < 32; h += 2)
                                if(ce[ceno[h2][mosse2[h]]] == 1 && ce[ceno[h2][mosse2[h + 1]]] == 3 && ce[ceno[h2][direzio[h1 - 16]]] == 3)
                                    punti[ceno[h2][direzio[h1 - 16]]]++;


                }
            }

            if(!mossa)
            {
                pun = 0;
                direz = 0;
                for(h = 1; h < 34; h++)
                    if(punti[h] > pun)
                    {
                        direz = h;
                        pun = punti[h];
                    }

                if(ce[direz] == 3)
                {
                    ce[cellav] = 3;
                    ce[direz] = 2;
                } else
                {
                    ok = false;
                    conta = 0;
                    do
                    {
                        conta++;
                        if(conta > 100)
                        {
                            g.drawString("Hai vinto", 35, 25);
                            break;
                        }
                        arnd3 = (int)(Math.random() * 8D);
                        for(h = 0; h < 8; h++)
                            if(ce[ceno[cellav][direzio[h]]] == 3 && arnd3 == h)
                            {
                                ce[cellav] = 3;
                                ce[ceno[cellav][direzio[h]]] = 2;
                                ok = true;
                            }

                    } while(!ok);
                }
            }
            scacchiera();
            oche = 0;
            for(h = 1; h < 34; h++)
                if(ce[h] == 1)
                    oche++;

            if(oche < 6)
                g.drawString("Hai perso", 35, 25);
        }
    }

    public boolean action(Event event, Object obj)
    {
        if((event.target instanceof Button) && "Ricomincia".equals(obj))
            init();
        if(event.target instanceof Checkbox)
        {
            if(box1.getState())
                gioco = 2;
            if(box2.getState())
                gioco = 1;
            if(box3.getState())
                gioco = 0;
            init();
        }
        return true;
    }

    public void scacchiera()
    {
        Graphics g = getGraphics();
        g.setColor(Color.black);
        g.setFont(new Font("Helvetica", 1, 18));
        g.drawString("Hala-tafl", 400, 30);
        g.setFont(new Font("Helvetica", 2, 11));
        g.drawString("gioco realizzato da Antonino Iacono (aiacono@tiscalinet.it)", 10, 300);
        g.setColor(Color.blue);
        g.fillRect(24, 83, 4, 114);
        g.fillRect(62, 83, 4, 114);
        g.fillRect(100, 7, 4, 266);
        g.fillRect(138, 7, 4, 266);
        g.fillRect(176, 7, 4, 266);
        g.fillRect(214, 7, 4, 266);
        g.fillRect(252, 83, 4, 114);
        g.fillRect(290, 83, 4, 114);
        g.fillRect(100, 7, 114, 4);
        g.fillRect(100, 45, 114, 4);
        g.fillRect(24, 83, 266, 4);
        g.fillRect(24, 121, 266, 4);
        g.fillRect(24, 159, 266, 4);
        g.fillRect(24, 197, 270, 4);
        g.fillRect(100, 235, 114, 4);
        g.fillRect(100, 273, 118, 4);
        for(h = 1; h < 34; h++)
        {
            if(ce[h] == 1)
            {
                g.setColor(Color.white);
                g.fillRect((b[h] / 2 - 1) * 38 + 28, (a[h] / 2 - 1) * 38 + 11, 34, 34);
                g.setColor(Color.black);
                g.drawRect((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 27, 23, 6);
                g.setColor(Color.cyan);
                g.fillOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 30, 23, 8);
                g.setColor(Color.black);
                g.drawOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 30, 23, 8);
                g.setColor(Color.cyan);
                g.fillRect((b[h] / 2 - 1) * 38 + 34, (a[h] / 2 - 1) * 38 + 27, 22, 6);
                g.fillOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 22, 23, 8);
                g.setColor(Color.black);
                g.drawOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 22, 23, 8);
            }
            if(ce[h] == 2)
            {
                g.setColor(Color.black);
                g.drawRect((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 24, 23, 10);
                g.setColor(Color.red);
                g.fillOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 30, 23, 8);
                g.setColor(Color.black);
                g.drawOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 30, 23, 8);
                g.setColor(Color.red);
                g.fillRect((b[h] / 2 - 1) * 38 + 34, (a[h] / 2 - 1) * 38 + 24, 22, 10);
                g.fillOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 18, 23, 8);
                g.setColor(Color.black);
                g.drawOval((b[h] / 2 - 1) * 38 + 33, (a[h] / 2 - 1) * 38 + 18, 23, 8);
            }
            if(ce[h] == 3)
            {
                g.setColor(Color.white);
                g.fillRect((b[h] / 2 - 1) * 38 + 28, (a[h] / 2 - 1) * 38 + 11, 34, 34);
            }
        }

    }

    public volpe()
    {
        primo = true;
        ok = true;
        libero = false;
        mossa = false;
        testo = "La Saga di Grettis, scritta probabilmente\nda un monaco islandese nell'anno 1300\ncirca, fa riferimento a un gioco chiamato\nHala-tafl  che,  da quanto se ne capisce,\ncorrisponde a un tipo di gioco diffuso poi\nnel resto d'Europa col nome di la Volpe e\nle Oche.\nI giocatori sono due:  il primo gioca con\nuna pedina  (la Volpe), l'altro gioca con\ntredici  pedine  (le Oche);  muovono  per\nprime le Oche con una pedina che andra'\na occupare  una  qualsiasi  delle  caselle \nadiacenti, purche' libera.\nAd  ogni  mossa seguente, le pedine delle\nOche possono avanzare orizzontalmente o\nverticalmente, ma non in diagonale.\nLa Volpe quando e' il suo turno, muove di\nuna  casella  per volta,  ma in qualsiasi\ndirezione.\nLa  Volpe  cattura  le  Oche  saltando la\npedina  e  andando  a occupare la casella\nvacante che sta dietro quest'ultima.\nLe Oche catturate vengono tolte dal gioco.\nLe Oche non possono catturare  la  Volpe,\nma devono cercare di immobilizzarla impe-\ndendole qualsiasi mossa.\nIn questo caso hanno vinto.\nLa Volpe vince se riesce a catturare tante\npedine  nemiche  da  rendere  l'avversario\ninoffensivo,  incapace  cioe'  di bloccare\nle sue mosse.\n\nBuon divertimento da Antonino Iacono ";
        gruppo = new CheckboxGroup();
        box1 = new Checkbox("Facile", gruppo, false);
        box2 = new Checkbox("Medio", gruppo, false);
        box3 = new Checkbox("Difficile", gruppo, true);
        are = new TextArea(testo, 40, 40);
        ric = new Button("Ricomincia");
        ceno = new int[35][25];
        punti = new int[35];
        ce = new int[35];
    }

    boolean primo;
    boolean ok;
    boolean libero;
    boolean mossa;
    String testo;
    CheckboxGroup gruppo;
    Checkbox box1;
    Checkbox box2;
    Checkbox box3;
    TextArea are;
    Button ric;
    int arnd3;
    int pun;
    int direz;
    int x1;
    int y1;
    int h;
    int h1;
    int h2;
    int e;
    int r;
    int cella;
    int cella2;
    int oche;
    int cellav;
    int conta;
    int gioco;
    int difficolta;
    int a[] = {
        0, 2, 2, 2, 4, 4, 4, 6, 6, 6, 
        6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 
        8, 10, 10, 10, 10, 10, 10, 10, 12, 12, 
        12, 14, 14, 14
    };
    int b[] = {
        0, 6, 8, 10, 6, 8, 10, 2, 4, 6, 
        8, 10, 12, 14, 2, 4, 6, 8, 10, 12, 
        14, 2, 4, 6, 8, 10, 12, 14, 6, 8, 
        10, 6, 8, 10
    };
    int ce2[] = {
        0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
        2, 3, 3, 3, 1, 3, 3, 3, 3, 3, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 0
    };
    int ce3[] = {
        0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
        2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
        3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 0
    };
    int ce1[] = {
        0, 3, 3, 3, 3, 3, 3, 1, 3, 3, 
        2, 3, 3, 1, 1, 3, 3, 3, 3, 3, 
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
        1, 1, 1, 1, 0
    };
    int mangia[] = {
        2, 7, 10, 11, 22, 17, 14, 13, 0, 6, 
        20, 16, 24, 18, 4, 8
    };
    int direzio[] = {
        16, 17, 18, 13, 8, 7, 6, 11, 20, 22, 
        24, 14, 4, 2, 0, 10, 21, 23, 19, 9, 
        3, 1, 5, 15
    };
    int mosse2[] = {
        18, 24, 13, 14, 8, 4, 7, 2, 6, 0, 
        11, 10, 17, 22, 16, 20, 17, 16, 16, 11, 
        11, 6, 6, 7, 7, 8, 8, 13, 13, 18, 
        18, 17
    };
    int ceno[][];
    int punti[];
    int ce[];
}
