0% found this document useful (0 votes)
100 views

Hangman Code

This Java code defines a Hangman game applet. It initializes variables like the number of lives, the secret word, and buttons for letters of the alphabet. It contains methods for checking letter guesses, restarting the game, and drawing the hangman graphic and letters of the secret word based on the number of guesses remaining.

Uploaded by

Derek Liu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Hangman Code

This Java code defines a Hangman game applet. It initializes variables like the number of lives, the secret word, and buttons for letters of the alphabet. It contains methods for checking letter guesses, restarting the game, and drawing the hangman graphic and letters of the secret word based on the number of guesses remaining.

Uploaded by

Derek Liu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

import java.awt.

*;
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("");

msgs[2] = new JLabel("");


msgs[3] = new JLabel("");
msgs[4] = new JLabel("");
msgs[5] = new JLabel("");
msgs[6] = new JLabel("");
msgs[7] = new JLabel("");
msgs[8] = new JLabel("");
msgs[9] = new JLabel("");
msgs[10] = new JLabel("");
msgs[11] = new JLabel("");
msgs[12] = new JLabel("");
msgs[13] = new JLabel("");
msgs[14] = new JLabel("");
msgs[15] = new JLabel("");
msgs[16] = new JLabel("");
msgs[17] = new JLabel("");
msgs[0].setBounds(110,365,30,30);
msgs[15].setBounds(300,200,400,100);
msgs[16].setBounds(300,220,400,100);
msgs[17].setBounds(300,240,400,100);
this.add(msgs[0]);
this.add(msgs[15]);
this.add(msgs[16]);
this.add(msgs[17]);
for(int i = 1; i< 15; i++){
this.add(msgs[i]);
msgs[i].setText(" ");
msgs[i].setBounds(110+i*60,365,30,30);
}
JPanel buttonPanel = new JPanel();
myButton[0] = new JButton("A");
myButton[1] = new JButton("B");
myButton[2] = new JButton("C");
myButton[3] = new JButton("D");
myButton[4] = new JButton("E");
myButton[5] = new JButton("F");
myButton[6] = new JButton("G");
myButton[7] = new JButton("H");
myButton[8] = new JButton("I");
myButton[9] = new JButton("J");
myButton[10] = new JButton("K");
myButton[11] = new JButton("L");
myButton[12] = new JButton("M");
myButton[13] = new JButton("N");

myButton[14] = new JButton("O");


myButton[15] = new JButton("P");
myButton[16] = new JButton("Q");
myButton[17] = new JButton("R");
myButton[18] = new JButton("S");
myButton[19] = new JButton("T");
myButton[20] = new JButton("U");
myButton[21] = new JButton("V");
myButton[22] = new JButton("W");
myButton[23] = new JButton("X");
myButton[24] = new JButton("Y");
myButton[25] = new JButton("Z");
myButton[26] = new JButton("Restart");
myButton[27] = new JButton("YOUR GUESS");
for(int i = 0; i< 28; i++){
myButton[i].addActionListener(this);
buttonPanel.add(myButton[i]);
}
this.add(buttonPanel);
gameWord = setWord();
paint(g);
}
public void actionPerformed(ActionEvent evt)
{
Graphics g = getContentPane().getGraphics();
String command = evt.getActionCommand();
if(ButtonText.equals(command)){
check("A");
myButton[0].setEnabled(false);}
if(ButtonText2.equals(command)){
check("B");
myButton[1].setEnabled(false);}
if(ButtonText3.equals(command)){
check("C");
myButton[2].setEnabled(false);}
if(ButtonText4.equals(command)){
check("D");
myButton[3].setEnabled(false);}
if(ButtonText5.equals(command)){
check("E");
myButton[4].setEnabled(false);}
if(ButtonText6.equals(command)){
check("F");
myButton[5].setEnabled(false);}
if(ButtonText7.equals(command)){

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(){

for(int i =0; i<=gameWord.length()-1;i++){


if(msgs[i].getText().equals(" ")||msgs[i].getText().equals(""))
return false;
}
return true;
}
public void enableall(){
myButton[27].setEnabled(true);
for(int i = 0; i< 26; i++)
myButton[i].setEnabled(true);
}
public void restart(){
lives= 7;
gameWord= setWord();
for(int i = 0; i< 18; i++){
msgs[i].setText(" ");
}
enableall();
}
public void seeIfWord(){
String s = JOptionPane.showInputDialog(null,"What is your guess?","Take a
Guess",JOptionPane.QUESTION_MESSAGE);
if(s.equals(gameWord)){
msgs[15].setText("YOU WIN CONGRATULATIONS");
msgs[16].setText("WOULD YOU LIKE TO PLAY AGAIN");
msgs[17].setText("IF SO PRESS RESTART");
disableall();
}else{
lives--;
msgs[15].setText("NICE TRY");
msgs[16].setText("BUT YOUR WRONG");
}
}
}

You might also like