Stanford CS106A Hangman Console – Based Game Solution

12 Nov

I am really proud to say that I was able to figure out part 1 of Hangman game as documented in Assignment 4 all on my own without any outside help!!! AND the more I program, the more I love it. I couldn’t think of a more perfect way to spend this Friday 11/11/11 🙂

So here is what I have as my solution:


I must say, I’m really liking these strings. You can also find my solution on gist:

<pre>
/*
 * File: Hangman.java
 * ------------------
 * This program will eventually play the Hangman game from
 * Assignment #4.
 */

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.awt.*;

public class Hangman extends ConsoleProgram {

	private HangmanLexicon hangmanWords = new HangmanLexicon();

	private RandomGenerator rgen = RandomGenerator.getInstance();

	/** Tracks the number of guesses the player has */
	private int guessCounter = 8;

    public void run() {
    	setUpGame();
    	playGame();
	}

    /*Set up the game by displaying the welcome message,
     *how many letters there are in the word,
     *and how many guesses the user has
     */
    private void setUpGame() {
    	println("Welcome to Hangman!");
    	println("The word now looks like this: " + hiddenWord);
    	println("You have " + guessCounter + " guesses left.");
    }

    //Generates a random word selected from the HangmanLexicon
    private String pickWord() {
    	int randomWord = rgen.nextInt(0, (hangmanWords.getWordCount() - 1));
    	String pickedWord = hangmanWords.getWord(randomWord);
    	return pickedWord;
    }

    //Shows how many letters there are in the word as part of game setup
	private String showNumberOfLetters() {
		String result = "";
		for(int i = 0; i&lt; word.length(); i++) {
			result = result + "-";
		}
		return result;
		}

	private void playGame() {
		while(guessCounter &gt; 0) {
			String getChar = readLine("Your guess: ");
			while (true) {
				if(getChar.length() &gt; 1) {
					getChar = readLine("You can only guess one letter at a time. Try again: ");
				}
				if(getChar.length() == 1) break;
			}
			ch = getChar.charAt(0);
			if(Character.isLowerCase(ch)) {
				ch = Character.toUpperCase(ch);
			}
			letterCheck();
			if (hiddenWord.equals(word)) {
				println("You guessed the word: " + word);
				println("You win");
				break;
			}
			println("The word now looks like this: " + hiddenWord);
			println("You have " + guessCounter + " guesses left.");

		}
		if (guessCounter == 0) {
			println("You're completely hung.");
			println("The word was:" + word);
			println("You lose.");
		}
	}

	//updates the hiddenWord if the character entered is correct
	private void letterCheck() {
		//checks to see if the guessed letter is in the word
		if(word.indexOf(ch) == -1) {
			println("There are no " + ch + "'s in the word");
			guessCounter--;
		}
		if(word.indexOf(ch) != -1) {
			println("The guess is correct.");
		}
		//goes through each of the letters in the word and checks if it matches the guessed letter,
		//if it's a match, updates the hidden word to reveal the position of the guessed letter
		for(int i = 0; i &lt; word.length(); i++) {
			if (ch == word.charAt(i)) {
				if (i &gt; 0) {
					hiddenWord = hiddenWord.substring(0, i) + ch + hiddenWord.substring(i + 1);
				}
				if(i == 0) {
					hiddenWord = ch + hiddenWord.substring(1);
				}
			}
		}
	}

	//This is the secret word
	private String word = pickWord();

	//This is the word being guessed
	private String hiddenWord = showNumberOfLetters();

	//This is the latest character entered by the user
	private char ch;
}
</pre>
Advertisements

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