Hangman Code
Hangman Code
Hangman Code
*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Hangman extends JApplet implements ActionListener
{
private int lives = 7;
private String gameWord;
private final String ButtonText = "A";
private final String ButtonText2 = "B";
private final String ButtonText3 = "C";
private final String ButtonText4 = "D";
private final String ButtonText5 = "E";
private final String ButtonText6 = "F";
private final String ButtonText7 = "G";
private final String ButtonText8 = "H";
private final String ButtonText9 = "I";
private final String ButtonText10 = "J";
private final String ButtonText11 = "K";
private final String ButtonText12 = "L";
private final String ButtonText13 = "M";
private final String ButtonText14 = "N";
private final String ButtonText15 = "O";
private final String ButtonText16 = "P";
private final String ButtonText17 = "Q";
private final String ButtonText18 = "R";
private final String ButtonText19 = "S";
private final String ButtonText20 = "T";
private final String ButtonText21 = "U";
private final String ButtonText22 = "V";
private final String ButtonText23 = "W";
private final String ButtonText24 = "X";
private final String ButtonText25 = "Y";
private final String ButtonText26 = "Z";
private final String ButtonText27 = "Restart";
private final String ButtonText28 = "YOUR GUESS";
JButton[] myButton = new JButton[28];
ArrayList<String> list = new ArrayList<String>();
JLabel[] msgs = new JLabel[18];
public void init()
{
Graphics g = this.getGraphics();
msgs[0] = new JLabel("");
msgs[1] = new JLabel("");
check("G");
myButton[6].setEnabled(false);}
if(ButtonText8.equals(command)){
check("H");
myButton[7].setEnabled(false);}
if(ButtonText9.equals(command)){
check("I");
myButton[8].setEnabled(false);}
if(ButtonText10.equals(command)){
check("J");
myButton[9].setEnabled(false);}
if(ButtonText11.equals(command)){
check("K");
myButton[10].setEnabled(false);}
if(ButtonText12.equals(command)){
check("L");
myButton[11].setEnabled(false);}
if(ButtonText13.equals(command)){
check("M");
myButton[12].setEnabled(false);}
if(ButtonText14.equals(command)){
check("N");
myButton[13].setEnabled(false);}
if(ButtonText15.equals(command)){
check("O");
myButton[14].setEnabled(false);}
if(ButtonText16.equals(command)){
check("P");
myButton[15].setEnabled(false);}
if(ButtonText17.equals(command)){
check("Q");
myButton[16].setEnabled(false);}
if(ButtonText18.equals(command)){
check("R");
myButton[17].setEnabled(false);}
if(ButtonText19.equals(command)){
check("S");
myButton[18].setEnabled(false);}
if(ButtonText20.equals(command)){
check("T");
myButton[19].setEnabled(false);}
if(ButtonText21.equals(command)){
check("U");
myButton[20].setEnabled(false);}
if(ButtonText22.equals(command)){
check("V");
myButton[21].setEnabled(false);}
if(ButtonText23.equals(command)){
check("W");
myButton[22].setEnabled(false);}
if(ButtonText24.equals(command)){
check("X");
myButton[23].setEnabled(false);}
if(ButtonText25.equals(command)){
check("Y");
myButton[24].setEnabled(false);}
if(ButtonText26.equals(command)){
check("Z");
myButton[25].setEnabled(false);}
if(ButtonText27.equals(command))
restart();
if(ButtonText28.equals(command))
seeIfWord();
paint(g);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.black);
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(30,50,30,300);
g2.drawLine(30,50,150,50);
g2.drawLine(150,50,150,90);
g2.drawLine(10,300,200,300);
if(gameFinished()){
msgs[15].setText("YOU WIN CONGRATULATIONS");
msgs[16].setText("WOULD YOU LIKE TO PLAY AGAIN");
msgs[17].setText("IF SO PRESS RESTART");
disableall();
}
if (lives <=6 )
g.drawOval(135,90,30,30);
if (lives <=5 )
g.drawLine(150,120,150,150);
if (lives <=4 )
g.drawLine(150,150,165,183);
if (lives <=3 )
g.drawLine(150,150,137,183);
if (lives <=2 )
g.drawLine(150,125,176,135);
if (lives <= 1)
g.drawLine(150,125,126,135);
if(lives == 0){
msgs[15].setText("YOU LOSE "+"YOUR WORD WAS "
+gameWord.toUpperCase());
msgs[16].setText("WOULD YOU LIKE TO PLAY AGAIN");
msgs[17].setText("IF SO PRESS RESTART");
disableall();
}
if(gameWord.length() >= 3){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
}
if(gameWord.length() >= 4){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
}
if(gameWord.length() >= 5){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
}
if(gameWord.length() >= 6){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
}
if(gameWord.length() >= 7){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
}
if(gameWord.length() >= 8){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
}
if(gameWord.length() >= 9){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
}
if(gameWord.length() >= 10){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
}
if(gameWord.length() >= 11){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
g.drawLine(690,400,740,400);
}
if(gameWord.length() >= 12){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
g.drawLine(690,400,740,400);
g.drawLine(750,400,800,400);
}
if(gameWord.length() >= 13){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
g.drawLine(690,400,740,400);
g.drawLine(750,400,800,400);
g.drawLine(810,400,860,400);
}
if(gameWord.length() >= 14){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
g.drawLine(690,400,740,400);
g.drawLine(750,400,800,400);
g.drawLine(810,400,860,400);
g.drawLine(870,400,920,400);
}
if(gameWord.length() >= 15){
g.drawLine(90,400,140,400);
g.drawLine(150,400,200,400);
g.drawLine(210,400,260,400);
g.drawLine(270,400,320,400);
g.drawLine(330,400,380,400);
g.drawLine(390,400,440,400);
g.drawLine(450,400,500,400);
g.drawLine(510,400,560,400);
g.drawLine(570,400,620,400);
g.drawLine(630,400,680,400);
g.drawLine(690,400,740,400);
g.drawLine(750,400,800,400);
g.drawLine(810,400,860,400);
g.drawLine(870,400,920,400);
g.drawLine(930,400,980,400);
}
}
public void check(String letter){
ArrayList<String> letters1 = new ArrayList<String>();
int counter=0;
while(counter <= gameWord.length()-1){
letters1.add(gameWord.substring(counter,counter+1).toUpperCase());
counter++;
}
if(letters1.contains(letter)){
display(letter);
}else{
lives--;
}
}
public String setWord(){
ArrayList<String> words = new ArrayList<String>();
ArrayList<String> letters2 = new ArrayList<String>();
int randomint = 0;
int counter = 0;
try{
Scanner load = new Scanner(new File("Dic.txt"));
String getWords;
while(load.hasNext()){
getWords = load.nextLine();
words.add(getWords);
}
load.close();
}
catch(IOException e){
System.out.println("file error");
}
randomint = (int)(Math.random()* words.size())+1;
if (words.size() != 0){
String word = words.get(randomint);
// msgs[15].setText(word);
while(counter <= word.length()-1){
letters2.add(word.substring(counter,counter+1));
counter++;
}
if(letters2.contains(",")|| letters2.contains("-")|| letters2.size()>15||
letters2.contains(".")|| letters2.contains(" ")||letters2.size()<=3){
return setWord();
}else{
return word;
}
}else{
return words.get(0);
}
}
public void disableall(){
myButton[27].setEnabled(false);
for(int i = 0; i< 26; i++)
myButton[i].setEnabled(false);
}
public void display(String letter3){
int index = 0;
int counter1 = 0;
ArrayList<String> letters4 = new ArrayList<String>();
int counter2=0;
while(counter2 <= gameWord.length()-1){
letters4.add(gameWord.substring(counter2,counter2+1).toUpperCase());
counter2++;
}
index = letters4.indexOf(letter3);
msgs[index].setText(letter3);
letters4.remove(index);
index = letters4.indexOf(letter3);
if(index != -1)
msgs[index+1].setText(letter3);
}
public boolean gameFinished(){