good people at oracle
good people at oracle

Reputation: 31

Trouble implementing java hangman to return blanks with correct letters (e.g. "_ _ r _")

I've been working on a text-based hangman game in Java BlueJ for a school project and have been mostly successful, but I ran into a bit of a wall with this part.

I'm trying to make it so every time the player guesses a letter correctly, it prints out the blank spaces (asterisks) with the correct letters inserted. Right now, I have it printing out the letter you guessed and past correct guesses with the correct number of blanks. My only issue now is that it prints an incorrect number of blanks at the start of the game.

How can I fix this issue with the blank spaces?

import java.util.*;

public class Hangman {
    Word w = new Word();
    private String word = w.chooseWord();
    private int count = 0;
    String guess;

public String getWord() {
    String w = word;
    return w;
}

public int countLetters() {
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

public String Blanks() {
    countLetters();
    int num = 0;
    String spaces = "";
    String blankSpace = "*";
    while (num < count) {
        spaces = spaces + blankSpace;
        num++;
    }
    return spaces;
}

String wordSoFar = Blanks();

public int countOccurrences() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            count++;
        }
    }
    return count;
}

public String wordSoFar() {
    for (int i = 0; i < word.length(); i++) {
        if (word.substring(i, i + 1).equals(guess)) {
            wordSoFar = wordSoFar.substring(0, i) + guess + wordSoFar.substring(i + 1 , wordSoFar.length());
        }
    }
    return wordSoFar;
}  

public void Guess() {
    //Removed code that draws hangman due to it making this really long

    boolean correct = false;
    Scanner scan = new Scanner(System.in);
    int numIncorrectGuesses = 0;
    int numCorrectGuesses = 0;

    while (numCorrectGuesses != word.length() && numIncorrectGuesses < 6) {  
        guess = scan.next().substring(0,1);

        if (word.contains(guess)) {
            correct = true;
            numCorrectGuesses += countOccurrences();
            System.out.println(wordSoFar());
        } else {
            correct = false;
            numIncorrectGuesses++;
        }

    //Removed code that draws hangman due to it making this really long

    if (numCorrectGuesses == word.length()) {
        System.out.println("You win");
    } else {
        System.out.println("You lose");
    }
}

Adding driver class:

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.Blanks());
        game.Guess();
    }
}

Class that selects word:

import java.util.*;

    public class Word {
        //String[] bank = {removing word bank due to length};

        public String chooseWord() {
            Random r = new Random();
            return new String(bank[r.nextInt(bank.length)]);
        }
    }

Here's an example of the craziness that happens (the first line is just for testing; final game won't show the word. The single character lines are my guesses):

object
************
    _______
   |/      |
   |
   |
   |
   |
   |
___|___
o
o*****
b
ob****
j
obj***
e
obje**
c
objec*
t
object
You win

Upvotes: 2

Views: 1164

Answers (2)

sabbir
sabbir

Reputation: 438

you have to remove the line

wordSoFar = Blanks();

from the beginning of wordSoFar() function. Instead do it at the beginning of guess() function. You are initializing it with blank every time you guess a new character. And every time you call Blank() function, it increase count (as it is a class variable), which eventually increases number of "_".

Now for your current double * problem, write a getter for wordSoFar in Hangman class.

public String getWordSoFar() {
    return wordSoFar
}

And in main function

public class runGame {
    public static void main(String[]args) {
        Hangman game = new Hangman();
        System.out.println(game.getWord());
        System.out.println(game.getWordSoFar());
        game.Guess();
    }
}

And another think, countLetter() function should initialize the count to 0 before counting.

public int countLetters() {
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
        if (Character.isLetter(word.charAt(i))) {
            count++;
        }
    }
    return count;
}

Upvotes: 2

Teddy Koker
Teddy Koker

Reputation: 984

Instead of using a String, you could can convert it to a array of chars

char[] wordArray = word.toCharArray();
char[] blankArray = new char[wordArray.length];
Arrays.fill(blankArray,'_');

Guess should be a character since the user should only be able to guess one letter

Then for this

public String wordSoFar(){
    for(int i = 0; i < wordArray.length(); i++){
        if(wordArray[i] == guess){
            blankArray[i] = wordArray[i];
        }
    }
    return blankArray;
}

The blankArray keeps on getting pieced together. You can test if its solved by

if(String(blankArray)==String(wordArray)){}

Upvotes: 0

Related Questions