import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.lang.*;

public class hmapplet extends Applet {

  StringBuffer wordinprogress;
  String headermsg, msg;
  int i, n, badtries;
  String guessword, words[];
  boolean done, match;
  char ch;
  Font wordfont, defaultfont;
  Color defaultcolor;


  public void init() {

    wordfont = new Font("Courier",Font.BOLD,18);
    defaultfont = getFont();

    i = badtries = 0;
    headermsg = "Welcome to Hangman!  Guest a letter...";
    msg = "";
    
    words = new String[13];
    words[0]="encyclopedia";
    words[1]="banana";
    words[2]="jungle";
    words[3]="programming";
    words[4]="objectivity";
    words[5]="directionally";
    words[6]="velocity";
    words[7]="bookshelf";
    words[8]="harmonious";
    words[9]="blinker";
    words[10]="tazmania";
    words[11]="madness";
    words[12]="mahogany";

    n = (int) (java.lang.Math.random() * words.length);
    guessword = words[n];

    wordinprogress = new StringBuffer(guessword);
    for (i=0; i < wordinprogress.length(); i++) {
      wordinprogress.setCharAt(i,'_');
    }

    done = match = false;

    repaint();
  }


  public boolean keyDown(Event e, int c) {
    if (done)
      return false;

    ch = (char)c;

    match = false;
    for (i=0; i < wordinprogress.length(); i++) {
      if(guessword.charAt(i) == ch) {
        wordinprogress.setCharAt(i,ch);
        match = true;
      }
    }

    if (!match) {
      badtries++;
      msg = "nope, no '" + ch + "' in this word";
    } else {
      msg = "not bad..please continue.";
    }

    done = true;
    for (i=0; i < wordinprogress.length(); i++) {
      if(wordinprogress.charAt(i) == '_')
        done = false;
    }

    if (done) {
       msg="Congrats!  You've guessed it.";
    }

    if (badtries >= 7) {
      msg = "7 bad tries, you're out!";
      done = true;
    }

    repaint();
    return true;
  }



  public void paint(Graphics g) {
    defaultcolor = g.getColor();

    g.drawString(headermsg, 5, 15);

    g.setColor(Color.blue);
    g.setFont(wordfont);

    for(i=0; i<wordinprogress.length(); i++) {
      g.drawString(wordinprogress.charAt(i)+"",25+20*i,40);
    }
//    g.drawString(wordinprogress.toString(),25,40);

    g.setFont(defaultfont);
    g.setColor(defaultcolor);
    g.drawString(msg,5,65);
    g.drawString("Number of Bad Attempts: "+badtries,5,90);

    g.drawLine(50,250,100,250);
    g.drawLine(75,120,75,250);
    g.drawLine(75,120,200,120);

    if(badtries>0) {
      g.drawArc(155,140,40,40,0,360);
      if(badtries>1) {
        g.drawLine(175,180,175,220);
        if(badtries>2) {
          g.drawLine(175,190,200,215);
          if(badtries>3) {
            g.drawLine(175,190,150,215);
            if(badtries>4) {
              g.drawLine(175,220,200,240);
              if(badtries>5) {
                g.drawLine(175,220,150,240);
                if(badtries>6) {
                  g.drawLine(175,120,175,140);
                  g.drawString("x",160,160);
                  g.drawString("x",180,160);
                  g.drawLine(165,170,185,170);
                }
              }
            }
          }
        }
      }
    }

  }

}

