Coding for the first time (Java hangman game)- going from console-based to GUI

NewGirl picture NewGirl · Mar 31, 2014 · Viewed 22.8k times · Source

I am completely new to programming and I have to complete some tasks in Java (I'm using Eclipse). This one is a hangman game. My code is below. I have written a console-based program that basically works (counting lives instead of drawing a hangman graphic).

I think this question will seem very simple to most people, but I would really appreciate your help.

I am trying my best to look for answers online, but as a complete novice it is hard to know where to start. I would really just like to know if I am on the right track so that I don't waste too much time.

I am supposed to (and want to!) do this myself, so ideally I would like hints on what to focus on.

The questions I have are:

  1. I now need to create a GUI (which I have never done before): how useful is this code that I have already worked out? Is everything completely different once I start creating the game with a GUI?
  2. I used a StringBuffer to store the previously guessed letters. I wanted to search this in order to display the word with all the previously guessed letters filled in (as it stands it only prints out the current guess with all the other letters obscured). Is that possible?

public static void main(String[] args) {
    Scanner myScanner = new Scanner(System.in);
    StringBuffer buffer = new StringBuffer();

    String secretWord;
    int secretWordLength;
    int position;
    int livesLost = 0;
    int totalLives = 10;
    int lettersRemaining;
    boolean guessInWord;
    char guess;
    StringBuffer prevGuessedLetters;

    //prompt user to enter a word and set as an instance of the secretWord variable
    System.out.println("Enter a word:");
    secretWord = myScanner.next();

    //determine the length of the word entered
    secretWordLength = secretWord.length();
    System.out.println(secretWordLength);
    lettersRemaining = secretWordLength;

    for (position = 0; position < secretWordLength; position++) {
        System.out.print("*");
    }
    System.out.println();

    //loop starts
    while (lettersRemaining > 0 && livesLost < 10) {
        //prompt user to guess a letter
        System.out.println("Guess a letter:");
        guess = myScanner.findWithinHorizon(".", 0).charAt(0);

        //check if the letter guessed is in the secretWord  
        guessInWord = (secretWord.indexOf(guess)) != -1;

        if (guessInWord == false) {
            livesLost++;
            System.out.print("Sorry, you have lost a life. You still have ");
            System.out.print(totalLives -= livesLost);
            System.out.println(" life/lives left. Keep trying.");
        } else {
            System.out.println("That was a good guess, well done!");

            for (position = 0; position < secretWordLength; position++) {
                if (secretWord.charAt(position) == guess) {
                    System.out.print(guess);
                    lettersRemaining--;
                } else {
                    System.out.print("*");
                }
            }
        }
        System.out.println();
        prevGuessedLetters = buffer.append(guess);
        System.out.print("Previously guessed letters: ");
        System.out.println(prevGuessedLetters);
        System.out.print("Letters remaining: ");
        System.out.println(lettersRemaining);
    }

    if (livesLost == totalLives) {
        System.out.println("Sorry, you lose!");
    } else {
        System.out.print("Well done, you win! The word was ");
        System.out.println(secretWord);
    }
}

Answer

2rs2ts picture 2rs2ts · Mar 31, 2014

Java comes with a GUI toolkit called Swing, which you should learn to use for the purposes of your task at hand.

As far as searching for previously guessed letters, you may be better served by the Set, which is a Collection of unique elements. You could use a Set of Characters, i.e. a Set<Character>.

A crude example:

Set<Character> guesses = new HashSet<Character>();
Character guess;
// etc.

while (lettersRemaining > 0 && livesLost < 10)
{
    //prompt user to guess a letter
    System.out.println("Guess a letter:");
    guess = myScanner.findWithinHorizon(".",0).charAt(0);

    if (guesses.contains(guess)) {
        System.out.println("You have already guessed this character!");
    } else {
        guesses.add(guess);
        //check if the letter guessed is in the secretWord
        guessInWord = (secretWord.indexOf(guess))!= -1;
        // etc.
    }
}

Actually, you can get some extra mileage out of the add method by taking advantage of the fact that it returns true if the element was not already in the set:

    if (guesses.add(guess)) {
        //check if the letter guessed is in the secretWord
        guessInWord = (secretWord.indexOf(guess))!= -1;
        // etc.
    } else {
        System.out.println("You have already guessed this character!");
    }