Java Certification, Programming, JavaBean and Object Oriented Reference Books
Program to create a classic hangman game

programtocreateaclassichangmangame.java

// Program to create a classic hangman game

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;

public class HangMan10 extends Applet implements ActionListener,ItemListener
{
 Button button[];
 TextField word,letter;
 TextField guess;
 Label label1,label2,label3;
 String alphabuttons[];
 Panel panel1,panel2,panel3,pana,panb,panc;
 Font font;
 CheckboxGroup group;
 Checkbox beg,inte,adv;
 String begin[];
 String inter[];
 String advan[];
 int lives,select;
 String answer = "        ";
 String missy;
 String buffy = "--------";
 StringBuffer store2= new StringBuffer(buffy);
 int counta = lives;
 public void init()
 {
  this.setLayout(new BorderLayout());
  // sets up the array to produce the alphabet buttons on the grid
  alphabuttons = new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
 
  // sets up the arrays with the words to be guessed in all three stages
  begin = new String[]{"CAT","WREN","MOUSE","GERBIL"};
  inter = new String[]{"ROSE","GRASS","WILLOW","RAGWORT"};
  advan = new String[]{"SHIRT","JACKET","SANDALS","TROUSERS"};
 
  // create and arrange panel 1 which will house the text fields and the labels
 
  panel1 = new Panel(); // panel 1 will hold the two labels and text fields at the top of the applet
  add("North",panel1);
 
  panel1.setLayout(new BorderLayout());
  label1 = new Label("GUESS THE WORD"); // create the label
  font = new Font("TimesRoman",Font.ITALIC,16);
  label1.setFont(font); // set the font to the label
 
  pana = new Panel();
  panel1.add("North",pana);
  pana.add(label1); // add the label to the panel
  label1.setAlignment(label1.CENTER); // set the label to the centre
 
  panb = new Panel();
  panel1.add("Center",panb);
  word = new TextField(24); // creates the text field for the word set to 24 chars
  word.addActionListener(this);
  word.setEditable(true); // allows text to be edited
  panb.add(word,"CENTER"); // adds the text field to the panel
  label3 = new Label("Win/Loose");
  label3.setFont(font);
  panb.add(label3);
  label1.setAlignment(label1.RIGHT);
  letter = new TextField(10);
  letter.addActionListener(this);
  panb.add(letter,"RIGHT");
 
  panc = new Panel();
  panel1.add("South",panc);
 
  label2 = new Label("GUESSES REMAINING"); // create the label and intiliase
  label2.setFont(font); // sets the font previously defined for label 1
  panc.add(label2);
  label2.setAlignment(label2.LEFT); // Sets the label left
 
  guess = new TextField(3); // initialises the guess text field to 3 chars
  guess.addActionListener(this);
  panc.add(guess);
 
  // create and arrange panel 2 which will house the alphabet grid
 
  panel2 = new Panel();
  add("Center",panel2);
 
  // set gridLayout for panel2(int rows , int col, int hgap, int vgap
  panel2.setLayout(new GridLayout(4,7,3,3));
 
  button = new Button[28];
 
  for (int i=0;i<26;i+=1)
  {
   // set the buttons of the alphabet up using the array
   button[i] = new Button(alphabuttons[i]);
   panel2.add(button[i]);
   button[i].addActionListener(this);
  }
 
  button[26] = new Button("Answer"); // creates the answer button
  button[26].setForeground(Color.green);
  button[26].addActionListener(this);
 
  button[27] = new Button("Reset"); // creates the reset button
  button[27].setForeground(Color.yellow);
  button[27].addActionListener(this);
 
  // add the remaining buttons to panel 2
  panel2.add(button[26]);
  panel2.add(button[27]);
 
  // create and arrange panel 3 which will house the radio buttons for the level
 
  panel3 = new Panel();
  add("South",panel3);
 
  group = new CheckboxGroup();
 
  // create the radio buttons
 
  beg = new Checkbox("Beginner",group,false);
  beg.addItemListener(this);
 
  inte = new Checkbox("Intermediate",group,false);
  inte.addItemListener(this);
 
  adv = new Checkbox("Advanced",group,false);
  adv.addItemListener(this);
 
  // add the radio buttons to panel3
  panel3.add(beg);
  panel3.add(inte);
  panel3.add(adv);
 }
 
 
 public void actionPerformed(ActionEvent e)
 {
  int j=0;
  int count=0;
  int k=0;
  String currentLetter;
  String temp="";
  StringBuffer Sanswer = new StringBuffer(answer);
  Sanswer.setLength(answer.length());
  boolean hang = false;
  boolean correct=true;
  store2.setLength(answer.length());
  count = Sanswer.length();
 
 
  for(k=0;k<=25;k+=1)
  {
   if (e.getSource()==button[k])
   {
    currentLetter = (button[k].getLabel());
    letter.setText(currentLetter);
 
    for (j=0;j<count;j++)
    {
     //cycles through the letters in the word
     if (currentLetter.equals(String.valueOf(answer.charAt(j))))
     {
      // replaces the letter if it is within the word
      store2.replace(j,j+1,currentLetter);
      temp=store2.toString();
      lives+=1; // does not decrement the lives
      if(temp.equals(word.getText()))
      {
       letter.setText("YOU WIN");
       letter.setForeground(Color.blue);
      }
     }
 
    }
    lives-=1; // decrements the lives
    guess.setText(Integer.toString(lives));
    word.setText(store2.toString());
 
    if (lives<1)
    {
     letter.setText("YOU LOSE");
     letter.setForeground(Color.red);
    }
 
   }
  }
 
  if(e.getSource()==button[26]) // answer button
  {
 
   word.setText(answer);
   word.setForeground(Color.darkGray);
   letter.setText("Unlucky");
  }
 
  if(e.getSource()==button[27]);
  {
  /* word.setText(null);
   guess.setText(null);
   letter.setText("delete");
   for(int x=0;x<9;x++)
   {
    store2.replace(x,x+1,"-");
   }
   count = Sanswer.length();
   count=answer.length(); */
  }
 } // ends actionPerformed
 
 // invoked with the radio buttons are selected or deselected
 public void itemStateChanged(ItemEvent e)
 {
  // checks if the button is pressed
  select = (int)(java.lang.Math.random()*4); // random number generator for the arrays
  StringBuffer miss = new StringBuffer(answer);
  if (e.getSource()==beg)
  {
   if(beg.getState()==true)
   {
    lives = 9;
    answer = (begin[select]);
 
    for (int y=0;y<answer.length();y++)
    {
     miss.replace(y,y+1,"-");
    }
    word.setText(miss.toString());
    missy = miss.toString();
    word.setForeground(Color.black);
    guess.setText(Integer.toString(lives));
   }
  }
  else if (e.getSource()==inte)
  {
   if(inte.getState()==true)
   {
    lives = 8;
    answer = (inter[select]);
    for (int y=0;y<answer.length();y++)
    {
     miss.replace(y,y+1,"-");
    }
    word.setText(miss.toString());
    missy = miss.toString();
    word.setForeground(Color.black);
    guess.setText(Integer.toString(lives));
   }
  }
  else if(e.getSource()==adv)
  {
   if(adv.getState()==true)
   {
    lives = 7;
    answer = (advan[select]);
     for (int y=0;y<answer.length();y++)
    {
     miss.replace(y,y+1,"-");
    }
    word.setText(miss.toString());
    missy = miss.toString();
    word.setForeground(Color.black);
    guess.setText(Integer.toString(lives));
   }
  }
 } // ends itemStateChanged for the radio buttons

}//ends the class    

Return to : Java Programming Hints and Tips