//
// Copyright (c) 1996 R.Arumugam - Aru@Pobox.com
//
// Permission to use, copy, hack, and distribute this software and its
// documentation for NON-COMMERCIAL purposes and without fee is hereby
// granted provided that this copyright notice appears in all copies.
//



import java.awt.*;
import java.applet.*;
import wordList;

public class HangDuke extends Applet implements Runnable {

    Panel panel1;
    Panel panel2;
    dukeArea Duke;
    Keyin keyBord;
    Graphics g2;

    Image img[] = new Image[9];
    scoreBord  score;
    int msgLen, incCtr = 0;
    static String msg = "  "+"This programme was created by R.Arumugam, Madras," +
       "Tamil Nadu - INDIA. Please send your suggestions/comments to my E-Mail Address: Aru@Pobox.com, " +
       "thank you...  ";

    public void init() {
        msgLen = msg.length();
        for (int i = 0; i < 9; i++)
            img[i] = getImage(getCodeBase(), "image"+i+".gif");
        Image im = createImage(size().width, size().height);
        Graphics gg = im.getGraphics();
        for (int i = 0; i < 9; i++)
            gg.drawImage(img[i], 0, 0, this);
        Duke = new dukeArea(this);
        setLayout(new GridLayout(1,2));
        resize(620,300);
        add(Duke);

        panel2=new Panel();
        panel2.setLayout(new GridLayout(2,1));
        keyBord = new Keyin();
        panel2.add(keyBord);
        score = new scoreBord(this, Duke);
        panel2.add(score);
        add(panel2);
        panel2.reshape(165,222,164,221);
        g2 = Duke.getGraphics();
    }

    public void start() {
            (new Thread(this)).start();
    }


    public void run() {
        while(true) {
            scrollMsg();
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {}
        }
    }


    void scrollMsg() {
            String status;
            if ((msgLen-50) - incCtr < 0)
                status = msg.substring(incCtr, (50 + incCtr) + ((msgLen-50) - incCtr));
                else
                status = msg.substring(incCtr, 50 + incCtr);

            if (incCtr  > msgLen - 1 )
                incCtr = 1;
            else
                incCtr++;

            if (incCtr + 50 > msgLen)
             status = status + msg.substring(1, incCtr - (msgLen - 50));
            showStatus(status);
    }



    public boolean imageUpdate(Image img, int flag, int x, int y,
                        int h, int w){
            if (flag == ALLBITS) {
                Duke.loading = false;
                Duke.repaint();
                return false;
            }
        return true;
    }

    public boolean action(Event evt, Object arg) {
            String st = ((Button)evt.target).getLabel();
            if (st.equals("Reset")) {
              keyBord.EnableAll();
              score.ResetAll();
              return true;
            }

            if (st.equals("Again")) {
              keyBord.EnableAll();
              score.playAgain();
              return true;
            }

            ((Button)evt.target).disable();
            score.checkChar(st);
            return true;
    }
}


class scoreBord extends Canvas {
   int Played, Won, Remain, Correct, WordLen;
   String Guess, Word;
   char chr[];
   wordList Dictionary;
   dukeArea errorScr;
   HangDuke app;

   scoreBord(HangDuke app, dukeArea duk) {
        this.app = app;
        Dictionary = new wordList(app.getCodeBase(), "Word.Dir");
        errorScr = duk;
        ResetAll();
   }

   void ResetAll() {
        Word = new String(wordList.getWord());
        WordLen = Word.length();
        Correct = 0;
        Played = 0;
        Won = 0;
        Remain = 10;
        InitArray(Word.length());
        errorScr.Reset();
        repaint();
   }


   void playAgain() {
        Word = new String(wordList.getWord());
        WordLen = Word.length();
        Correct = 0;
        Remain = 10;
        InitArray(Word.length());
        errorScr.Reset();
        repaint();
   }


   void InitArray(int Len) {
        int x;
        chr = new char[Len * 2];
        for (x = 0; x < (Len - 1) * 2; x += 2) {
            chr[x] = '_';
            chr[x + 1] = ' ';
        }
        chr[x] = '_';
        Guess = new String(chr);
   }


   void checkChar(String ch) {
       boolean found = false;

       for (int i = 0; i < WordLen; i++) {
            if (Word.charAt(i) == ch.charAt(0)) {
                chr[i * 2] = Word.charAt(i);
                Correct++;
                found = true;
            }
       }
       if (found) {
           Guess = new String(chr);
           entryOk();
       }
       else
           entryError();
   }

   void entryError() {
       errorScr.incErrorLevel();
       Remain--;
       if (Remain < 1) {
            Played++;
            errorScr.youLose(Word);
       }
       repaint();
   }

   void entryOk() {
        if (Correct > WordLen - 1) {
            errorScr.youWon();
            Played++;
            Won++;
        }
        repaint();
   }

   public void update(Graphics g) {
        paint(g);
   }

