954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hangman Program Help

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++;
		}
	}
}
techgeek420
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

techgeek420
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?

techgeek420
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

You need a counter to keep track of them.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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?

techgeek420
Newbie Poster
10 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: