Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2016
    Posts
    4
    Rep Power
    0

    Default Need help with a java project

    I'm working on a hangman project for my first java class and the game always results in a win regardless of input. Any help would be greatly appreciated.

    Here is my code:
    Java Code:
    package kyle;
    
    import java.util.Scanner;
    import java.util.Random;
    
    public class Kyle {
    	String[] wordArray = { "dog", "cow", "egg", "house", "cat" };
    	String[] hintArray = { "Bark", "Moooo", "Chickens Lay These", "Where you live", "Meow" };
    	int gameNumber;
    	char input;
    
    	public static void main(String[] args) {
    		Scanner console = new Scanner(System.in);
    		Random r = new Random();
    		Kyle k = new Kyle();
    		k.gameNumber = r.nextInt(4 + 1);
    		boolean weArePlaying = true;
    
    		while (weArePlaying) {
    			System.out.println("Welcome to Hangman!");
    			System.out.println(k.hintArray[k.gameNumber]);
    			char[] wordToGuess = k.wordArray[gameNumber(k.wordArray.length)].toCharArray();
    			int amountOfGuesses = (wordToGuess.length);
    			char[] playerGuesses = new char[amountOfGuesses];
    
    			boolean isSolved = false;
    			int guesses = 0;
    
    			while (!isSolved && guesses != amountOfGuesses) {
    				System.out.println("Current guesses: ");
    				System.out.println(k.calculateMask());
    				printArray(playerGuesses);
    				System.out.printf("You have " + (amountOfGuesses - guesses) + " guesses left");
    				System.out.println("");
    				System.out.println("Enter a letter");
    				char input = console.nextLine().charAt(0);
    				guesses++;
    
    				if (input == '+') {
    					weArePlaying = false;
    					isSolved = true;
    				} else {
    					playerGuesses[guesses] = input;
    
    				}
    				if (isTheWordGuessed(playerGuesses)) {
    					isSolved = true;
    					System.out.println("You win!");
    				}
    
    			}
    			if (!isSolved)
    				System.out.println("You ran out of guesses");
    			System.out.println("Do you want to play another game? (y/n)");
    			String anotherGame = console.next();
    			if (anotherGame.equals("n"))
    				weArePlaying = false;
    			else if (anotherGame.equals("y"))
    				weArePlaying = true;
    		}
    
    	}
    
    	private static int gameNumber(int length) {
    
    		return 0;
    	}
    
    	public static void printArray(char[] array) {
    		for (int i = 0; i < array.length; i++) {
    			System.out.println(array[i] + " ");
    
    		}
    
    	}
    
    	public static boolean isTheWordGuessed(char[] wordToGuess) {
    		for (int i = 0; i < wordToGuess.length; i++) {
    			if (wordToGuess[i] == '*')
    				return false;
    		}
    		return true;
    	}
    
    	public String calculateMask() {
    		String mask = "";
    
    		String word = wordArray[gameNumber];
    		for (int i = 0; i < word.length(); i++) {
    			char c = word.charAt(i);
    			String s = String.valueOf(c);
    			boolean guessed = word.contains(String.valueOf(input));
    
    			if (guessed) {
    				mask += String.valueOf(word.length());
    			} else {
    				mask += "*";
    			}
    		}
    		return mask;
    	}
    }
    Last edited by TheIrishman112; 10-27-2016 at 07:17 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with a java project

    always results in a win regardless of input
    How are you trying to debug the code to see what it is doing that? Try printing out the values of the variables that are being tested to determine a winner so you can see what the code sees when it does the test for a winner.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Oct 2016
    Posts
    4
    Rep Power
    0

    Default Re: Need help with a java project

    To be honest I'm not really sure where to begin with that I haven't done much debugging before. This is what is printed in the console when I run the program.

    Welcome to Hangman!
    Bark
    ***
    Current guesses:
    You have 8 guesses left
    Enter a letter
    r
    You win!
    Do you want to play another game? (y/n)

    I'm not sure what the best way is to determine where the error is occurring. I'm using eclipse if that helps. Thanks for the help!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with a java project

    Start by adding a println statement in the method that is always returning true: isTheWordGuessed()
    Print out the contents of the char array that is test in that method to see what is in it.
    The Arrays class's toString() method is useful for that:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Oct 2016
    Posts
    4
    Rep Power
    0

    Default Re: Need help with a java project

    Okay so that was helpful, I now can see that the wordToGuess array is empty and therefore never contains the * which would make it false.
    Is there an easier way for me to define that an empty space in the array also will not suffice? I need the guesses and the characters of the word to match but I can't set them equivalent because there will be incorrect guesses as well.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with a java project

    The unfilled slots in a char array will have a 0 value (Not '0'). You could test for that.
    Where does the code insert the '*' that is being tested for into the array?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Oct 2016
    Posts
    4
    Rep Power
    0

    Default Re: Need help with a java project

    I see that I am not filling that array. What would be the best way to fill the array with '*'?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with a java project

    way to fill the array with '*'
    A loop since the array's length changes from word to word.
    Or look at the methods in the Arrays class. There is probably a method there that will do it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Convert Java Project (Old Style) to Maven Project
    By oliosien in forum Apache Maven
    Replies: 1
    Last Post: 03-23-2015, 03:22 PM
  2. Help me to my Project in JAVA OOP training project.
    By jrdn128 in forum New To Java
    Replies: 12
    Last Post: 12-15-2014, 09:53 AM
  3. Replies: 2
    Last Post: 06-26-2014, 07:43 PM
  4. Replies: 6
    Last Post: 06-20-2014, 04:01 PM
  5. Replies: 7
    Last Post: 11-27-2012, 10: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
  •