Results 1 to 6 of 6
Thread: Hangman Assignment
- 01-30-2012, 07:21 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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; } }
- 01-30-2012, 07:51 PM #2
Re: Hangman Assignment
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.what needs to be done
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.
- 01-31-2012, 05:47 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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?
if no underscoresJava Code:return true;
if finds an underscoreJava Code:return false;
?
- 01-31-2012, 06:47 AM #4
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
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; } } }
- 01-31-2012, 06:58 AM #5
Member
- Join Date
- Jan 2012
- Posts
- 40
- Rep Power
- 0
Re: Hangman Assignment
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.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; }
- 01-31-2012, 01:01 PM #6
Re: Hangman Assignment
You should test the complete method to see if it works.my attempt at the complete method.. how does it look?
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
-
Hangman
By Feriscool in forum New To JavaReplies: 23Last Post: 05-17-2011, 06:54 PM -
hangman
By coltragon in forum New To JavaReplies: 2Last Post: 01-16-2010, 09:56 AM -
hangman
By javaMike in forum Advanced JavaReplies: 2Last Post: 11-14-2009, 09:06 AM -
Hangman GUI help
By kurt in forum New To JavaReplies: 5Last Post: 05-22-2009, 10:22 AM -
Hangman Help!!!
By chinasome in forum New To JavaReplies: 5Last Post: 11-08-2008, 02:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks