Ok, Iam now doing my last assignment for my class. Thank god. lol Well I have to make a problem that will write an interactive Java program that allows the user to play the game of Hangman with words that are given to me from a file.

Here is what I have to do,

Write an interactive Java program that allows the user to play the game of Hangman.

To begin, create a new project folder in your workspace folder.
Create your class file hangman.java inside your project folder. 
Download this text file (dictionary.txt), and place it in the root of your project folder.
(To download the file, click your right mouse button on the link, and choose "Save Target As" from the menu that appears)

You can also download a working copy of the program, which will allow you to play the game so you can see how it works, and so you can see what will be expected of your program. Basically, your JAVA program should perform exactly the same as the demo copy you will be downloading.
Download the program here (hangman.exe).

Note that this working demo is a Windows executable file. It will not work on Mac, UNIX, or any other operating system. If you are using one of these other operating systems, you will need to put it on a Windows based machine to see it working.

Make sure to place the downloaded program in the same folder with the dictionary.txt file.
Once downloaded, you can use Windows Explorer or My Computer to open the project folder. Simply double-click your mouse on the hangman.exe file to run the program.

 

Program Notes
The program will begin by reading the contents of a file called dictionary.txt. This is the file you are downloading from the link above.
The file contains a list of words that could possibly be chosen throughout the course of the game. This file contains one word per line, and will not contain more than 15,000 words. No word will contain more than 20 characters. You need to read the data from this file and store it into a partially-filled array. If the file cannot be opened, your program should display and error message stating that the dictionary file cannot be read, and then immediately exit since the program cannot continue without having words from which to choose.

After having read the dictionary into your program, your program will need to display the basic directions on the screen. Use the directions that are given in my demo program. After displaying the directions, you should ask the user to press ENTER to continue. This will give the user the chance to read the directions before they are removed from the screen.

Next, your program will need to randomly choose a word from the list of words stored in your array. Afterwards, you should clear the screen and display a series of asterisks to represent the letters to be guessed. You will display one asterisk for each letter in the chosen word. When displaying the asterisks, make sure you place a space between each asterisk to make it easier to read. See the technical notes below for information about clearing the screen.

Two lines below the asterisks, you should show the user the number of guesses left. The game begins with 8 guesses.

Next, you should allow the user to enter a letter. The user should be able to enter upper or lowercase letters. Compare the letter against the ones in the solution. If the letter is in the solution, you should replace the appropriate asterisk(s) with the guessed letter and then redraw the screen (clear the screen, then rewrite the information back to the screen). If the guessed letter is not in the solution, you should decrement the number of guesses left and redraw the screen. 

If the user enters the number "9" instead of a letter, your program should immediately exit.

The program should keep a list of the incorrect letters guessed and display them on the screen. Again, make sure to print a space between each letter when displaying them to make them easier to read.

If the user correctly guessed all of the letters, you should display a message stating that the user has won.
If the user makes 8 incorrect guesses, you should display a message stating that the user has lost the game.

Ask if the user wants to play again. If so, you will randomly select another word and start the game again.



Input
In order to receive any credit for this program, your program MUST follow this input specification EXACTLY. 

The first input you get from the user should be the ENTER key. This is done after displaying the instructions.


The second thing you will get from the user is a character. This is a letter that the player is guessing.
The user can enter any character on the keyboard, then press the ENTER key.
Also note, that it is possible that the user might try to press the ENTER key without entering any letters. Make sure this does not cause a problem in your program.


The user will continue entering letters until: 
They type "9" and press enter, in which case the program ends 
They make a total of 8 incorrect guesses, in which case the game ends 
They correctly guess all of the letters in the puzzle, in which case the game ends


After the game ends, the user will press enter "Y" (upper or lowercase) to indicate that they wish to play again. Any other key entered should cause the program to end. 
 

Output
The program's output consists of the following, in this order:

The program first displays the instructions on the screen, followed by a message telling the user to press enter to continue.


The program next displays a series of asterisks on the screen, one asterisk per letter in the secret word. There should be a space printed between each asterisk.


Under the asterisks, the program displays 
the number of guesses remaining 
a list of the incorrect letters guessed by the user (display UPPERCASE only), with a space between each one 
a message asking the user to enter a letter to guess, or 9 to exit the program


