Hey everyone!
I am trying to writing a program that will play hangman. I have an understanding, on paper, of how to write the program... but I just cant fully convert it into java. I have started the program but I've gotten to a point where I'm stumped. Any help will be much appreciated!

here is what i have so far...

import java.util.Scanner;

class HangMan
{
	public static void main (String args[])
	{

		Scanner input = new Scanner(System.in);
		int correct = 0;
		int guesses = 0;
		int wrong = 0;
		System.out.print("Player 1 - Input the word to be guessed (do not use any capital letters): ");
		String word = input.next();
		String []Player1Word = new String[word.length()];
		for(int a = 0; a < word.length(); a++)
			Player1Word[a] = word.substring(a,a+1);
		String []Player2Guess = new String[word.length()];
		System.out.println("Player 2 - The word you are trying to guess has " + word.length() + " letters in it.");
		for (int k = 0; k < word.length(); k++)
		{
			System.out.print("__" + " ");
			if(k == word.length() - 1)
				System.out.println(" ");
		}
		while(correct<word.length() || wrong<=6)
		{
			System.out.print("Player 2- please guess a letter: ");
			String guess = input.next();
			Player2Guess[guesses] = guess;
			for(int g = 0; g < word.length(); g++)
			{
				if(Player2Guess[guesses] = Player1Word[g])
					correct++;
					int k = Player1Word.indexOf(guess);
					System.out.println("there is a "+guess);
					for (int b = 0; b < word.length(); b++)
					{
						if(b==k)
							System.out.print(guess+" ");
						System.out.print("__" + " ");
						if(b == word.length() - 1)
							System.out.println(" ");
					}
				else if
					wrong++;
			}
			guesses++;
		}
	}
}

Recommended Answers

All 7 Replies

I have an understanding, on paper, of how to write the program

Can you post a simple list of the items you are having problems with?
Then we'll work on them one by one.

Well right now Im confused on how to get it to print the correct guessed letter with the appropriated number of blanks around it. Also to get it to print the previous correct guessed letters.
...Once I understand that I can tell you the next thing im having a problem with

how to get it to print the correct guessed letter with the appropriated number of blanks around it.

Create two loops. The first prints out the blanks in front, the second the blanks after.
Print the letter between the two loops.
System.out.print(" ") will print a single space. Put it in a loop.

ok that actually makes alot of sense... I was thinking to hard on it.
Now how do I get it to remember the correct letters that have been guessed when i start the loop over again?

You need a counter to keep track of them.

cant I use the array to call in later on in the loop? Though I don't think I have the guess paired with the array correctly.

How would I call the letter from the array since its a loop?

If the letter is in an array, you could get at it by using an index to the array:
theArray[ix] // gets the letter in the array at index ix

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.