Home > Computer Programming > Java hangman

Java hangman

September 27, 2012 Leave a comment Go to comments

This is hardly a work of art, but it should convince you that if you plan your development before you start coding, you make your life a whole lot easier.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hangman;

import java.util.Scanner;

/**
 *
 * @author robertsonj
 */
public class Hangman {

    static Scanner in;
    static String targetWord;
    static int wordLength;
    static char[] wordArray;
    static char[] guessArray;
    static int wrongGuesses = 0;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int lettersFound = 0;
        String currentGuess;
        String guessedLetters = "";
        final int GUESS_LIMIT = 10;

        in = new Scanner(System.in);

        // Get word from the user
        targetWord = getUserString();

        // Set up parallel arrays
        wordLength = targetWord.length();
        wordArray = new char[wordLength];
        guessArray = new char[wordLength];
        wordArray = targetWord.toCharArray();

        // initialise guessArry to underscores
        for (int i = 0; i < wordLength; i++) {
            guessArray[i] = '_';
        }



        // While letter found counter < num letters in word
        while (lettersFound < wordLength && wrongGuesses < GUESS_LIMIT) {
            // Get a guess
            currentGuess = getUserGuess();
            // If it hasn't been guessed before
            if (guessedLetters.indexOf(currentGuess) == -1) {

                int occurrences = checkGuess(currentGuess);
                if (occurrences != 0) {
                    lettersFound += occurrences;
                } else {
                    wrongGuesses++;
                }
                // Print current status
                printStatus();
            } // Else 
            else {
                // Display invalid guess message
                // continue loop
                System.out.println("You have already guessed that letter. Please guess again.");
                continue;
            }
            // End if

        }
        // End while
        if (wrongGuesses < GUESS_LIMIT) {
            System.out.println("Congratulations, you won!");
        } else {
            System.out.println("You got hanged.");
        }
    }

    static String getUserString() {
        System.out.println("Please enter the Hangman word:");
        return in.nextLine();
    }

    static String getUserGuess() {
        System.out.println("Please guess a letter:");
        return in.nextLine().substring(0, 1); // just in case they enter more than one letter, we'll just return the first
    }

    static int checkGuess(String s) {
        // Run through array 1
        // Wherever it is found
        // Increment letters found counter
        // Change array 2 to match array 1

        int numOccurrences = 0;
        for (int i = 0; i < wordLength; i++) {
            if (String.valueOf(wordArray[i]).equalsIgnoreCase(s)) {
                guessArray[i] = wordArray[i];
                numOccurrences++;
            }
        }
        return numOccurrences;
    }

    static void printStatus() {
        // print number of wrongGuesses
        for (int i = 0; i < wordLength; i++) {
            System.out.print(guessArray[i] + " ");
        }
        System.out.println("");
        System.out.println("You have had " + wrongGuesses + " wrong guesses.");
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s