Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************
- * Program: Final Group Project
- * Author: Jacob Webb, Alex Hoch, Steve Hergert
- * Class: Java
- * Date: 11/30/2011
- * Description: Hangman Game
- *
- * I certify that this program is either (1) my original work or
- * (2) my original modification of a program published in the
- * course textbook or provided by the instructor.
- *
- * I understand that even though I am allowed to discuss the ideas
- * of this program with someone else, I cannot copy someone else's
- * code and must write this program on my own. I understand that
- * in the case of academic dishonesty, I will receive a zero for this
- * program and may by subject to the College's disciplinary actions.
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.JRadioButton;
- public class HangmanPanel extends JPanel {
- private JButton 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, submit, newGame, clear, hard, easy;
- private JTextField guessPhraseField;
- private JLabel photoLabel, wordLabel, letter1, letter2, letter3, letter4,
- letter5, letter6, difficulty;
- private JLabel guessr = new JLabel();
- private Dictionary dict1;
- private boolean letterBool[] = { false, false, false, false, false, false };
- private char guessLetter = '*';
- private String letter;
- private int guessrr, randomWord;
- private int wrongLetter = 0;
- JLabel letters[] = { letter1, letter2, letter3, letter4, letter5, letter6 };
- // optimization GOOOOOOOOOOOOO -S
- public char alphabet[] = { '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' };
- private JButton alphabetButton[] = { 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 };
- private String alphabetString[] = { "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" };
- private ImageIcon startGame, addHead, addBody, addOneArm, addBothArm,
- addOneLeg, gameOver;
- private boolean allTrue = false;
- /*
- * getter and setter methods
- */
- public void setAllEnabled(boolean isEnabled) {
- for (int alphButtonIndex = 0; alphButtonIndex < 26; alphButtonIndex++) {
- // sets all of the buttons enabled (self-explanatory)
- alphabetButton[alphButtonIndex].setEnabled(isEnabled);
- }
- }
- public HangmanPanel() {
- dict1 = new Dictionary();
- System.out.println(dict1);
- guessrr = 6;
- // hangmanStand = new ImageIcon(getClass().getResource("hangman0.png"));
- // photoLabel = new JLabel(hangmanStand);
- LetterButtonListener listener = new LetterButtonListener();
- // new game button -S
- newGame = new JButton("New Game");
- for (int alphStringIndex = 0; alphStringIndex < 26; alphStringIndex++) {
- // adds listeners for all of the buttons
- alphabetButton[alphStringIndex] = new JButton(
- alphabetString[alphStringIndex]);
- alphabetButton[alphStringIndex].addActionListener(listener);
- }
- guessr = new JLabel("Guesses Remaining: " + guessrr);
- wordLabel = new JLabel("Word: ");
- // creates a place to enter the phrase (still needs listener) -J
- // got it! :) -S
- guessPhraseField = new JTextField(40);
- guessPhraseField.addActionListener(listener);
- guessPhraseField
- .setText("If you think you know what the whole word is, "
- + "enter it here (6 letters)");
- // creates a submit button for submitting phrase guess (still needs
- // listener) -J
- // got this too -S
- submit = new JButton("Submit");
- submit.addActionListener(listener);
- clear = new JButton("Clear");
- clear.addActionListener(listener);
- for (int letterLabelIndex = 0; letterLabelIndex < 6; letterLabelIndex++) {
- letters[letterLabelIndex] = new JLabel("-");
- }
- add(newGame);
- add(Box.createRigidArea(new Dimension(500, 0)));
- newGame.addActionListener(listener);
- // Creates the letter buttons on the panel -J
- for (int alphButtonIndex = 0; alphButtonIndex < 26; alphButtonIndex++) {
- add(alphabetButton[alphButtonIndex]);
- }
- // adds phrase field to panel
- add(guessPhraseField);
- add(submit);
- add(clear);
- add(Box.createRigidArea(new Dimension(500, 0)));
- // see if we can mess with the layout temporarily because it's hard to
- // see right now
- startGame = new ImageIcon(getClass().getResource("startGame.JPG"));
- addHead = new ImageIcon(getClass().getResource("head2.JPG"));
- addBody = new ImageIcon(getClass().getResource("head-body.JPG"));
- addOneArm = new ImageIcon(getClass().getResource("h-b-oneArm.JPG"));
- addBothArm = new ImageIcon(getClass().getResource("h-b-bothArms.JPG"));
- addOneLeg = new ImageIcon(getClass().getResource("h-b-a-oneLeg.JPG"));
- gameOver = new ImageIcon(getClass().getResource("gameOver.JPG"));
- photoLabel = new JLabel(startGame);
- add(wordLabel);
- for (int letterIndex = 0; letterIndex < 6; letterIndex++) {
- add(letters[letterIndex]);
- System.out.print("- ");
- }
- add(photoLabel);
- add(Box.createRigidArea(new Dimension(500, 0)));
- add(guessr);;
- difficulty = new JLabel ("Is that all you got?");
- difficulty.setFont(new Font("Candara", Font.ITALIC, 52));
- hard = new JButton ("Enter Hard Mode!");
- hard.addActionListener(listener);
- add(difficulty);
- add(hard);
- easy = new JButton ("Back to Easy Mode");
- easy.addActionListener(listener);
- setPreferredSize(new Dimension(500, 500));
- // int rand = (int) (Math.random() * length); -A
- // System.out.println("Random number is: " + rand);
- // System.out.println("Random word is: " + word);
- }
- private class LetterButtonListener implements ActionListener {
- public void actionPerformed(ActionEvent event) {
- for (int alphIndex = 0; alphIndex < 26; alphIndex++) {
- if (event.getSource() == alphabetButton[alphIndex]) {
- letter = alphabetButton[alphIndex].getText();
- guessLetter = letter.charAt(0);
- alphabetButton[alphIndex].setEnabled(false);
- System.out.println("Guessed letter: " + guessLetter);
- }
- }
- if (event.getSource() == newGame || event.getSource() == easy) {
- remove(easy);
- System.out.println("New game");
- guessLetter = '*';
- setAllEnabled(true);
- submit.setEnabled(true);
- clear.setEnabled(true);
- photoLabel.setIcon(startGame);
- dict1.setWord(randomWord);
- dict1.getWord();
- System.out.println(dict1);
- wordLabel.setText("Word: ");
- guessrr = 6;
- add(wordLabel);
- guessPhraseField.setEnabled(true);
- for (int ch = 0; ch < 6; ch++) {
- letters[ch].setText("-");
- System.out.print("- ");
- add(letters[ch]);
- }
- add(photoLabel);
- add(Box.createRigidArea(new Dimension(500, 0)));
- guessr.setText("Guesses Remaining: " + guessrr);
- add(guessr);
- // re-add the image here so it doesn't move the -'s around
- // set all back to enabled, generate a new word, change text
- // field to empty
- guessPhraseField
- .setText("If you think you know what the whole word is, enter it here (6 letters)");
- difficulty.setText("Is that all you got?");
- add(difficulty);
- add(hard);
- }
- if (event.getSource() == hard) {
- remove(hard);
- System.out.println("New game");
- guessLetter = '*';
- setAllEnabled(true);
- submit.setEnabled(true);
- clear.setEnabled(true);
- photoLabel.setIcon(startGame);
- dict1.setHard(randomWord);
- dict1.getHard();
- System.out.println(dict1);
- wordLabel.setText("Word: ");
- guessrr = 6;
- add(wordLabel);
- guessPhraseField.setEnabled(true);
- for (int ch = 0; ch < 6; ch++) {
- letters[ch].setText("-");
- System.out.print("- ");
- add(letters[ch]);
- }
- add(photoLabel);
- add(Box.createRigidArea(new Dimension(500, 0)));
- guessr.setText("Guesses Remaining: " + guessrr);
- add(guessr);
- // re-add the image here so it doesn't move the -'s around
- // set all back to enabled, generate a new word, change text
- // field to empty
- guessPhraseField
- .setText("If you think you know what the whole word is, enter it here (6 letters)");
- difficulty.setText("Can't handle the heat?");
- add(difficulty);
- add(easy);
- }
- if (event.getSource() == clear) {
- guessPhraseField.setText("");
- }
- // if you press the buttons and they are correct, the dashes change
- // to the buttons
- for (int ch = 0; ch < dict1.getWord().length(); ch++) {
- if (guessLetter == (dict1.getWord().charAt(ch))) {
- // if guessLetter is equal to the char of index variable ch,
- letters[ch].setText(letter);
- // set letter text to the letter
- letterBool[ch] = true;
- // set each letter to true once they are correct
- System.out.println(guessLetter + " is correct");
- }
- }
- if (guessLetter != (dict1.getWord().charAt(0))
- && guessLetter != (dict1.getWord().charAt(1))
- && guessLetter != (dict1.getWord().charAt(2))
- && guessLetter != (dict1.getWord().charAt(3))
- && guessLetter != (dict1.getWord().charAt(4))
- && guessLetter != (dict1.getWord().charAt(5))
- && (event.getSource() != submit && event.getSource() != guessPhraseField)) {
- wrongLetter++;
- // fixed: when you'd enter a word by guessing the entire word,
- // it would still deduct a number from Guesses Remaining -S
- if (event.getSource() == submit || event.getSource() == clear
- || event.getSource() == newGame)
- wrongLetter = 0;
- if (event.getSource() == easy || event.getSource() == hard)
- wrongLetter = 0;
- if (wrongLetter == 1) {
- guessrr = 5;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(addHead);
- }
- if (wrongLetter == 2) {
- guessrr = 4;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(addBody);
- }
- if (wrongLetter == 3) {
- guessrr = 3;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(addOneArm);
- }
- if (wrongLetter == 4) {
- guessrr = 2;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(addBothArm);
- }
- if (wrongLetter == 5) {
- guessrr = 1;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(addOneLeg);
- }
- if (wrongLetter == 6) {
- guessrr = 0;
- guessr.setText("Guesses Remaining: " + guessrr);
- photoLabel.setIcon(gameOver);
- wordLabel.setText("Game Over Sucka!");
- guessPhraseField.setEnabled(false);
- setAllEnabled(false);
- submit.setEnabled(false);
- clear.setEnabled(false);
- guessPhraseField.setText("");
- for (int ch = 0; ch < 6; ch++) {
- remove(letters[ch]);
- }
- }
- }
- // bugs I've found: all the for loops need letters to be at 25
- // at game over the user can still enter the word in the
- // textfield to win
- // letters don't reset when new game is pressed -J
- // if they guess the entire word and get it correct:
- if ((event.getSource() == submit || event.getSource() == guessPhraseField)
- && allTrue == false) {
- String guessedWord = guessPhraseField.getText();
- if (guessedWord.equalsIgnoreCase(dict1.getWord())) {
- wordLabel.setText("Correct! Word is: " + dict1.getWord());
- setAllEnabled(false);
- submit.setEnabled(false);
- clear.setEnabled(false);
- guessLetter = '*';
- guessPhraseField.setText("");
- guessPhraseField.setEnabled(false);
- for (int ch = 0; ch < 6; ch++) {
- remove(letters[ch]);
- }
- // can't get this to work, want to have it set the dashes
- // (-) to the exact letters, too tired though :(
- /*
- * for (int ch = 0; ch < 5; ch++) {
- * letters[ch].setText(word[index].charAt(ch)); }
- */
- // instead maybe it will work to remove the dashes and
- // include the word in the "correct!" string... -S
- for (int ch = 0; ch < 6; ch++) {
- remove(letters[ch]);
- }
- } else if (!guessedWord.equalsIgnoreCase(dict1.getWord())) {
- photoLabel.setIcon(gameOver);
- wordLabel.setText("Game Over Sucka!");
- setAllEnabled(false);
- submit.setEnabled(false);
- clear.setEnabled(false);
- guessPhraseField.setText("");
- guessPhraseField.setEnabled(false);
- for (int ch = 0; ch < 6; ch++) {
- remove(letters[ch]);
- }
- guessrr = 0;
- guessr.setText("Guesses Remaining: " + guessrr);
- }
- for (int ch = 0; ch < 6; ch++) {
- if ((guessLetter == (dict1.getWord().charAt(ch)))
- || (letters[ch].equals(dict1.getWord().charAt(ch)))) {
- setAllEnabled(false);
- guessPhraseField.setText("");
- guessPhraseField.setEnabled(false);
- wordLabel.setText("You win! Word is: " + dict1);
- submit.setEnabled(false);
- clear.setEnabled(false);
- remove(letters[ch]);
- }
- }
- }
- if (letterBool[0] == true && letterBool[1] == true
- && letterBool[2] == true && letterBool[3] == true
- && letterBool[4] == true && letterBool[5] == true) {
- setAllEnabled(false);
- allTrue = true;
- wordLabel.setText("Winner! Word is: " + dict1);
- submit.setEnabled(false);
- clear.setEnabled(false);
- guessPhraseField.setText("");
- guessPhraseField.setEnabled(false);
- for (int ch = 0; ch < 6; ch++) {
- remove(letters[ch]);
- letterBool[ch] = false;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment