Results 1 to 6 of 6
  1. #1
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Hangman Assignment

    Hello there,
    I have been assigned a hangman assignment in which we just need to complete 4 methods within a precreated hangman java program in order to make it work. I have completed one of the required methods but am stuck on the others (complete, found, and tostring). I just need help conceptualizing the different methods and what needs to be done, or if someone can get me started and give an explanation that would also help a lot. I am at the phase where i am just staring at the code and can't seem to break out of it in order to get going. There are some prebuilt packages but nothing that should really get in the way of understanding some of it. I am brand new to java and havent coded anything in a good number of years.

    Java Code:
    import ljing.*;
    import java.util.*;
    import java.io.*;
    
    /** A word game. */
    public class Hangman extends Program {
    
    public static void main(String[] args) {
    new Hangman().run();
    }
    
    /** The set of legal words. */
    private String[] dictionary;
    
    /** Number of guesses left. */
    private int guesses;
    
    /**
    * known[i] is letter i of word if it has been discovered, an underscore
    * otherwise.
    */
    private char[] known;
    
    /** Returns true if known contains no underscores. */
    protected boolean complete() {
    // TODO You have to write this one
    return false;
    }
    
    /**
    * Returns true if letter occurs at least once in word. The corresponding
    * elements of known are set to letter.
    */
    protected boolean found(char letter, String word) {
    // TODO You have to write this one
    return false;
    }
    
    /** Returns a random word from the dictionary. */
    protected String randomWord() {
    // TODO You have to write this one. DONE-------------------
    Random rand = new Random();
    int i = rand.nextInt(dictionary.length);
    return dictionary[i];
    }
    
    /** Reads the dictionary. */
    protected void readDictionary() {
    printLine("Reading dictionary...");
    try {
    File file = new File("enable1.txt");
    // Count the words in the dictionary
    Scanner input = new Scanner(file);
    int i = 0;
    while (input.hasNextLine()) {
    String word = input.nextLine();
    if (word.equals(word.toLowerCase())) {
    i++;
    }
    }
    dictionary = new String[i];
    // Store the words in the dictionary
    input = new Scanner(file);
    i = 0;
    while (input.hasNextLine()) {
    String word = input.nextLine();
    if (word.equals(word.toLowerCase())) {
    dictionary[i] = word;
    i++;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    System.exit(1);
    }
    }
    
    /** Plays the game. */
    public void run() {
    readDictionary();
    guesses = 6;
    String word = randomWord();
    known = new char[word.length()];
    for (int i = 0; i < known.length; i++) {
    known[i] = '_';
    }
    while (guesses > 0) {
    printLine("\n" + toString());
    char letter = readLine("Your guess?").charAt(0);
    if (!found(letter, word)) {
    guesses--;
    }
    if (complete()) {
    printLine("\n" + toString());
    printLine("You win!");
    return;
    }
    }
    printLine("\nYou lose. The word was " + word + ".");
    }
    
    /**
    * Returns a String representing the state of the game, e.g.,
    * "5 guesses left: _ _ _ e _ _ _ _ ";
    */
    public String toString() {
    // TODO You have to write this one DOES NOT PRINT, just returns a string
    // stringresult="";
    //return result;
    printLine (+guesses+" Guesses left: ");
    return null;
    }
    
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Hangman Assignment

    what needs to be done
    You need to describe in some detail what you want a method to do. Will it return a value? What arguments will be passed to it.
    Pick one of the methods you need to write and describe what it is supposed to do. Make a list of the steps the method needs to do.
    Given a description of what a method is to do, then you can think about how to write the code to do it.

  3. #3
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Hangman Assignment

    The complete method:
    This method scans all of the known letters or discovered words in the known string and returns true if all of the letters do NOT contain underscores thus ending the word/game.
    So would i just write a simple scan while loop that looks through the known string and if statement for when it finds an underscore, and the else clause would return a true value? Am i understanding this correctly and is this how i would go about doing it?
    Java Code:
    return true;
    if no underscores
    Java Code:
    return false;
    if finds an underscore
    ?

  4. #4
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Hangman Assignment

    Here is my attempt at the complete method.. how does it look?
    Java Code:
    	/** Returns true if known contains no underscores. */
    	protected boolean complete() {
    		// TODO You have to write this one
    		for (int i = 0; i < (known.length); i++){
    			if (known[i] = '_'){
    				return false;
    			}
    			else {
    				return true;
    			}
    		}
    
    		
    	}

  5. #5
    Lucid15 is offline Member
    Join Date
    Jan 2012
    Posts
    40
    Rep Power
    0

    Default Re: Hangman Assignment

    Java Code:
    	/**
    	 * Returns true if letter occurs at least once in word. The corresponding
    	 * elements of known are set to letter.
    	 */
    	protected boolean found(char letter, String word) {
    		// TODO You have to write this one
    		return false;
    	}
    So i am gonna want this method to take the persons guess letter and scan the chosen random word in the dictionary for that letter. If found return true, else return false.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,401
    Rep Power
    25

    Default Re: Hangman Assignment

    my attempt at the complete method.. how does it look?
    You should test the complete method to see if it works.
    Set the known array to a String that should be complete and call the method and see what it returns.
    Then set the array to a String that is not complete and do the same. Try several combinations of Strings for the not complete test.

Similar Threads

  1. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 06:54 PM
  2. hangman
    By coltragon in forum New To Java
    Replies: 2
    Last Post: 01-16-2010, 09:56 AM
  3. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  4. Hangman GUI help
    By kurt in forum New To Java
    Replies: 5
    Last Post: 05-22-2009, 10:22 AM
  5. Hangman Help!!!
    By chinasome in forum New To Java
    Replies: 5
    Last Post: 11-08-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •