#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Hangman Game Menu


    Hi, I have to create a game and enter a game menu. I have the code written for the game and it runs. However, I don't know how to put in a game menu. Can someone please help? Here's my game code so far:


    Code:
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    import java.net.*;
    import javax.swing.JMenuBar;
    public class Hangman extends BasicGame
    {
        private final String [] imgs; 
        private String secretWord;
        private String letter;
        private String[ ] displayWord;
        private String[ ] words;
        private static java.util.Random randy = new java.util.Random();
        private int dictionarySize;
        private int guessesLeft;
        private final int guessesAllowed = 6;
         
        public static void main(String[] args) 
        {    
            Hangman test = new Hangman();
            test.playManyGames();
            }
        
        public Hangman()
        {
            super();
            //words = new String[];
            words = new String [] {"jazzy", "fuzzy", "muzzy", "fezzy", "fizzy", "whizzy", "abuse", "scuzzy", "zuzim", "dizzy", 
                                   "frizz", "huzza", "mezzo", "pizza", "hajji", "jujus","tizzy", "zanza", "zanzen", "jacky",
                                   "jumpy", "tazza", "tazze", "zizit", "jammy", "jemmy", "jiffy", "jimmy", "jimpy", "junky",
                                   "mujik", "quack", "quick", "zappy", "zippy", "jacks", "jocko", "jocks", "jugum", "jumbo",
                                   "jumps", "kopje", "kudzu", "quaff", "quaky", "quiff", "zaxes", "zinky", "capiz", "enzym"};
            dictionarySize = words.length;
            imgs = new String [] {"(URL address )",
                                  "(URL address)",
                                  "(URL address)",
                                  "(URL address)",
                                  "(URL address)",
                                  "(URL address)",
                                  "(URL address)"};
            reset();
        }
        
        public void askUsersFirstChoice()
        {   
            askUsersNextChoice();
            
        }
        
        public void askUsersNextChoice()
        {  
            boolean correct = false;
            String a = ("");
            String displayString = displayWord [0] + " " + displayWord [1] + " " + displayWord [2] + " " + displayWord [3]  + " " + displayWord [4];
            try
            {   ImageIcon hangmanIcon = new ImageIcon(new URL(imgs[guessesAllowed - guessesLeft]));
                JOptionPane.showMessageDialog(null, displayString, "Hangman", JOptionPane.INFORMATION_MESSAGE, hangmanIcon);
            }
            
            catch (Exception e)
            {  
               JOptionPane.showMessageDialog(null, "could not retrieve images from internet");
               JOptionPane.showMessageDialog(null,displayString);
            } 
            //JOptionPane.showMessageDialog(null,displayString);
            while(a.equals(""))
            {
                a = JOptionPane.showInputDialog("Guess a letter: ");
                if (a != null && ! a.equals (""))
                letter = a;
                else
                {
                    System.exit(0);
                }
            }
            
            for (int i = 0; i < secretWord.length(); i++)
            {
                if (a.equals (secretWord.substring (i,i+1)))
                {
                    correct = true;
                    displayWord [i] = a;
                }
            }  //======================
           if (!correct)
            {
            guessesLeft --;
            }
        }
        
        public boolean shouldContinue() 
        {   
            if (guessesLeft <= 0)
                return false;
            return !isCorrect();
        }  //======================
       
        private boolean isCorrect()
        {
            for (int i = 0; i < secretWord.length(); i++)
            {
                String a = "_";
                if (a.equals (displayWord [i]))
                {
                    return false;
                }
            }  //======================
            return true;
        }
       
        public void showUpdatedStatus()
        {
        }
       
        public void showFinalStatus()
        {
            if (!isCorrect())
            {
            try
            {   ImageIcon hangmanIcon = new ImageIcon(new URL(imgs[guessesAllowed]));
                JOptionPane.showMessageDialog(null, "Sorry, you are incorrect. The secret word was " + secretWord, "Hangman", JOptionPane.INFORMATION_MESSAGE, hangmanIcon);
            }
            
            catch (Exception e)
            {  
               JOptionPane.showMessageDialog(null, "could not retrieve images from internet");
               JOptionPane.showMessageDialog(null,"Sorry, you are incorrect. The secret word was " + secretWord);
            } 
            }
            else
                JOptionPane.showMessageDialog(null,"Goodjob, you are correct. The secret word was " + secretWord);  
            reset();
        }
       
        public void reset()
        {
            guessesLeft = guessesAllowed;
            int tim = randy.nextInt (dictionarySize);
            secretWord = words [tim];
            displayWord = new String [] {"_", "_", "_", "_", "_"};
        }
      }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    Thanks, I edited it
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    What kind of menu are you trying to add to the program? How will it work? How will the user chose one of the items in the menu?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    A game menu where the gamer can choose play or exit.
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    How will the menu be shown to the user?
    What will the user see when the menu is shown?
    How will the user choose an item?
    What will the code do when the user chooses an item in the menu?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    The user will click a button. If they click play, it will go to the game. If they click exit, the game will exit (System.exit(0) is what I usually use to exit).
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    If there are only those two choices: play or exit, that should be easy. What problems are you having?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    I just do not know how to put it into the code
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Can you use one of the JOptionPane show methods to ask the question and get the user's response?


    Can you make a list of steps for how the program should work?
    Something like:
    begin loop
    ask user question: play or exit
    if user says exit then exit
    play the game
    end loop ==> goes back to top of loop
    Last edited by NormR; December 12th, 2013 at 08:27 AM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0
    Yes
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Our posts crossed. See end of my last post.

IMN logo majestic logo threadwatch logo seochat tools logo