Logic goes awry in the program running the executioner (Java)

So here's another hangman question to add to the library here. My entities and boundary classes are almost entirely except for a method called opensLetter (), which replaces spaces with a correctly guessed letter. It also counts the number of correctly guessed letters (if any) and returns that integer to the driver to determine if he misses or hits. The showLetter () function will return zero if the user enters the wrong letter or returns the number of correct letters to determine the correct letter. My problem is that opensLetter () always returns zero despite filling in the correct letter. I chose several souts to isolate what is happening and it seems that the counter is set to zero after exiting the for loop. I am still learning Java, so there is a good chance this is something simple,but at the moment it seems difficult to me. Here is the driver:

package hangman;

import java.util.Scanner;

public class Hangman {

public static int NUMBER_MISSES = 5;

public static void main(String[] args) {

    String guessedLetter;
    WordHider hider = new WordHider();
    Dictionary dictionary = new Dictionary();

    Scanner Keyboard = new Scanner(System.in);
    hider.setHiddenWord(dictionary.getRandomWord());
    System.out.println(hider.getHiddenWord().length());
    System.out.println(hider.getHiddenWord());

    do {
        hider.wordFound();
        System.out.printf(hider.getPartiallyFoundWord() + "   Chances Remaing: %d \nMake a guess: ", NUMBER_MISSES);
        guessedLetter = Keyboard.nextLine();
        hider.revealLetter(guessedLetter.toLowerCase());
        if (hider.revealLetter(guessedLetter)== 0) {
            NUMBER_MISSES--;
            if (NUMBER_MISSES == 4) {
                System.out.println("Swing and a miss!");
            }
            else if (NUMBER_MISSES == 3) {
                System.out.println("Yup. That. Is. A. Miss.");
            }
            else if (NUMBER_MISSES == 2) {
                System.out.println("MISS! They say third time is a charm.");
            }
            else if (NUMBER_MISSES == 1) {
                System.out.println("Ouch. One guess left, think carefully.");
            }              
        } else {
            System.out.println("That a hit!");
        }
        if (hider.wordFound() == true) {
          NUMBER_MISSES = 0;
        }
    } while (NUMBER_MISSES > 0);

    if ((NUMBER_MISSES == 0) && (hider.wordFound() == false)) {
        System.out.println("Critical Failure. The word was " + hider.getHiddenWord() + " try harder next time and you'll win.");
    } else if ((NUMBER_MISSES == 0) && (hider.wordFound() == true)) {
        System.out.println(hider.getHiddenWord() + "\nBingo! You win!");
    }

}

      

}

This is a class that stores words from .txt into an array and generates a random word:

package hangman;

import java.util.Random;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Dictionary {

//Random randomizer = new Random();
private static String randomWord;
String[] dictionary = new String[81452];
private static String FILE_NAME = "dictionarycleaned.txt";

Dictionary() {
    int words = 0;
    Scanner infile = null;
    try {
        infile = new Scanner(new File(FILE_NAME));
        while (infile.hasNext()) {
            dictionary[words] = infile.nextLine();
            words++;

        }
        //System.out.println(dictionary[81451]);
    } catch (FileNotFoundException e) {
        System.err.println("Error opening the file " + FILE_NAME);
        System.exit(1);
    }

}

public String getRandomWord(){
  //randomWord = (dictionary[randomizer.nextInt(dictionary.length)]);  //Are either of these techniques better than the other?
  randomWord = (dictionary[new Random().nextInt(dictionary.length)]);
  return randomWord;    
}

      

}

And this is the class that contains opensLetter (), it also handles the random word:

package hangman;

public class WordHider {

private static String hiddenWord;
private static String partiallyFoundWord;

WordHider() {

    hiddenWord = "";
    partiallyFoundWord = "";

}

public String getHiddenWord() {
    return hiddenWord;
}

public String getPartiallyFoundWord() {

    return partiallyFoundWord;

}

public void setHiddenWord(String newHiddenWord) {
    int charCount;
    hiddenWord = newHiddenWord;
    for (charCount = 0; charCount < hiddenWord.length(); charCount++) {
        partiallyFoundWord += "*";
    }

}

public int revealLetter(String letter) {
    int correctChars = 0;

    if (letter.length() < 1 || letter.length() > 1) {
        correctChars = 0;
        return correctChars;
    } else {

        String tempString = "";

        for (int i = 0; i < hiddenWord.length(); i++) {
            if ((letter.charAt(0) == hiddenWord.charAt(i)) && (partiallyFoundWord.charAt(i) == '*')) {                   
                correctChars++;
                tempString += Character.toString(hiddenWord.charAt(i));

            } else {
                tempString += partiallyFoundWord.charAt(i);



            }

        }
        partiallyFoundWord = tempString;           
    }

    return correctChars;
}

public boolean wordFound() {
    boolean won = false;
    if (partiallyFoundWord.contains(hiddenWord)) {
        won = true;
    }
    return won;
}

public void hideWord() {
    for (int i = 0; i < hiddenWord.length(); i++) {
        partiallyFoundWord += "*";
    }

}

      

}

It's also worth noting that I'm on a CS college course and there is strict code copying law that doesn't belong to me. So, if any soul that comes through this can you explain what I am doing wrong, mostly in English. I would like to calculate the code anyway, I'm just logically stuck. thanks in advance

+3


source to share


1 answer


In your driver main()

, you have:

hider.revealLetter(guessedLetter.toLowerCase());
if (hider.revealLetter(guessedLetter)== 0)

      

This is why you get one successful call and then there is nothing to do the second time. There are several stylistic issues that I could highlight, but one big one:

if (letter.length() < 1 || letter.length() > 1) {
    correctChars = 0;
    return correctChars;
} else {

      

Why not easy letter.length() != 1

, but since it's correctChars

already initialized to zero, you don't need to do it again, so the whole "then" part can be discarded, but if

will become letter.length() == 1

.



also:

tempString += Character.toString(hiddenWord.charAt(i));

      

and

tempString += partiallyFoundWord.charAt(i);                                   

      

Both do the same thing, so go for one style or the other.

+4


source







All Articles