The goal of this project is to become more familiar with creating and using classes and objects in Java, to further introduce you to the concepts of abstraction and modularization, and to directly use collections of items.

Instructions

Create a new Java project named “Project5_netid” (where you replace netid with your NetID). You can start from your Project4 project.

Create a RandomString class and implement the following:

  • Create a file named guess_phrases.txt in the Project5_netid project that contains phrases to be guessed in your Hangman game. This file will have one guess phrase per line. Make sure this file is in the topmost project folder, not inside your src folder.
  • A constructor that receives the name of a file to get string values from. The constructor should read in the phrases from the file and store them for later use.
  • next – a method that returns a random string value from the file; this value shouldn’t be repeated until all guess phrases in the file have been used.
  • Create a main method to test that next is working correctly by repeatedly calling next & printing the result – you should not have any repeats, and the phrases should not be in the same order as in the file.

Create a GuessPhrasePanel class and implement the following:

  • Has a constructor taking a String guess phrase
  • Shows all the letters in the phrase, initially just showing the underline for all alphabet letters (not the letter as it needs to be guessed). Spaces and other punctuation should be shown with no underline.
  • setPhrase – sets a new phrase to this GuessPhrase object, gets rid of old phrase parts and creates new ones
  • hasLetter – a method that returns whether or not a specified letter is in the phrase
  • revealLetter – a method that reveals or shows a specified letter (if there are more than one of that letter, all should be revealed)
  • isFullPhraseRevealed – returns whether or not the full phrase has been revealed
  • revealFullPhrase – reveals the full phrase
  • SPECIAL NOTE: make sure your methods works the same for upper and lower case letters

Create a HangmanGame class to test GuessPhrasePanel

  • Create fields for RandomString and GuessPhrasePanel objects
  • Create a constructor that initializes the fields and adds a GuessPhrase panel to a JFrame. The constructor should also add a KeyListener to the frame that does the following:
    • reveals the letter typed if it is in the guess phrase
    • reveals the whole phrase if the enter key is pressed
    • gets a random string and sets it as the text for the GuessPhrase if the space bar is pressed
    • does nothing if any other key is pressed
  • Create a main that calls the HangmanGame constructor

EXTRA CREDIT. Add the following functionality to your game:

  • Write a method getDifficultyLevel that returns 0 if it is easy, 1 if it is medium, 2 if it is difficult. Some ideas in “computing” this method are a combination of length and the number of different letters, if you want to get even more points you could use letter frequencies such as those described at: http://en.wikipedia.org/wiki/Letter_frequency.
  • Display the difficulty level somewhere in your game the difficulty level of the current phrase, this should be something that makes sense to a player (e.g. not a 0-2)

Be sure to:

  • Use proper indentation
  • Use appropriate variable/field names
  • Group like things together (items that are part of the same objects)
  • Use appropriate comments – including comments for each method
  • Check your curly braces

Hints / tips

  • When initializing your GuessPhrasePanel and creating the setPhrase method, you may find it useful to create a private initializeLetters() method to be used by both the constructor and setPhrase. It would look like:
private void initializeLetters() { this.removeAll(); // throw away existing letters in phrase this.updateUI(); // reset entire UI // add code statements to // initialize letters in phrase to be guessed // & add them to the GuessPhrasePanel this.revalidate();// push changes through }
  • You can match special keys on the keyboard like enter or space using virtual key constants in the KeyEvent class, like KeyEvent.VK_ESCAPE.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.