Guest User

Untitled

a guest
Apr 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 21.49 KB | None | 0 0
  1. /******************************************************
  2.  * Program:   Final Group Project
  3.  * Author:     Jacob Webb, Alex Hoch, Steve Hergert
  4.  * Class:     Java
  5.  * Date:     11/30/2011
  6.  * Description: Hangman Game
  7.  *
  8.  * I certify that this program is either (1) my original work or
  9.  * (2) my original modification of a program published in the
  10.  * course textbook or provided by the instructor.
  11.  *
  12.  * I understand that even though I am allowed to discuss the ideas
  13.  * of this program with someone else, I cannot copy someone else's
  14.  * code and must write this program on my own.  I understand that
  15.  * in the case of academic dishonesty, I will receive a zero for this
  16.  * program and may by subject to the College's disciplinary actions.
  17.  */
  18.  
  19. import javax.swing.*;
  20. import java.awt.*;
  21. import java.awt.event.*;
  22. import javax.swing.JRadioButton;
  23.  
  24.  
  25.  
  26. public class HangmanPanel extends JPanel {
  27.         private JButton a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t,
  28.                         u, v, w, x, y, z, submit, newGame, clear, hard, easy;
  29.         private JTextField guessPhraseField;
  30.         private JLabel photoLabel, wordLabel, letter1, letter2, letter3, letter4,
  31.                         letter5, letter6, difficulty;
  32.         private JLabel guessr = new JLabel();
  33.         private Dictionary dict1;
  34.  
  35.         private boolean letterBool[] = { false, false, false, false, false, false };
  36.         private char guessLetter = '*';
  37.         private String letter;
  38.  
  39.         private int guessrr, randomWord;
  40.         private int wrongLetter = 0;
  41.         JLabel letters[] = { letter1, letter2, letter3, letter4, letter5, letter6 };
  42.         // optimization GOOOOOOOOOOOOO -S
  43.         public char alphabet[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
  44.                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  45.                         'w', 'x', 'y', 'z' };
  46.         private JButton alphabetButton[] = { a, b, c, d, e, f, g, h, i, j, k, l, m,
  47.                         n, o, p, q, r, s, t, u, v, w, x, y, z };
  48.         private String alphabetString[] = { "A", "B", "C", "D", "E", "F", "G", "H",
  49.                         "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
  50.                         "V", "W", "X", "Y", "Z" };
  51.         private ImageIcon startGame, addHead, addBody, addOneArm, addBothArm,
  52.                         addOneLeg, gameOver;
  53.         private boolean allTrue = false;
  54.  
  55.  
  56.  
  57.  
  58.         /*
  59.          * getter and setter methods
  60.          */
  61.  
  62.         public void setAllEnabled(boolean isEnabled) {
  63.                 for (int alphButtonIndex = 0; alphButtonIndex < 26; alphButtonIndex++) {
  64.                         // sets all of the buttons enabled (self-explanatory)
  65.                         alphabetButton[alphButtonIndex].setEnabled(isEnabled);
  66.                 }
  67.         }
  68.        
  69.         public HangmanPanel() {
  70.            
  71.                 dict1 = new Dictionary();
  72.                 System.out.println(dict1);
  73.                 guessrr = 6;
  74.  
  75.                 // hangmanStand = new ImageIcon(getClass().getResource("hangman0.png"));
  76.                 // photoLabel = new JLabel(hangmanStand);
  77.                 LetterButtonListener listener = new LetterButtonListener();
  78.                 // new game button -S
  79.                 newGame = new JButton("New Game");
  80.                 for (int alphStringIndex = 0; alphStringIndex < 26; alphStringIndex++) {
  81.                         // adds listeners for all of the buttons
  82.                         alphabetButton[alphStringIndex] = new JButton(
  83.                                         alphabetString[alphStringIndex]);
  84.                         alphabetButton[alphStringIndex].addActionListener(listener);
  85.                 }
  86.                 guessr = new JLabel("Guesses Remaining: " + guessrr);
  87.                 wordLabel = new JLabel("Word: ");
  88.                 // creates a place to enter the phrase (still needs listener) -J
  89.                 // got it! :) -S
  90.                 guessPhraseField = new JTextField(40);
  91.                 guessPhraseField.addActionListener(listener);
  92.                 guessPhraseField
  93.                                 .setText("If you think you know what the whole word is, "
  94.                                                 + "enter it here (6 letters)");
  95.                 // creates a submit button for submitting phrase guess (still needs
  96.                 // listener) -J
  97.                 // got this too -S
  98.                 submit = new JButton("Submit");
  99.                 submit.addActionListener(listener);
  100.                 clear = new JButton("Clear");
  101.                 clear.addActionListener(listener);
  102.                 for (int letterLabelIndex = 0; letterLabelIndex < 6; letterLabelIndex++) {
  103.                         letters[letterLabelIndex] = new JLabel("-");
  104.                 }
  105.                 add(newGame);
  106.                 add(Box.createRigidArea(new Dimension(500, 0)));
  107.                 newGame.addActionListener(listener);
  108.                 // Creates the letter buttons on the panel -J
  109.                 for (int alphButtonIndex = 0; alphButtonIndex < 26; alphButtonIndex++) {
  110.                         add(alphabetButton[alphButtonIndex]);
  111.                 }
  112.                 // adds phrase field to panel
  113.                 add(guessPhraseField);
  114.                 add(submit);
  115.                 add(clear);
  116.                 add(Box.createRigidArea(new Dimension(500, 0)));
  117.                 // see if we can mess with the layout temporarily because it's hard to
  118.                 // see right now
  119.                 startGame = new ImageIcon(getClass().getResource("startGame.JPG"));
  120.                 addHead = new ImageIcon(getClass().getResource("head2.JPG"));
  121.                 addBody = new ImageIcon(getClass().getResource("head-body.JPG"));
  122.                 addOneArm = new ImageIcon(getClass().getResource("h-b-oneArm.JPG"));
  123.                 addBothArm = new ImageIcon(getClass().getResource("h-b-bothArms.JPG"));
  124.                 addOneLeg = new ImageIcon(getClass().getResource("h-b-a-oneLeg.JPG"));
  125.                 gameOver = new ImageIcon(getClass().getResource("gameOver.JPG"));
  126.                 photoLabel = new JLabel(startGame);
  127.  
  128.                 add(wordLabel);
  129.                 for (int letterIndex = 0; letterIndex < 6; letterIndex++) {
  130.                         add(letters[letterIndex]);
  131.                         System.out.print("- ");
  132.                 }
  133.  
  134.                
  135.  
  136.                 add(photoLabel);
  137.                 add(Box.createRigidArea(new Dimension(500, 0)));
  138.                 add(guessr);;
  139.                 difficulty = new JLabel ("Is that all you got?");
  140.                 difficulty.setFont(new Font("Candara", Font.ITALIC, 52));
  141.                 hard = new JButton ("Enter Hard Mode!");
  142.                 hard.addActionListener(listener);
  143.                 add(difficulty);
  144.                 add(hard);
  145.                 easy = new JButton ("Back to Easy Mode");
  146.                 easy.addActionListener(listener);
  147.                
  148.                
  149.                 setPreferredSize(new Dimension(500, 500));
  150.                 // int rand = (int) (Math.random() * length); -A
  151.                 // System.out.println("Random number is: " + rand);
  152.                 // System.out.println("Random word is: " + word);
  153.  
  154.         }
  155.        
  156.  
  157.         private class LetterButtonListener implements ActionListener {
  158.                 public void actionPerformed(ActionEvent event) {
  159.  
  160.                         for (int alphIndex = 0; alphIndex < 26; alphIndex++) {
  161.                                 if (event.getSource() == alphabetButton[alphIndex]) {
  162.                                         letter = alphabetButton[alphIndex].getText();
  163.                                         guessLetter = letter.charAt(0);
  164.                                         alphabetButton[alphIndex].setEnabled(false);
  165.                                         System.out.println("Guessed letter: " + guessLetter);
  166.                                 }
  167.                         }
  168.                         if (event.getSource() == newGame || event.getSource() == easy) {
  169.                                 remove(easy);  
  170.                                 System.out.println("New game");
  171.                                 guessLetter = '*';
  172.                                 setAllEnabled(true);
  173.                                 submit.setEnabled(true);
  174.                                 clear.setEnabled(true);
  175.                                 photoLabel.setIcon(startGame);
  176.                                 dict1.setWord(randomWord);
  177.                                 dict1.getWord();
  178.                                 System.out.println(dict1);
  179.                                 wordLabel.setText("Word: ");
  180.                                 guessrr = 6;
  181.                                 add(wordLabel);
  182.                                 guessPhraseField.setEnabled(true);
  183.                                 for (int ch = 0; ch < 6; ch++) {
  184.                                         letters[ch].setText("-");
  185.                                         System.out.print("- ");
  186.                                         add(letters[ch]);
  187.                                 }
  188.                                 add(photoLabel);
  189.                                 add(Box.createRigidArea(new Dimension(500, 0)));
  190.                                 guessr.setText("Guesses Remaining: " + guessrr);
  191.                                 add(guessr);
  192.                                 // re-add the image here so it doesn't move the -'s around
  193.                                 // set all back to enabled, generate a new word, change text
  194.                                 // field to empty
  195.                                 guessPhraseField
  196.                                                 .setText("If you think you know what the whole word is, enter it here (6 letters)");
  197.                                 difficulty.setText("Is that all you got?");
  198.                                 add(difficulty);
  199.                                 add(hard);
  200.                                
  201.                         }
  202.                        
  203.                         if (event.getSource() == hard) {
  204.                             remove(hard);
  205.                             System.out.println("New game");
  206.                             guessLetter = '*';
  207.                            
  208.                             setAllEnabled(true);
  209.                             submit.setEnabled(true);
  210.                             clear.setEnabled(true);
  211.                             photoLabel.setIcon(startGame);
  212.                             dict1.setHard(randomWord);
  213.                             dict1.getHard();
  214.                             System.out.println(dict1);
  215.                             wordLabel.setText("Word: ");
  216.                             guessrr = 6;
  217.                             add(wordLabel);
  218.                             guessPhraseField.setEnabled(true);
  219.                             for (int ch = 0; ch < 6; ch++) {
  220.                                     letters[ch].setText("-");
  221.                                     System.out.print("- ");
  222.                                     add(letters[ch]);
  223.                             }
  224.                             add(photoLabel);
  225.  
  226.                             add(Box.createRigidArea(new Dimension(500, 0)));
  227.                             guessr.setText("Guesses Remaining: " + guessrr);
  228.                             add(guessr);
  229.                             // re-add the image here so it doesn't move the -'s around
  230.                             // set all back to enabled, generate a new word, change text
  231.                             // field to empty
  232.                             guessPhraseField
  233.                                             .setText("If you think you know what the whole word is, enter it here (6 letters)");
  234.                             difficulty.setText("Can't handle the heat?");
  235.                             add(difficulty);
  236.                             add(easy);
  237.                            
  238.  
  239.                     }
  240.                         if (event.getSource() == clear) {
  241.                                 guessPhraseField.setText("");
  242.                         }
  243.                         // if you press the buttons and they are correct, the dashes change
  244.                         // to the buttons
  245.                         for (int ch = 0; ch < dict1.getWord().length(); ch++) {
  246.                                 if (guessLetter == (dict1.getWord().charAt(ch))) {
  247.                                         // if guessLetter is equal to the char of index variable ch,
  248.                                         letters[ch].setText(letter);
  249.                                         // set letter text to the letter
  250.                                         letterBool[ch] = true;
  251.                                         // set each letter to true once they are correct
  252.                                         System.out.println(guessLetter + " is correct");
  253.                                 }
  254.                         }
  255.  
  256.                         if (guessLetter != (dict1.getWord().charAt(0))
  257.                                         && guessLetter != (dict1.getWord().charAt(1))
  258.                                         && guessLetter != (dict1.getWord().charAt(2))
  259.                                         && guessLetter != (dict1.getWord().charAt(3))
  260.                                         && guessLetter != (dict1.getWord().charAt(4))
  261.                                         && guessLetter != (dict1.getWord().charAt(5))
  262.                                         && (event.getSource() != submit && event.getSource() != guessPhraseField)) {
  263.                                 wrongLetter++;
  264.                                 // fixed: when you'd enter a word by guessing the entire word,
  265.                                 // it would still deduct a number from Guesses Remaining -S
  266.                                 if (event.getSource() == submit || event.getSource() == clear
  267.                                                 || event.getSource() == newGame)
  268.                                         wrongLetter = 0;
  269.  
  270.                                 if (event.getSource() == easy || event.getSource() == hard)
  271.                                 wrongLetter = 0;
  272.                                
  273.                                 if (wrongLetter == 1) {
  274.                                         guessrr = 5;
  275.                                         guessr.setText("Guesses Remaining: " + guessrr);
  276.                                         photoLabel.setIcon(addHead);
  277.                                 }
  278.                                 if (wrongLetter == 2) {
  279.                                         guessrr = 4;
  280.                                         guessr.setText("Guesses Remaining: " + guessrr);
  281.                                         photoLabel.setIcon(addBody);
  282.                                 }
  283.                                 if (wrongLetter == 3) {
  284.                                         guessrr = 3;
  285.                                         guessr.setText("Guesses Remaining: " + guessrr);
  286.                                         photoLabel.setIcon(addOneArm);
  287.                                 }
  288.                                 if (wrongLetter == 4) {
  289.                                         guessrr = 2;
  290.                                         guessr.setText("Guesses Remaining: " + guessrr);
  291.                                         photoLabel.setIcon(addBothArm);
  292.                                 }
  293.                                 if (wrongLetter == 5) {
  294.                                         guessrr = 1;
  295.                                         guessr.setText("Guesses Remaining: " + guessrr);
  296.                                         photoLabel.setIcon(addOneLeg);
  297.                                 }
  298.                                 if (wrongLetter == 6) {
  299.                                         guessrr = 0;
  300.                                         guessr.setText("Guesses Remaining: " + guessrr);
  301.                                         photoLabel.setIcon(gameOver);
  302.                                         wordLabel.setText("Game Over Sucka!");
  303.                                         guessPhraseField.setEnabled(false);
  304.                                         setAllEnabled(false);
  305.                                         submit.setEnabled(false);
  306.                                         clear.setEnabled(false);
  307.                                         guessPhraseField.setText("");
  308.                                         for (int ch = 0; ch < 6; ch++) {
  309.                                                 remove(letters[ch]);
  310.                                         }
  311.                                 }
  312.                         }
  313.  
  314.                         // bugs I've found: all the for loops need letters to be at 25
  315.                         // at game over the user can still enter the word in the
  316.                         // textfield to win
  317.                         // letters don't reset when new game is pressed -J
  318.  
  319.                         // if they guess the entire word and get it correct:
  320.                         if ((event.getSource() == submit || event.getSource() == guessPhraseField)
  321.                                         && allTrue == false) {
  322.                                 String guessedWord = guessPhraseField.getText();
  323.                                 if (guessedWord.equalsIgnoreCase(dict1.getWord())) {
  324.                                         wordLabel.setText("Correct! Word is: " + dict1.getWord());
  325.                                         setAllEnabled(false);
  326.                                         submit.setEnabled(false);
  327.                                         clear.setEnabled(false);
  328.                                         guessLetter = '*';
  329.                                         guessPhraseField.setText("");
  330.                                         guessPhraseField.setEnabled(false);
  331.                                         for (int ch = 0; ch < 6; ch++) {
  332.                                                 remove(letters[ch]);
  333.                                         }
  334.                                         // can't get this to work, want to have it set the dashes
  335.                                         // (-) to the exact letters, too tired though :(
  336.                                         /*
  337.                                          * for (int ch = 0; ch < 5; ch++) {
  338.                                          * letters[ch].setText(word[index].charAt(ch)); }
  339.                                          */
  340.                                         // instead maybe it will work to remove the dashes and
  341.                                         // include the word in the "correct!" string... -S
  342.                                         for (int ch = 0; ch < 6; ch++) {
  343.                                                 remove(letters[ch]);
  344.                                         }
  345.                                 } else if (!guessedWord.equalsIgnoreCase(dict1.getWord())) {
  346.                                         photoLabel.setIcon(gameOver);
  347.                                         wordLabel.setText("Game Over Sucka!");
  348.                                         setAllEnabled(false);
  349.                                         submit.setEnabled(false);
  350.                                         clear.setEnabled(false);
  351.                                         guessPhraseField.setText("");
  352.                                         guessPhraseField.setEnabled(false);
  353.                                         for (int ch = 0; ch < 6; ch++) {
  354.                                                 remove(letters[ch]);
  355.                                         }
  356.                                         guessrr = 0;
  357.                                         guessr.setText("Guesses Remaining: " + guessrr);
  358.                                 }
  359.                                 for (int ch = 0; ch < 6; ch++) {
  360.                                         if ((guessLetter == (dict1.getWord().charAt(ch)))
  361.                                                         || (letters[ch].equals(dict1.getWord().charAt(ch)))) {
  362.                                                 setAllEnabled(false);
  363.                                                 guessPhraseField.setText("");
  364.                                                 guessPhraseField.setEnabled(false);
  365.                                                 wordLabel.setText("You win! Word is: " + dict1);
  366.                                                 submit.setEnabled(false);
  367.                                                 clear.setEnabled(false);
  368.                                                 remove(letters[ch]);
  369.                                         }
  370.                                 }
  371.                         }
  372.                         if (letterBool[0] == true && letterBool[1] == true
  373.                                         && letterBool[2] == true && letterBool[3] == true
  374.                                         && letterBool[4] == true && letterBool[5] == true) {
  375.                                 setAllEnabled(false);
  376.                                 allTrue = true;
  377.                                 wordLabel.setText("Winner! Word is: " + dict1);
  378.                                 submit.setEnabled(false);
  379.                                 clear.setEnabled(false);
  380.                                 guessPhraseField.setText("");
  381.                                 guessPhraseField.setEnabled(false);
  382.                                 for (int ch = 0; ch < 6; ch++) {
  383.                                         remove(letters[ch]);
  384.                                         letterBool[ch] = false;
  385.                                 }
  386.  
  387.                         }
  388.                 }
  389.         }
  390. }
Add Comment
Please, Sign In to add comment