Results 1 to 5 of 5
  1. #1
    9tjh is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default Hangman Game Help Please

    Hi Java Forums
    I am stuck on my hangman game project, I don't know where to start with my last four methods
    Java Code:
    import java.util.*;
    import java.awt.*;
    import java.applet.*;
    import java.io.*;
    import java.net.*;
    
    public class Hangman() {
    	
    	final int maxTries = 5;
    	final int maxWordLen = 20;
    	char secretWord[];
    	int secretWordLen;
    	char wrongLetters[];
    	int wrongLettersCount;
    	char word[];
    	int wordLen;
    	
    }
    	public Hangman() {
    		wrongLettersCount = 0;
    		wrongLetters = new char[maxTries];
    		secretWordLen = 0;
    		secretWord = new char[maxWordLen];
    		word = new char[maxWordLen];
    		
    		processFile();
    		String category = "";
    		String phrase = "";
    		
    		Random generator = new Random();
    		int rand = generator.nextInt();
    	}
    	
    	public Hangman(int) {
    		Hangman();
    		rand.setSeed(0);
    	}
    
    	private void processFile() {
    		Scanner console = new Scanner(System.in);
    		Scanner input = new Scanner(clues);
    		while (input == clues) {
    			String name = console.nextLine();
    			try {
    				input = new Scanner(new File(name));
    				} catach (FileNotFoundException e) {
    					System.out.println("File not found." + "Please try again.")
    				}
    			}
    			System.exit(1);
    		}
    	}
    	private Clue processLine(String line) {
    		Scanner lineScanner = new Scanner(line);
    		lineScanner.useDelimiter(":");
    		lineScanner.next();
    	}
    	public void newGame() {
    		int i;
    		String s = wordlist[(int)Math.floor(Math.random()*wordlist.length)];
    		secretWordlLen = Math.min(s.length(), maxWordLen);
    		
    		for (i = 0; i < secretWordLen; i++) {
    			secretWord[i] = s.charAt(i);
    		}
    		
    		for (i = 0; i < maxWordLen; i++) {
    			word[i] = 0;
    		}
    		
    		wordLen = 0;
    		for (i=0; i < maxTries; i++) {
    			wrongLetters[i] = 0;
    		}
    		
    		wrongLettersCount = 0;
    		
    	}
    	private void generateClue() {
    		Scanner fileScan = new Scanner(new FileInputStream("clues.txt"));
    		String secretWord = fileScan.next();
    		StringBuffer word = new StringBuffer();
    		for (int i = 0; i <= secretWord.length(); i++)
    			word.append("_");
    		System.out.println(word);
    	}
    	public boolean isCorrectGuess(char c) {
    		for (i = 0; i < secretWordLen; i++) {
    			if (key == word[i]) {
    				found = true;
    				return true;
    			}
    		}
    	
    	/**
    	 *Check if letter is in secret word? if so, add it
    	 */
    		if (!found) {
    			for (i=0; i < secretWordLen; i++) {
    				if (key ==secretWord[i]) {
    					word[i] = (char)key;
    					wordLen++;
    					found = true;
    				}
    			}
    		}
    	/**
    	 *Check if already in wrong letters
    	 */
    		if(!found) {
    			for (i=0; i < maxTries; i++) {
    				if (key == wrongLetters[i]) {
    					found = true;
    					return true;
    				}
    			}
    		/**
    		 *If wrong letter; add to wrong letters
    		 */
    		 if (wrongLettersCount < wrongLetters.length) {
    		 	wrongLetters[wrongLettersCount++] = (char)key;
    		 	if (wrongLettersCount < maxTries) {
    		 		return false;
    		 	}
    		 	else {
    		 		for (i=0; i < secretWordLen; i++) {
    		 			word[i] = secretWord[i];
    		 		}
    		 }
    		 return true
    		
    		}
    		
    	}
    	public boolean isGameOver() {
    		if (secretWordLen == wordLen || wrongLettersCount == maxTries) {
    			newGame ();
    			return true;
    		}
    		return false
    	}		
    	}
    		
    		
    	public boolean isComplete() {
    		if (secretWord== word || wrongLettersCount < maxTries) {
    			return true
    		}
    		return false
    	}
    	}
    	public String getVisiblePhrase() {
    		
    	}
    	public String getCurrentCategory() {
    		
    	}
    	public String getCurrentPhrase() {
    		
    	}
    The isComplete() method determines if the game has ended by the user completing the phrase by guessing all the correct letters needed to fill in the clue. If the player has won, this method returns true. If the player has not yet won, this method returns false.
    The getVisiblePhrase() method simply returns the phrase the player is currently trying to guess. Spaces should be added between each character in the phrase so that the number of blank spaces is easily seen in the GUI. If you don't add the spaces, the underscores used to as a letter placeholder will run together.
    The getCurrentCategory method returns the category of the current Clue object being used in the Hangman game.
    The getCurrentPhrase method returns the phrase of the current Clue object being used in the Hangman game.

    Your time much appreciated
    Last edited by 9tjh; 12-04-2009 at 03:26 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    1) Welcome to the forum.
    2) Please use code tags so that your code will be readable. If you do this, more will take the time to read your code increasing your chances of getting help. To see how to do this, please read my signature below. Note that you can edit your post above by simply adding the code tags to the code.
    3) Please go into more detail on what is tripping you up. Showing us your attempts at creating these methods will go a long way to letting us know what you do or don't understand.

    Much luck

  3. #3
    9tjh is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the welcome, to be honest I don't even know if I'm going in the right direction with this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    OK, I can now read your code. Thanks. First the isComplete method. What you have is this:
    Java Code:
    	public boolean isComplete() {
    		System.out.println("Congratua")
    	}
    This won't compile since the method signature (the first line of the method -- public boolean isComplete() ) states that it will return a boolean value, and the method body doesn't do this.

    What you need to do inside this method is exactly what the instructions tell you to do: check to see if the user has solved the puzzle and if so return true, if not return false. This method shouldn't have any calls to System.out.println or anything similar.

    For my money, I'd check the letters (Strings) in the guessedLetters StringBuffer and see if it contains all the letters in the phrase. If so, return true, if not false.

    Similar for your other methods. They are not for calling System.out.println but rather for doing what your instructions tell you must be done, and they all need to return what the method signature states must be returned.

    Much luck!

  5. #5
    9tjh is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Need Some Help, Simple Loop For Hangman Game
    By Juo in forum New To Java
    Replies: 2
    Last Post: 11-14-2009, 07:51 PM
  2. Hangman Game..
    By iPetey in forum New To Java
    Replies: 4
    Last Post: 05-07-2009, 02:24 PM
  3. Need help with hangman game
    By kurt in forum New To Java
    Replies: 4
    Last Post: 04-25-2009, 06:47 PM
  4. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 01:56 PM
  5. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:16 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
  •