   public void paint(Graphics g) {
        int x = size().width - 1;
        int y = size().height - 1;
        g.setColor(Color.blue);
        g.drawRect(0,0, x,y);
        g.fillRect(0,0,x,y);
        g.setColor(Color.yellow);
        g.drawString("Guess Word:", x/9, (y/6));
        g.drawString(Guess, x/2 , (y/6));
        g.drawString("Games Played:",x/4, (y/7) * 3 );
        g.drawString(Integer.toString(Played) ,(x/2) + 40, (y/7) * 3 );
        g.drawString("Games Won:", x/4, (y/7) * 4 );
        g.drawString(Integer.toString(Won), (x/2) + 40, (y/7) * 4 );
        g.drawString("Games Lost:", x/4, (y/7) * 5 );
        g.drawString(Integer.toString(Played - Won),(x/2) + 40, (y/7) * 5 );
        g.drawString("Guesses Remaining:", x/4, (y/7) * 6 );
        g.drawString(Integer.toString(Remain), (x/2) + 40, (y/7) * 6 );
    }
}



class dukeArea extends Canvas {
    int errorLevel = 0;
    String Stringwas;
    boolean youWon = false, youLose = false, loading = true;
    int ix[], iy[];
    HangDuke app;
    dukeArea(HangDuke app) {
        this.setBackground(Color.white);
        this.app = app;
        ix = new int[10];
        iy = new int[10];
        ix[0] = 55;
        ix[1] = 60;
        ix[2] = 0;
        ix[3] = 130;
        ix[4] = 14;
        ix[5] = 127;
        ix[6] = 80;
        ix[7] = 80;
        ix[8] = 101;
        iy[0] = 0;
        iy[1] = 66;
        iy[2] = 66;
        iy[3] = 67;
        iy[4] = 210;
        iy[5] = 209;
        iy[6] = 86;
        iy[7] = 67;
        iy[8] = 67;
        Reset();
    }

    void Reset() {
        errorLevel = 0;
        youWon = false;
        youLose = false;
        repaint();
    }

    void incErrorLevel() {
        errorLevel++;
        repaint();
    }

    void youWon() {
        youWon = true;
        repaint();
        app.keyBord.DisableAll();
    }

    void youLose(String str) {
        Stringwas = str;
        youLose = true;
        repaint();
        app.keyBord.DisableAll();
    }



    public void paint(Graphics g){
        int x = size().width - 1;
        int y = size().height - 1;

        hangBar(g, x, y);
        if (loading) {
            Font font = new Font("TimesRoman", Font.BOLD, 40);
            g.setFont(font);
            g.setColor(Color.red);
            g.drawString("Loading..", x/4 , y/2);
            return;
        }

        for (int i = 0; (i < errorLevel) & (i < 9); i++)
            g.drawImage(app.img[i], (x/2) - (100 - ix[i]),17 + iy[i], this);

        if (errorLevel > 9) {
            g.setColor(Color.black);
            g.fillOval((x/2)- 30, 150, 60, 50);
        }

        if (youWon) {
            Font font = new Font("TimesRoman", Font.BOLD, 40);
            g.setFont(font);
            g.setColor(Color.orange);
            g.drawString("You Win !", x/4 , y/2);
            return;
        }

        if (youLose) {
            Font font = new Font("TimesRoman", Font.BOLD, 24);
            g.setFont(font);
            g.setColor(Color.red);
            g.drawString("You Lose !, the word was ", x/8 , y/2);
            g.drawString(Stringwas, x/3 , (y/5) * 3 );
            return;
        }
    }


    void hangBar(Graphics g, int x, int y) {
        g.setColor(Color.blue);
        g.drawRect(0,0,x,y);
        int px[] = new int[4];
        int py[] = new int[4];
        px[0] = 20;
        px[1] = 30;
        px[2] = 10;
        px[3] = 20;
        py[0] = 10;
        py[1] = y - 10;
        py[2] = y - 10;
        py[3] = 10;
        g.drawPolygon(px, py, 4);
        g.fillPolygon(px, py, 4);
        g.setColor(Color.red);
        px[0] = 3;
        px[1] = x/2;
        px[2] = x/2;
        px[3] = 3;
        py[0] = 13;
        py[1] = 10;
        py[2] = 16;
        py[3] = 13;
        g.drawPolygon(px, py, 4);
        g.fillPolygon(px, py, 4);
    }
}





class Keyin extends Panel {

    Keyin() {
        this.setBackground(Color.white);
        setLayout(new GridLayout(4,7,2,2));
        add(new Button("A"));
        add(new Button("B"));
        add(new Button("C"));
        add(new Button("D"));
        add(new Button("E"));
        add(new Button("F"));
        add(new Button("G"));
        add(new Button("H"));
        add(new Button("I"));
        add(new Button("J"));
        add(new Button("K"));
        add(new Button("L"));
        add(new Button("M"));
        add(new Button("N"));
        add(new Button("O"));
        add(new Button("P"));
        add(new Button("Q"));
        add(new Button("R"));
        add(new Button("S"));
        add(new Button("T"));
        add(new Button("U"));
        add(new Button("V"));
        add(new Button("W"));
        add(new Button("X"));
        add(new Button("Y"));
        add(new Button("Z"));
        add(new Button("Again"));
        add(new Button("Reset"));
        reshape(5,5,164,221);
    }

    void EnableAll() {
        int ctr = this.countComponents();
        for (int x = 0; x < ctr; x++)
            getComponent(x).enable();
    }

    void DisableAll() {
        int ctr = this.countComponents();
        for (int x = 0; x < ctr; x++) {
            if ((((Button)getComponent(x)).getLabel() == "Reset") |
                    (((Button)getComponent(x)).getLabel() == "Again"))
                continue;
            getComponent(x).disable();
        }
    }
}


