Question: Objective: Develop a Java Hangman game using the class MyArrayList. The program should read a list of words from a text file, sort the words

Objective:
Develop a Java Hangman game using the class MyArrayList. The program should read a list of words from a text file, sort the words alphabetically, and then allow the user to play the game with one of the words chosen at random.
Instructions:
1. Use the provided text file named `words.txt`.
2. Use the provided `MyArrayListHM` class to implement the generic MyArrayList functionalities.
3. Implement the Hangman game logic using the `MyHangman` class.
4. Write the 3 methods in the `MyHangman` class:
1. Write the method loadWords() to read the words from the words.txt file, store them in the words arraylist and display the words. Use the sortlist() method of the `MyArrayListHM` class to sort the list and display the sorted list of words.
2. Write the method startNewGame() to initialize a new game of Hangman. This method should randomly select a word from the list of words, set up the secret word and guessed word arrays, and initialize the number of guesses left.
3. Write the method playGame() that handles user input, updates the game state, and provides feedback to the player. The method manages the gameplay loop to continue the game as long as there are guesses left and the word has not been completely guessed. (Hint Use the updateGuessedWord() and isWordGuessed() methods discussed below)
5. Write a method updateGuessedWord() in the MyHangman class. The method receives the correct guessed letter as parameter and updates the guessed word with the correct guessed letter. (Hint - Use the remove and the add methods of the MyArrayListHM class to replace the _ character with the correct guessed letter).
6. Write a method isWordGuessed() in the MyArrayListHM class to check if the entire word has been correctly guessed by the player. The method should return a boolean value indicating whether the word has been correctly guessed. (Hint - Use the contains method of the MyArrayListHM class to check if the guessed word still contains an underscore character '_')
Example Output:
The words: [Apple, Bread, Chair, Earth, House, Italy, Juice, Knife, Lemon, Mouse, Snake, Table, Venus, Zebra, Eagle, Horse, Onion, Pizza, Robin, Tiger]
The sorted words: [Apple, Bread, Chair, Eagle, Earth, Horse, House, Italy, Juice, Knife, Lemon, Mouse, Onion, Pizza, Robin, Snake, Table, Tiger, Venus, Zebra]
Welcome to Hangman!
Guesses left: 6
Enter a letter: r
Incorrect guess.
Guessed word: [_, E,_,_,_]
Guesses left: 5
Enter a letter: o
Correct guess!
Guessed word: [_, E,_, O,_]
Guesses left: 5
Enter a letter: s
Incorrect guess.
Guessed word: [_, E,_, O,_]
Guesses left: 4
Enter a letter: p
Incorrect guess.
Guessed word: [_, E,_, O,_]
Guesses left: 3
Enter a letter: m
Correct guess!
Guessed word: [_, E, M, O,_]
Guesses left: 3
Enter a letter: l
Correct guess!
Guessed word: [L, E, M, O,_]
Guesses left: 3
Enter a letter: n
Correct guess!
Congratulations! You guessed the word: [L, E, M, O, N]
Instructions to Complete the Assignment:
Use the two Java files: `MyArrayListHM.java` and `MyHangman.java`and complete the code.
I have no idea where to begin.provided code:
public class MyArrayListHM
{
private int size; // Number of elements in the list
private E[] data;
private int MAXELEMENTS =100;
/** Create an empty list */
public MyArrayListHM(){
data =(E[])new Object[MAXELEMENTS];// cannot create array of generics
size =0; // Number of elements in the list
}
public boolean isFull()
{
if (size size)
throw new IndexOutOfBoundsException
("Index: "+ index +", Size: "+ size);
// Move the elements to the right after the specified index
for (int i = size -1; i >= index; i--)
{
data[i +1]= data[i];
}
// Insert new element to data[index]
data[index]= e;
// Increase size by 1
size++;
}
public boolean contains(Object e){
for (int i =0; i < size; i++)
if (e.equals(data[i])) return true;
return false;
}
public E get(int index){
if (index <0|| index >= size)
throw new IndexOutOfBoundsException
("Index: "+ index +", Size: "+ size);
return data[index];
}
public E remove(int index){
if (index <0|| index >= size)
throw new IndexOutOfBoundsException
("Index: "+ index +", Size: "+ size);
E e = data[index];
// Shift data to the left
for (int j = index; j < size -1; j++)
data[j]= data[j +1];
data[size -1]= null; // This element is now null
// Decrement size
size--;
return e;
}
public void clear()
{
size =0;
}
public String toString()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!