Once a guess is made the screen is cleared, and #2 and #3 (above) are redisplayed. If the guessed letter is in the word, one or more of the asterisks will be replaced with the correctly guessed letter. Display only uppercase letters.


When the game ends the program displays a message stating whether the user won or lost the game. If the game is lost, the program should also show the user the solution to the puzzle.


Next the program will display the total number of games won and total number of games lost.


Finally the program will display a message asking if the user wants to play again. Make sure to let them know that they should answer "Y" to go again.

Th find the file of words, it has a just like 15,000 words in the file. Tell me if you need to see the file.

Ok, my question is how do I put the given words in my array?

This is what I have so far,

import java.io.*;
import java.util.*;

public class Hangman {

	
	public static void main(String[] args) {
		
	Scanner kbd = new Scanner(System.in);
	
	String enter;
	Scanner inStream = null;	
	String filename = "dictionary.txt";
	
	System.out.print("This is a word guessing game. A word will be selected at random and kept hidden." + '\n' + "You will try to figure out the secret word by guessing letters which you think are in the" + '\n' + "word. You will guess one letter at a time. If the letter you guess is correct, the" + '\n' + "position(s) of the letter in the secret word will be shown. You will be allowed 8 wrong" + '\n' + "guesses. If you gues incorrectly 8 times, you lose the game. If you guess all of the" + '\n' + "letters in the word, you win." + '\n');
		
	System.out.println('\n' + "Press enter to continue");
	enter = kbd.next();
	
	try 
	{
		inStream = new Scanner (new File ("dictionary.txt"));
	}
		catch (Exception e)
		{
			System.out.println("Error opening the file " + "dictionary.txt");
			System.exit(0);
		}
		
	while (inStream.hasNextLine())	
	{
		String line = inStream.nextLine();
		System.out.println(line);
	}
	inStream.close();	
		
	final int WORDS_LENGTH = 15000;
	
	double [] words = new double [WORDS_LENGTH];
	
	

	}

}

Edited 5 Years Ago by DallasFan3: n/a

You have a while loop starting at line 30 that gets input from the file. Add each to your array as you read it. You might want to declare your array at the top of the class, since as you have it now it doesn't exist when you need it.

You have a while loop starting at line 30 that gets input from the file. Add each to your array as you read it. You might want to declare your array at the top of the class, since as you have it now it doesn't exist when you need it.

What do you mean by "Add each to your array as you read it".?

As you go through this loop:

while (inStream.hasNextLine())	
	{
		String line = inStream.nextLine();
		System.out.println(line);
	}

instead of printing each line to the console as it's read, put it into an array. You'll have to keep track of where you are in the array, so start a counter at zero and use it as the index to the array, incrementing it each time you go through.

But isnt this an array?

final int WORDS_LENGTH = 15000; 	double [] words = new double [WORDS_LENGTH];

It is. It doesn't have anything in it, though, and it's the wrong type. This just tells the machine that you want 15,000 contiguous spaces in memory for storing double-precision floating-point numbers. You want an array of Strings, presumably, since you're talking about words, and you need to populate the array for it to do you any good.

Ok, like this?

String [] words = new String [WORDS_LENGTH];

So this aray will put all the words in the program?

No. No, no, no. Again, no.

That will reserve space in memory for WORDS_LENGTH number of Strings. If you don't put anything in the array, it won't have anything in it.

Yea, ok I figured it out. Ok, now I have to clear the screen and display a series of asterisks to represent the letters to be guessed. You will display one asterisk for each letter in the chosen word. Two lines below the asterisks, you should show the user the number of guesses left. The game begins with 8 guesses.

Next, I have to allow the user to enter a letter. The user should be able to enter upper or lowercase letters. Compare the letter against the ones in the solution. If the letter is in the solution, you should replace the appropriate asterisk(s) with the guessed letter and then redraw the screen (clear the screen, then rewrite the information back to the screen). If the guessed letter is not in the solution, you should decrement the number of guesses left and redraw the screen.

Any help would be thankful

This thread has been solved, so I will not be posting any further in it.

Start a new thread so people who might have an answer for you will find your question. And when you do, try to show a little more intitiative than just posting the problem as it's been assigned. Make an effort to solve it first, then ask for help.

This article has been dead for over six months. Start a new discussion instead.