I need to write the code for Hangman. I have written what I could but can't figure out rest of the code. Hopefully someone can help me out. I have two different classes: Hangman & Dictionary.
Printable View
I need to write the code for Hangman. I have written what I could but can't figure out rest of the code. Hopefully someone can help me out. I have two different classes: Hangman & Dictionary.
I think your plead for help is somewhat vague because first of all, a lot of people will avoid opening attached files, we'd rather see you use the Java highlight tags to put your code inline in the post.
Second, the issue at hand you seem to have is that SOMETHING is wrong or missing and I don't think people will like spending their time helping you by first having to try and figure out what your program does and what is actually missing.
Could you please be more specific about what area of your Hangman program you are having a problem with, trying to isolate each question to a smaller bit of the code?
Thanks!
Thanks for replying. Here is what I have so far.
The problem I'm having is it doesn't loop correctly.
PHP Code:// This is Hangman.java
import java.util.Scanner;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public class Hangman extends Dictionary
{
public static void main(String[] args)
{
// Create a scanner object named sc
Scanner sc = new Scanner (System.in);
//Continue the game untill user chooses No
String choice = "y";
while (choice.equalsIgnoreCase( "y"))
{
//System.out.println ("Welcome to game Hangman ");
// To display hangman message
JOptionPane.showMessageDialog(null, "Welcome to game Hangman");
System.out.println ();
System.out.print ("Please enter your name: ");
sc = new Scanner (System.in);
String name = sc.nextLine();
// Getting today's date
Date now = new Date ();
System.out.println ("Date: " + DateFormat.getDateInstance(DateFormat.SHORT).format (now));
System.out.println (name + ", purpose of this game is to guess the secret word.");
System.out.println ("You have six guesses remaining.");
System.out.println ("Good luck!");
System.out.println (" ");
Dictionary myDictionary = new Dictionary();
String secretWord = myDictionary.getRandomWord();
int emptyField = secretWord.length();
String blanks = "";
for(int i = 0; i < emptyField; i++)
{
blanks = blanks + " _ ";
}
System.out.println (blanks);
System.out.println (" ");
drawFigure(0);
int badGuessesRemaining = 6;
String word = secretWord;
String[] secretWordArray = new String [word.length()];
String[] displayWord = new String [word.length()];
String[] badGuesses = new String [badGuessesRemaining];
while(badGuessesRemaining != 0)
{
System.out.print ("Please enter your guess: ");
String guess = sc.nextLine();
for (int i=0; i<displayWord.length;i++)
{
displayWord[i] = "_";
}
//save word as array
for (int i=0; i<word.length(); i++)
{
secretWordArray[i] = word.substring(i,i+1);
}
//check for match
boolean match = false;
for (int i=0; i < secretWordArray.length; i++)
{
if (secretWordArray[i].equals(guess))
{
displayWord[i] = secretWordArray[i];
match = true;
}
}
if (!match)
{
badGuesses[badGuessesRemaining - 1] = guess;
badGuessesRemaining--;
}
//show display word
for (int i=0; i<displayWord.length;i++)
{
System.out.print(displayWord[i] + " ");
}
System.out.print("Do you wish to play this game again? (y/n): ");
choice = sc.next();
System.out.println();
}
}
//System.out.println("The secret word is " + secretWordArray + " and it has these amount of characters. " );
}
static void drawFigure( int badGuesses){
if (badGuesses == 0)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | ");
System.out.println(" |");
System.out.println(" |");
System.out.println("_|_");
}
else if (badGuesses == 1)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" |");
System.out.println(" |");
System.out.println("_|_");
}
else if (badGuesses == 2)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" | -");
System.out.println(" |");
System.out.println("_|_");
}
else if (badGuesses == 3)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" | - +");
System.out.println(" |");
System.out.println("_|_");
}
else if (badGuesses == 4)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" | - + -");
System.out.println(" |");
System.out.println("_|_");
}
else if (badGuesses == 5)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" |");
System.out.println(" | /");
System.out.println("_|_");
}
else if (badGuesses == 6)
{
System.out.println(" ___");
System.out.println(" | |");
System.out.println(" | 0");
System.out.println(" |");
System.out.println(" | / '\'");
System.out.println("_|_");
}}}
/*
for (int = 0; i < incorrect.length; i++)
{
System.out.print (incorrect [i] + " ");
}
*/
PHP Code:// This is Dictionary.java
import java.util.Random;
public class Dictionary
{
private static String[] words = new String [22];
Dictionary()
{
words [0] = "dictionary";
words [1] = "restaurant";
words [2] = "television";
words [3] = "responsible";
words [4] = "technology";
words [5] = "computer";
words [6] = "communicate";
words [7] = "automibile";
words [8] = "coffee";
words [9] = "federation";
words [10] = "exaggerate";
words [11] = "cappuccino";
words [12] = "forensics";
words [13] = "laboratory";
words [14] = "lamborghini";
words [15] = "ferrari";
words [16] = "abandoned";
words [17] = "residents";
words [18] = "california";
words [19] = "undergraguate";
words [20] = "hangman";
words [21] = "reference";
}
String getRandomWord()
{
Random rnd = new Random ();
int n = rnd.nextInt (words.length);
return words [n];
}}
Please explain.Quote:
it doesn't loop correctly
For example execute the code, copy the output and paste here and explain what is wrong with the output, show what the correct output would be and explain how it needs to work.